Can anyone change this VBA Code to my required way?

  • Thread starter Narayana Reddy Bommu
  • Start date
N

Narayana Reddy Bommu

Hai,
I am looking for bookmarks to be existed even after their text is
auto-inserted. I have the bookmark insert function in this way.

Public Sub BM(NameOfBookmark As String, TextToInsert As Variant)
'this sub handles the insertion text into bookmarks
Dim Bookmark As String
Bookmark = NameOfBookmark
If ActiveDocument.Bookmarks.Exists(NameOfBookmark) Then
ActiveDocument.Bookmarks(NameOfBookmark).Range.Text = TextToInsert

Else
Call BMError(NameOfBookmark)
End If

End Sub

Now the problem is that the bookmark (let's say AgreementDay as
NameOfBookmark) is available in the Microsoft Menu of File-> Insert->
Bookmark options. Once the AgreementDay text is inserted by this call
function, i am not able to see that particular NameOfBookmark again in the
menu of File-> Insert-> Bookmark options. I need the bookmark to exist with
the same name after inserting text as well in the bookmarks list. I tried to
modify the above function in this manner.

Public Sub BM(NameOfBookmark As String, TextToInsert As Variant)
'this sub handles the insertion text into bookmarks
Dim Bookmark As String
Bookmark = NameOfBookmark
If ActiveDocument.Bookmarks.Exists(NameOfBookmark) Then
ActiveDocument.Bookmarks(NameOfBookmark).Range.Text = TextToInsert
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="Bookmark"

Else
Call BMError(NameOfBookmark)
End If

End Sub

But, it is not giving the required result. Can anyone help immediately?
 
H

Helmut Weber

Hi,

like that:

Sub MyBookmark(BookmarkName As String, TexttoInsert As String)
Dim rTmp As Range
If ActiveDocument.Bookmarks.Exists(BookmarkName) Then
Set rTmp = ActiveDocument.Bookmarks(BookmarkName).Range
rTmp.Text = TexttoInsert
rTmp.Bookmarks.Add BookmarkName
Else
MsgBox "No Bookmark: " & BookmarkName
End If
End Sub
' ---------------------------------------------------------------
Sub TestY()
MyBookmark "Test", "xxxxxxxxx"
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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