I am trying to do a find and replace on a word document. I have 2 issues one I get error Exception calling "Execute" with "15" argument(s): "String parameter too long."I'm assuming because my replacement text is over 255 characters. Can someone assist me with an alternative way to get the script to replace over 255 characters?
Here is the code
$Filename=file1.docx
Function OpenWordDoc($Filename)
{
$Word=NEW-Object –comobject Word.Application
Return $Word.documents.open($Filename)
}
[xml]$xmldata = Get-Content "file.xml"
$Doc=OpenWordDoc -Filename "file1.docx"
Function SearchAWord($Document,$findtext,$replacewithtext)
{
$FindReplace=$Document.ActiveWindow.Selection.Find
$matchCase = $false;
$matchWholeWord = $true;
$matchWildCards = $false;
$matchSoundsLike = $false;
$matchAllWordForms = $false;
$forward = $true;
$format = $false;
$matchKashida = $false;
$matchDiacritics = $false;
$matchAlefHamza = $false;
$matchControl = $false;
$read_only = $false;
$visible = $true;
$replace = 2;
$wrap = 1;
$FindReplace.Execute($findText, $matchCase, $matchWholeWord,`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, $forward, $wrap,`
$format, $replaceWithText, $replace, $matchKashida ,$matchDiacritics,`
$matchAlefHamza, $matchControl) | Out-Null
}
Function SaveAsWordDoc($Document,$FileName)
{
$Document.Saveas([REF] $Filename)
$Document.close()
}
$checkcontent = $xmldata.Benchmark.Group.Rule.check.'check-content'
$description = $xmldata.Benchmark.group.rule.description
SearchAWord –Document $Doc -findtext '<Information derived from discussion>' -replacewithtext $description
SearchAWord –Document $Doc -findtext '<Information derived from content>' -replacewithtext $checkcontent
SaveAsWordDoc –document $Doc –Filename "results.docx"
Any advice is appreciated.