Author Posts

April 26, 2016 at 10:50 am

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.

April 26, 2016 at 11:03 am

All you can do it try replacing smaller chunks of text that are less than 255 characters, I'd guess.

April 26, 2016 at 11:08 am

Not sure if I can do that I am replacing one word with a paragraph... is there a code in PowerShell to copy to clipboard and paste in a certain area?

April 26, 2016 at 12:08 pm

or what about delete and paste

April 27, 2016 at 5:20 am

I ended up using
$description = $xmldata.Benchmark.group.rule.description
$checkcontent = $xmldata.Benchmark.Group.Rule.check.'check-content'

$objrange = $doc.Bookmarks.Item("Description").Range
$objrange.Text = $description

$objrange = $doc.Bookmarks.Item("CheckContent").Range
$objrange.Text = $checkcontent

instead of

$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

April 4, 2017 at 11:11 am

Same situation was for me before but when i installed Long Path Tool, my computer was organized easily and i got relaxed from everyday files and junk.

April 20, 2017 at 10:35 am

In other words Long Path Tool is useful here because you can easily replace text in powershell with the help of this tool