create a bookmark

S

Shawn G.

What is the VBA to find text and create a bookmark?
I need to search the doc for a prase, <insert bookmark>
then turn that into a bookmark named bkmrkName

Any ideas?

Thanks!
 
H

Helmut Weber

Hi Shawn,

as easy as that, once you know.

Sub test0009()
Dim rDoc As Range
Set rDoc = ActiveDocument.Range
resetsearch
With rDoc.Find
.Text = "Steve"
If .Execute Then
rDoc.Bookmarks.Add Name:="Name", Range:=rDoc
End If
End With
resetsearch
End Sub

' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

Shawn G.

I was hopeing you were online! It works great Thanks!!!
An idea how to make this work from excel?
 
H

Helmut Weber

Hi Shawn,

like this:

Sub Exceltest0009()
' reference to word is set
' in tools references
' the word document is already open
Dim oWrd As Word.Application
Set oWrd = GetObject(, "Word.Application")
Dim oDoc As Word.Document
Set oDoc = oWrd.ActiveDocument
Dim rTmp As Word.Range
Set rTmp = oDoc.Range
With rTmp.Find
.Text = "Krypton"
If .Execute Then
rTmp.Bookmarks.Add Name:="Krypton", _
Range:=rTmp
End If
End With
End Sub

see:
http://word.mvps.org/faqs/interdev/EarlyvsLateBinding.htm
http://word.mvps.org/faqs/interdev/ControlWordFromXL.htm

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

Shawn G.

Helmut,
You are awesome! I spent all day yesterday trying to make this work and even
asked several times on the Excel programming Group And you solved the
problem!!!
Thank You Very Much!!!!!
 

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