#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    Word macro for adding comments


    Hi, I'm trying to write a macro to search through a word document for certain words, and add a comment to each instance of those words. Unfortunately I have no idea where to begin (though I've written a lot of VB - just not macros). Is there a simple way to do this, or, alternatively, a good on-line resource for me to look at?

    Thanks!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0
    Ok - I'm using word XP, but I would imagine that the process would be pretty similar for earlier versions.

    If you try the function below then it shows how you can at least set a comment with a range object.

    Sub test()
    Set aRange = ActiveDocument.Range(1, 5)
    ActiveDocument.Comments.Add aRange, "This is a comment"
    End Sub


    To access the text content of the window then you can refer to the following line of code:

    ActiveDocument.Content.Text

    You can then just walk the string to get the start points of your particular word/phrase, and since you have already defined the phrase itself, you should know your length (and so the end points can be calculated from it).

    Then you just have to go through and create a range object each time and assign the comment text that you want to that range. If you check the online help for the comment object it will give you some more info.

    Good luck.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0
    Ok - so I decided that your problem was a little bit more interesting than I first thought - there are some good applications for it.

    The two functions below will do pretty much what you're looking for - the first creates comments for the word you enter into the first input box, the second gets rid of ALL comments in a document - just in case you want to do some testing.

    I should warn you, though, that this code is _really_ slow... Every time you create a new comment object it has to initialize and then incorporate it into the document. I've included a doEvents (commented out) which you can use to make sure the window still accepts some inputs while processing.

    Public Sub addComments()
    Dim intCount As Integer
    Dim strSearch As String
    Dim strComment As String
    Dim intDiff As Integer

    strSearch = InputBox("Please enter the word to search for:", "Search")
    strComment = InputBox("Please enter the comment you wish to add to all instances of [" & strSearch & "].", "Search")

    For intCount = 1 To ActiveDocument.Words.Count
    With ActiveDocument
    If (LCase(Trim(.Words(intCount).Text)) = strSearch) Then
    intDiff = Len(.Words(intCount).Text) - Len(Trim(.Words(intCount).Text))
    '.Words(intCount).Font.Italic = True
    .Comments.Add .Range(.Words(intCount).Start, (.Words(intCount).End - intDiff)), strComment

    'Uncomment the lines below to add event handling while process is running
    'If (intCount Mod 2) = 1 Then
    ' DoEvents
    'End If
    End If
    End With
    Next

    End Sub

    Public Sub remComments()
    Dim intCount As Integer
    For intCount = 1 To ActiveDocument.Comments.Count
    ActiveDocument.Comments(1).Delete
    Next
    End Sub
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    THANKS! How about phrases?


    To ajmh: Thanks a million! The code you provided works very well.

    Could anyone assist with doing the same thing for phrases, such as "zoo keeper" or "zoo keeper's helper"?


    Originally Posted by ajmh
    Ok - so I decided that your problem was a little bit more interesting than I first thought - there are some good applications for it.

    The two functions below will do pretty much what you're looking for - the first creates comments for the word you enter into the first input box, the second gets rid of ALL comments in a document - just in case you want to do some testing.

    I should warn you, though, that this code is _really_ slow... Every time you create a new comment object it has to initialize and then incorporate it into the document. I've included a doEvents (commented out) which you can use to make sure the window still accepts some inputs while processing.

    Public Sub addComments()
    Dim intCount As Integer
    Dim strSearch As String
    Dim strComment As String
    Dim intDiff As Integer

    strSearch = InputBox("Please enter the word to search for:", "Search")
    strComment = InputBox("Please enter the comment you wish to add to all instances of [" & strSearch & "].", "Search")

    For intCount = 1 To ActiveDocument.Words.Count
    With ActiveDocument
    If (LCase(Trim(.Words(intCount).Text)) = strSearch) Then
    intDiff = Len(.Words(intCount).Text) - Len(Trim(.Words(intCount).Text))
    '.Words(intCount).Font.Italic = True
    .Comments.Add .Range(.Words(intCount).Start, (.Words(intCount).End - intDiff)), strComment

    'Uncomment the lines below to add event handling while process is running
    'If (intCount Mod 2) = 1 Then
    ' DoEvents
    'End If
    End If
    End With
    Next

    End Sub

    Public Sub remComments()
    Dim intCount As Integer
    For intCount = 1 To ActiveDocument.Comments.Count
    ActiveDocument.Comments(1).Delete
    Next
    End Sub
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0
    Thanks for nice information.

IMN logo majestic logo threadwatch logo seochat tools logo