Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old September 11th, 2003, 09:23 AM
Asbestos Asbestos is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 Asbestos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old September 13th, 2003, 07:00 AM
ajmh ajmh is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 19 ajmh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #3  
Old September 14th, 2003, 06:06 AM
ajmh ajmh is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 19 ajmh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Word macro for adding comments


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway