Member Avatar for Member #1002324

I want to make macro in Word to find "the" in document and replace it with "THE". Only words which have space before and after "the" should be changed and the in these or words like that should not change. How can I write commands for it in Word. I want to run it in command prompt. Which command is required to execute it?

Dani AI

Generated

As wanted a macro (instead of the manual Find/Replace suggested by ), here are two simple VBA options plus two ways to run them from the command line. One macro matches literal spaces on both sides of "the" (exact-space rule). The other uses Word's whole-word option (safer: it will not change "these" or "other" and also handles punctuation). Save the document as a macro-enabled file (.docm) and test on a copy.

' literal spaces: only matches " the " (space before and after)
Sub ReplaceSpaceSurroundedThe()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = " the "
        .Replacement.Text = " THE "
        .Forward = True
        .Wrap = wdFindContinue
        .MatchCase = False   ' set True to only match lowercase "the"
        .MatchWholeWord = False
    End With
    ActiveDocument.Content.Find.Execute Replace:=wdReplaceAll
End Sub
' whole-word: matches "the" as a separate word (recommended)
Sub ReplaceWholeWordThe()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = "the"
        .Replacement.Text = "THE"
        .Forward = True
        .Wrap = wdFindContinue
        .MatchCase = False   ' set True to only replace lowercase "the"
        .MatchWholeWord = True
    End With
    ActiveDocument.Content.Find.Execute Replace:=wdReplaceAll
End Sub

To run from Command Prompt:

  • Easiest: create an AutoOpen procedure that calls one of the above macros, save as .docm, then open the file from the command line. Example:
    start "" "C:\Full\Path\YourDoc.docm"
    (Word will open the doc and AutoOpen will run if macro security allows it.)
  • More controlled: run a small VBScript that starts Word, opens the doc and runs the macro. Example run file (execute with cscript //nologo RunWordMacro.vbs):
Set wd = CreateObject("Word.Application")
wd.Visible = False
Set doc = wd.Documents.Open("C:\Full\Path\YourDoc.docm")
wd.Run "ReplaceWholeWordThe"   ' or include project/doc prefix if needed
doc.Save
doc.Close
wd.Quit

Cautions: macros will be blocked unless the file is in a Trusted Location or macros are enabled/signed. Use a copy when testing. If you truly need only space-delimited matches, use the first macro; if you want any standalone "the" (even before punctuation), use the whole-word version.

Recommended Answers

All 4 Replies

Have you considered just using the "Find and Replace" tool? You would just tell word you want to find " the " and replace it with " THE ". Tell the tool to match the case in the advanced options.

Member Avatar for Member #1002324

Actually it is my Assignment to make macro for this purpose. Otherwise there is option available in Word to Find and Replace any word in document.

I see. I am not familiar with using macros in word, but found this page online that appears to be inline with what you are tyring to do.

Member Avatar for Member #1002324

Thanks JorgeM. My work was done. This website is quite helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.