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

A

Alice

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
 
D

Doug Robbins

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


--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top