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 replace the text with 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()
}
$key = Read-Host "Please enter Key"
foreach ($keys in $xmldata.Benchmark.Group)
{
$checkcontent = $xmldata.Benchmark.Group.Rule.check.'check-content'
$description = $xmldata.Benchmark.group.rule.description
SearchAWord –Document $Doc -findtext '' -replacewithtext $description
SearchAWord –Document $Doc -findtext '' -replacewithtext $checkcontent
SaveAsWordDoc –document $Doc –Filename "file.docx"
}
}
Any advice is appreciated.