none
Find Word in Document and Change it to Bold RRS feed

  • Question

  • I am calling a VBscript inside of my PowerShell script. I am trying  to find word inside a word document and change that specified word in bold throughout the document.

    The code is giving me an error:

    cscript : C:\Users\J54121\Desktop\Scripts\WordDoc\boldfont.vbs(2, 28) 
    Microsoft VBScript compilation error: Invalid character
    At C:\Users\J54121\Desktop\Scripts\WordDoc\WordDocChange.ps1:150 char:1
    + cscript "C:\Users\J54121\Desktop\Scripts\WordDoc\boldfont.vbs"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (C:\Users\J54121...valid character 
       :String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    Here is the code I am using

    Const wdReplaceAll  = 2
    Set objWord = CreateObject(“Word.Application”)
    objWord.Visible = True
    Set objDoc = objWord.Documents.Open(“$newfile”)
    Set objSelection = objWord.Selection
    objSelection.Find.Text = “Check Content”
    objSelection.Find.Forward = TRUE
    objSelection.Find.MatchWholeWord = TRUE
    objSelection.Find.Replacement.Font.Bold = True
    objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

    $newfile is a variable inside of my powershell script.

    I am not a VBscript person. Can someone tell me what I am doing wrong.



    • Edited by Ms Long Friday, May 13, 2016 2:55 PM
    Friday, May 13, 2016 2:31 PM

Answers

  • PowerShell:

    $searchText=$replaceText= 'Check Content'
    $newfile='d:\test\newtest.docx'
    $wdReplaceAll  = 2
    $wd=New-Object -ComObject Word.Application
    $wd.Visible=$true
    $doc = $wd.Documents.Open($newfile)
    $wd.Selection.Find.Replacement.Font.Bold = $true
    $wd.Selection.Find.Execute($searchText, $false, $true, $false, $false, $false, $true, $false, $true, $replaceText, $wdReplaceAll)
    # save - close - quit
    


    \_(ツ)_/

    • Marked as answer by Ms Long Friday, May 13, 2016 4:32 PM
    Friday, May 13, 2016 3:30 PM

All replies

  • Are you trying to remote this?  You cannot remotely execute word via a vbscript.

    This is the offending line.

    Set objWord =CreateObject(“Word.Application”)

    If you are not trying to remote then The line has illegal characters such as the smart quotes.  Retype - don't paste - the code using notepad.


    \_(ツ)_/



    • Edited by jrv Friday, May 13, 2016 2:49 PM
    Friday, May 13, 2016 2:47 PM
  • Also note that the following line will not work in VBScript:

    Set objDoc = objWord.Documents.Open(“$newfile”)

    You cannot reference PowerShell variables in VBScript.



    \_(ツ)_/

    Friday, May 13, 2016 2:51 PM
  • Ok I will try to retype it. I got the code from:

    https://blogs.technet.microsoft.com/heyscriptingguy/2006/02/01/how-can-i-boldface-a-specific-word-throughout-a-microsoft-word-document/

    I wasn't necessarily trying to remote. I just wanted to access the file and change the word I specify to bold...

    Friday, May 13, 2016 2:59 PM
  • PowerShell:

    $searchText=$replaceText= 'Check Content'
    $newfile='d:\test\newtest.docx'
    $wdReplaceAll  = 2
    $wd=New-Object -ComObject Word.Application
    $wd.Visible=$true
    $doc = $wd.Documents.Open($newfile)
    $wd.Selection.Find.Replacement.Font.Bold = $true
    $wd.Selection.Find.Execute($searchText, $false, $true, $false, $false, $false, $true, $false, $true, $replaceText, $wdReplaceAll)
    # save - close - quit
    


    \_(ツ)_/

    • Marked as answer by Ms Long Friday, May 13, 2016 4:32 PM
    Friday, May 13, 2016 3:30 PM
  • After retyping it. It didn't give me the error. THANKS for that!!

    I was able to pass $newfile to the vbscript by using:

    $args = "$newfile"
    $command = "cmd /C cscript ".\boldfont.vbs" $args"
    invoke-expression $command

    in my powershell script. Once I did that it worked.

    I wanted to give your script a try but I am getting error.

    C:\Users\J54121\Desktop\Scripts\WordDoc\boldfont.vbs(1, 1) Microsoft VBScript compilation error: Invalid character

    $searchText = replaceText="Check Content"
    $wdReplaceAll = 2
    $wd = New-object -ComObject Word.Application
    $wd.Visible=$true
    $doc = $wd.Documents.Open($newfile)
    $wd.selection.find.replacemnt.font.bold = $true
    $wd.selection.Find.Execute($searchtext, $false, $true, $false, $false, $false, $true, $false, $true, $replacetext, $wdReplaceAll)

    I did type it in manually





    • Edited by Ms Long Friday, May 13, 2016 4:07 PM
    Friday, May 13, 2016 4:03 PM
  • I wanted to give your script a try but I am getting error.

    C:\Users\J54121\Desktop\Scripts\WordDoc\boldfont.vbs(1, 1) Microsoft VBScript compilation error: Invalid character

    <snip>

    I did type it in manually

    That's because what jrv posted is PowerShell, not VBScript.

    Friday, May 13, 2016 4:09 PM
  • Ahhh!!! I feel stupid for that one.

    It worked. Thanks a lot for that as well. I think I like your way better, but I'm glad I discovered the VBScript way as well.

    Friday, May 13, 2016 4:32 PM
  • Ahhh!!! I feel stupid for that one.

    It worked. Thanks a lot for that as well. I think I like your way better, but I'm glad I discovered the VBScript way as well.

    It is actually best to stay in one scripting system when possible.  Existing VBScript is useful but always try for a PowerShell solution first.  It will save time.

    Have fun scripting.


    \_(ツ)_/

    Friday, May 13, 2016 4:48 PM