String Too Long Error, Find and Replace Text in Powershell

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of frank jackson frank jackson 5 days, 7 hours ago.

  • Author
    Posts
  • #38266
    Profile photo of Long S
    Long S
    Participant

    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.

  • #38269
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #38271
    Profile photo of Long S
    Long S
    Participant

    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?

  • #38272
    Profile photo of Long S
    Long S
    Participant

    or what about delete and paste

  • #38289
    Profile photo of Long S
    Long S
    Participant

    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

  • #67861
    Profile photo of frank jackson
    frank jackson
    Participant

    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.

    • #69022
      Profile photo of frank jackson
      frank jackson
      Participant

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

You must be logged in to reply to this topic.