0

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.

7
  • Yes, there's a 255 character limit via the object model. Commented Apr 26, 2016 at 17:43
  • As to the second issue - that really should be in a separate question as the two things are totally unrelated. You don't show us any of the Word.Application management you use which makes it impossible to say what the problem might be, but... Have you sent the Application.Quit() command? And do you correctly release all Word-related objects so that they don't "hold pointers"? Commented Apr 26, 2016 at 17:46
  • I edited the question to support one question ad list the Word.application management. My main issue is getting it to go over the 255 characters. Commented Apr 26, 2016 at 18:18
  • Cindy has already told you that Word has a 255-character limit when you're using it via the object model. That means that as long as you're using the object model, you can't get it to go over the 255 characters. Commented Apr 26, 2016 at 18:28
  • Understood. What I was looking for was an alternative way since that is the case. Commented Apr 26, 2016 at 18:32

1 Answer 1

2

I ended up revamping my code a lot. I assigned some bookmarks inside my word document and assigned those bookmarks a bookmark variable. so this code will find the bookmark I set and replace it with what I told it to.

Here are the final results:

$template = "template.docx"
$wd = New-Object –comobject Word.Application
$doc=$wd.documents.Add($template)

[xml]$xmldata = Get-Content "file.xml"
$newfile="file.docx"

$description = $xmldata.Benchmark.group.rule.description
$checkcontent = $xmldata.Benchmark.Group.Rule.check.'check-content'

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

#replace Check Content Bookmark
$objrange = $doc.Bookmarks.Item("CheckContent").Range 
$objrange.Text = $checkcontent

#save and close document
$doc.SaveAs([ref]$newfile)
$doc.Close()
$wd.Quit()
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, if your goal is to use a "template" to plug data into specific places, bookmarks is the better approach than Find/Replace :-) For another time, including this kind of information (why your code is doing what it's doing) would illicit suggestions for alternate approaches. Good job!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.