Using Excel VBA, how to search for a specific word and insert comments against that word in a Word document?

Veda Prakash Chinta

I am trying to create an excel based tool that reviews Word documents for specific errors. I want this tool to search for a word/sentence and insert a comment against it. I have written a code (please see below) that is able to highlight the word/sentence, however, unable to insert the comment.

Here is my code so far:

Sub Ref_Figs_Tbls()

    Dim wdDoc As Object

    Set wdDoc = ActiveDocument

    With wdDoc
        With .Range
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .MatchWildcards = True
                .Wrap = wdFindStop
                .Text = "Reference source not found"
                .Replacement.Text = ""
                .Execute
            End With

            Do While .Find.Found = True

                .Select
                .HighlightColorIndex = wdRed

                .Select
                Selection.Comments.Add Range:=Selection.Range
                Selection.TypeText Text:="Cross referencing error"

                .Collapse wdCollapseEnd
                .Find.Execute
            Loop
        End With
    End With

End Sub
DisplayName

Since you say you're acting from within Excel Application, then an unqualified Selection object would reference the host application, i.e. it'd return the Excel Selection edited to add a Word host application code

Hence you have to explicitly qualify Word application object as the Parent of the wanted Selection object (which I can't see any trace of in your code, though...)

Sub Ref_Figs_Tbls()


    Dim WordApp As Object

    'try and get Word application object, or exit sub
    Set WordApp = GetObject(, "Word.Application")
    If WordApp Is Nothing Then Set WordApp = CreateObject("Word.Application")
    If WordApp Is Nothing Then: MsgBox "Can't get a Word instance", vbCritical: Exit Sub

    With WordApp.ActiveDocument ' reference word application currently active document
        With .Range
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .MatchWildcards = True
                .Wrap = wdFindStop
                .text = "Reference source not found"
                .Replacement.text = ""
                .Execute
             End With

            Do While .Find.Found = True
                .Select
                With WordApp.Selection ' explicitly reference Word application object selection
                    .Range.HighlightColorIndex = wdRed
                    .Range.Comments.Add Range:=.Range '.Find.Parent
                    .text = "Cross referencing error"
                End With
                .Collapse wdCollapseEnd
                .Find.Execute
            Loop
        End With
    End With
    Set WordApp = Nothing
End Sub

BTW you don't need all that Select/Selection work, and you can directly work with wanted objects

for instance the Do While .Find.Found = True loop can turn into

        Do While .Find.Found = True
            With .Find ' reference the Find object
                .Parent.HighlightColorIndex = wdRed ' set Find Parent object (i.e. its Range) color
                .Parent.Comments.Add(Range:=.Parent).Range.text = "Cross referencing error" ' set Find Parent object (i.e. its Range) comment object text
                .Execute
            End With
        Loop

using Word as host application, the above code would simplify to:

Option Explicit

Sub Ref_Figs_Tbls()

    Dim wdDoc As Document

    Set wdDoc = ActiveDocument

    With wdDoc
        With .Range
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .MatchWildcards = True
                .Wrap = wdFindStop
                .Text = "Reference source not found"
                .Replacement.Text = ""
                .Execute
             End With

            Do While .Find.Found = True
                With .Find
                    .Parent.HighlightColorIndex = wdRed
                    .Parent.Comments.Add(Range:=.Parent).Range.Text = "Cross referencing error"
                    .Execute
                End With
            Loop
        End With
    End With

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Search specific selection of Word document in VBA

From Dev

How to get the range of a specific word in Word Document using Word Interop

From Dev

how to search an epub document for a specific word (in windows)?

From Dev

Excel VBA quit Word document

From Dev

Insert VBA into Word from Excel

From Dev

Insert table into a word document at a specific position using apache poi

From Dev

VBA Word: Insert text before found text in word document

From Dev

Insert a wdFieldDate into a Word (2010) document with VBA

From Dev

Insert chart as image into Word document from Excel

From Dev

VBA: Within Excel how do I save a word document as a PDF?

From Dev

Save embedded word document from Excel VBA

From Dev

Open and Close Word Document from VBA Excel

From Dev

Insert PDF into Word document

From Dev

Python - Insert data into a word document using a list

From Dev

VBA Excel word search and copying formulas

From Dev

VBA Excel word search and copying formulas

From Dev

How do I search for a word in Excel Using VBA and then Delete the entire row?

From Dev

How do I search for a word in Excel Using VBA and then Delete the entire row?

From Dev

How to select every table in a Microsoft Word document using VBA Macro

From Dev

How to find equation editor in word document using VBA?

From Dev

Filter out a specific word in Excel Vba

From Dev

How to insert a paragraph by replacing a string in word document

From Dev

Word 2013: Shortcut to insert timestamp into word document

From Dev

How to search a word in a dictionary with a document in R?

From Dev

Search specific word in mysql

From Dev

Laravel search for specific word

From Dev

Excel – Generating Word document using VB Script

From Dev

Excel – Generating Word document using VB Script

From Dev

Using Excel VBA to change the colour of a word based on the word in a string?

Related Related

  1. 1

    Search specific selection of Word document in VBA

  2. 2

    How to get the range of a specific word in Word Document using Word Interop

  3. 3

    how to search an epub document for a specific word (in windows)?

  4. 4

    Excel VBA quit Word document

  5. 5

    Insert VBA into Word from Excel

  6. 6

    Insert table into a word document at a specific position using apache poi

  7. 7

    VBA Word: Insert text before found text in word document

  8. 8

    Insert a wdFieldDate into a Word (2010) document with VBA

  9. 9

    Insert chart as image into Word document from Excel

  10. 10

    VBA: Within Excel how do I save a word document as a PDF?

  11. 11

    Save embedded word document from Excel VBA

  12. 12

    Open and Close Word Document from VBA Excel

  13. 13

    Insert PDF into Word document

  14. 14

    Python - Insert data into a word document using a list

  15. 15

    VBA Excel word search and copying formulas

  16. 16

    VBA Excel word search and copying formulas

  17. 17

    How do I search for a word in Excel Using VBA and then Delete the entire row?

  18. 18

    How do I search for a word in Excel Using VBA and then Delete the entire row?

  19. 19

    How to select every table in a Microsoft Word document using VBA Macro

  20. 20

    How to find equation editor in word document using VBA?

  21. 21

    Filter out a specific word in Excel Vba

  22. 22

    How to insert a paragraph by replacing a string in word document

  23. 23

    Word 2013: Shortcut to insert timestamp into word document

  24. 24

    How to search a word in a dictionary with a document in R?

  25. 25

    Search specific word in mysql

  26. 26

    Laravel search for specific word

  27. 27

    Excel – Generating Word document using VB Script

  28. 28

    Excel – Generating Word document using VB Script

  29. 29

    Using Excel VBA to change the colour of a word based on the word in a string?

HotTag

Archive