Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

VBA - Help defining for a "For Each....Next" command

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alice - 30 Jul 2005 19:05 GMT
I am advanced in Word, but new to using VBA, having watched Steph Krieger's
Webcast and read a few of your MVP articles on editing macros in VBA.  I am
using Word 2003 and am having trouble with the Dim "MyText" As _____ command
in order to do the For Each.....Next command.  I don't know what to define
the MyText as.  The macro finds the text "Matt.", then a Hyperlink to a
spcific Bookmark in the active document is inserted.  There are several
hundred instances of "Matt." in my document.  If I could get help with this
step, then I will be able to create macros for the 27 other Bookmarks in my
document!

Here is my initial macro (edited) and it does work:

Option Explicit
Sub MattB()
'
' MattB Macro
' Bookmark to Matthew in TOC
'
   With Selection.Find
       .ClearFormatting
       .Style = ActiveDocument.Styles( _
       "Style Body TextBT + Bold Char")
       .Text = "Matt."
       .Replacement.Text = ""
       .Forward = True
       .Wrap = wdFindAsk
       .Format = True
       .MatchCase = True
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute
   ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
       SubAddress:="Matthew", ScreenTip:="", TextToDisplay:="Matt."
End Sub
Signature

Thanks, Danke sehr, Merci!
Alice

Doug Robbins - 30 Jul 2005 19:44 GMT
Hi Alice,

This is the way to do it:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
   Do While .Execute(FindText:="Matt.", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
       ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="",
_
       SubAddress:="Matthew", ScreenTip:="", TextToDisplay:="Matt."
   Loop
End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

>I am advanced in Word, but new to using VBA, having watched Steph Krieger's
> Webcast and read a few of your MVP articles on editing macros in VBA.  I
[quoted text clipped - 37 lines]
>        SubAddress:="Matthew", ScreenTip:="", TextToDisplay:="Matt."
> End Sub

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.