September 11th, 2003, 10:23 AM
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?
September 13th, 2003, 08:00 AM
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.
Set aRange = ActiveDocument.Range(1, 5)
ActiveDocument.Comments.Add aRange, "This is a comment"
To access the text content of the window then you can refer to the following line of code:
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.
September 14th, 2003, 07:06 AM
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
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
Public Sub remComments()
Dim intCount As Integer
For intCount = 1 To ActiveDocument.Comments.Count
October 18th, 2011, 10:08 AM
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
November 28th, 2011, 06:20 AM
Thanks for nice information.