Add Bookmark

M

Mario

My document is separate in differant section. I made a
macro to add automaticly a new section when I need. This
new section was automaticly set up, like header, footer,
etc...

I want to add a bookmark at the begining of the new
section. The name of the bookmark is suppose to be the
name of the previous bookmark + 1 (Previous bookmark + 1 =
New bookmark - A1 + 1 = A2). I was abble to find the name
of the previous bookmark but not abbel to add 1 to the
bookmark because the previous bookmark is alphanumeric.

Could someone help me with this one ?

Thanks Mario
 
P

Peter Hewett

Hi Mario

This code will do what you want:

Public Function IncBookmarkNumber(ByVal strBookmark As String) As String
Dim lngIndex As Long
Dim lngNumber As Long
Dim strNumber As String

' Look for an underscore "_" character which separates
' the bookmark name from the bookmark number
lngIndex = InStrRev(strBookmark, "_")
If lngIndex > 0 Then

' Increment the bookmark number and return the updated bookmark name

' Do this the safe way so the code is robust
strNumber = Mid$(strBookmark, lngIndex + 1)
If IsNumeric(strNumber) Then
lngNumber = CInt(strNumber) + 1
IncBookmarkNumber = Left$(strBookmark, lngIndex) & CStr(lngNumber)
Else

' Text after the underscore is not numeric so add a numeric suffix
IncBookmarkNumber = strBookmark & "_1"
End If
Else

' No underscore present so add the first
IncBookmarkNumber = strBookmark & "_1"
End If
End Function ' IncBookmarkNumber

To use this code the bookmark names use the format of "Name_Number". So valid names are
"Bookmark_1", "Section_3" etc. If you pass in a bookmark name of "Section" it will return
a name of "Section_1". If you pass in a name of "AdddressBlock_6" it will return
"AdddressBlock_7".

To call it use:
Dim strNewBookmarkName As String
Dim strOldBookmarkName As String

strOldBookmarkName = "Section_5"
strNewBookmarkName = IncBookmarkNumber(strOldBookmarkName)

HTH + Cheers - Peter


My document is separate in differant section. I made a
macro to add automaticly a new section when I need. This
new section was automaticly set up, like header, footer,
etc...

I want to add a bookmark at the begining of the new
section. The name of the bookmark is suppose to be the
name of the previous bookmark + 1 (Previous bookmark + 1 =
New bookmark - A1 + 1 = A2). I was abble to find the name
of the previous bookmark but not abbel to add 1 to the
bookmark because the previous bookmark is alphanumeric.

Could someone help me with this one ?

Thanks Mario

HTH + Cheers - Peter
 

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