|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
|
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Word macro for adding comments |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|