Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Acessing a bookmark in a header

Status
Not open for further replies.

Viruland

Programmer
Dec 6, 2000
61
BE
Hi,

I'm trying to access a bookmark in a header of my word document by using a macro. Can someone tell me how you can do it.

Thx

Live fast, die young and leave a beautiful corpse behind.
 
Hi Viruland,

[blue]
Code:
activedocument.Bookmarks("[i]YourBookmark[/i]").Select
[/blue]
should do it.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
You may want to be careful. If you could clarify "access"?

Does the bookmark hold text, and you want to change it?

Does the bookmark just mark a location, and you want to put text after it (or before it)?

Are you going to action this more than once?

All of the above may affect what actually happens to the bookmark. For example, if you have some text actually bookmarked (not just the location of the start, but the text ITSELF), the if you Select it, then enter in new text, the new text is NOT included. Or worse can delete the bookmark itself. For example, assuming you have "This Header" as bookmarked text.

Example #1
ActiveDocument.Bookmarks("myheader").Select
Selection.InsertAfter Text:=" new text"

RESULT: "This Header new text"

However, if you run
ActiveDocument.Bookmarks("myheader").Select

again, ONLY "This Header" is selected, " new text" remains but will NOT be selected.

Example #2:
ActiveDocument.Bookmarks("myheader").Select
Selection.TypeText Text:=" new text"

will type in " new text" but replaces the bookmarked text, AND deletes the bookmark.

There are many ways to handle these situations.

Gerry
 
To access a bookmark in the header please add this code to your vba script

Dim oRange As Range
Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Bookmarks("TEST").Range
oRange.Text = "TEST THIS BOOKMARK"
ActiveDocument.Bookmarks.Add Name:="TEST", Range:=oRange

Last line is not really needed, but should add the bookmark to the bookmarks of the activedocument
 
Ah onekabee...
"Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
.Range.Bookmarks("TEST").Range"

will work fine, IF

1. There is only ONE Section - you have hard coded the Section index.
2. That Section does not have either Different first page, or Different odd/even. It does not cover the possibility of wdHeaderFooterFirstPage, or wdHeaderFooterEvenPages
3. There is no use of Continuous Section breaks, for example, in the use of protected areas for form fields.

Plus, as I was trying to state in my post that you have to be careful, your statement that "the last line is not really neded" is incorrect - that is, unless this is a ONE TIME ONLY operation.

It absolutely IS needed if you ever want to run it again. Inserting the text, even as a range, deletes the bookmark - at least it does with Word 2002. That is why, your code is correct, in that you need to recreate the bookmark with the Range. For example:

Code:
Dim oRange As Range
Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
    .Range.Bookmarks("TEST").Range
oRange.Text = "TEST THIS BOOKMARK"

[COLOR=red]' take out the recreation of the bookmark
'ActiveDocument.Bookmarks.Add Name:="TEST", Range:=oRange[COLOR=red]

Set oRange = Nothing
[COLOR=red] ' run it again with new text
Set oRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
    .Range.Bookmarks("TEST").Range
oRange.Text = "TEST THIS SECOND BOOKMARK"

The second insertion of text will fail - the object (the bookmark) does not exist.


Gerry
 
new to VBA, how could i handle header-sections, with a loop,

Could you give a link for more detailed info ...


Thanks for the comment
 
Sure, if you give some more specs about what you want to do.

The following checks through every header, in every section, through every header type (there are three in each header), and - if there is a bookmark in that header and header type - displays a message stating what section it is, which header it is, which header type it is, and most importantly, what the name of the bookmark is. That way, you can extract the name of a bookmark (if one exists) and use it for other processing.

Code:
Sub CheckForBookmarks()

Dim r As Range
Dim i As Integer
Dim SectionCount As Integer
Dim BookmarkIndexNumber As Integer
Dim strBookmarkName As String
Dim msg As String
Dim var
Dim var2
Dim var3

SectionCount = 1
i = 1
For var = 1 To ActiveDocument.Sections.Count
  For var2 = 1 To 3
    Set r = ActiveDocument.Sections(SectionCount).Headers(i).Range
    If r.Bookmarks.Count > 0 Then
      Select Case i
        Case 1
          BookmarkIndexNumber = 1
          For var3 = 1 To r.Bookmarks.Count
           msg = msg & r.Bookmarks(BookmarkIndexNumber).Name
           BookmarkIndexNumber = BookmarkIndexNumber + 1
          Next
          MsgBox "Section " & SectionCount & _
            " Primary header contains " & _
            r.Bookmarks.Count & " bookmark(s)." & _
            vbCrLf & "Their name(s) are: " & vbCrLf & _
            msg             
         Case 2
          BookmarkIndexNumber = 1
          For var3 = 1 To r.Bookmarks.Count
           msg = msg & r.Bookmarks(BookmarkIndexNumber).Name
           BookmarkIndexNumber = BookmarkIndexNumber + 1
          Next
          MsgBox "Section " & SectionCount & _
            " First Page header contains " & _
            r.Bookmarks.Count & " bookmark(s)." & _
            vbCrLf & "Their name(s) are: " & vbCrLf & _
            msg                                  
         Case 3
          BookmarkIndexNumber = 1
          For var3 = 1 To r.Bookmarks.Count
           msg = msg & r.Bookmarks(BookmarkIndexNumber).Name
           BookmarkIndexNumber = BookmarkIndexNumber + 1
          Next
          MsgBox "Section " & SectionCount & _
           " Primary header contains " & _
           r.Bookmarks.Count & " bookmark(s)." & _
           vbCrLf & "Their name(s) are: " & vbCrLf & _
           msg
       End Select
     End If
     Set r = Nothing
     i = i + 1
  Next
  SectionCount = SectionCount + 1
  i = 1
Next
End Sub

Gerry
 
Gerry,
thanks for the added a simple "vbCrLf" for more clear reading.

Works fine ....

but what do you mean with through every header type (there are three in each header), are ther three header types ??
wdHeaderFooterFirstPage ?
wdHeaderFooterPrimary ?
 
Every section has three headers. Even if you do nothing at all, there are always three headers.

The first is wdHeaderFooterPrimary. As its name implies, it is the primary. Sections also have two other headers that are directly related to the settings for having Different first page, and Different Odd/Even.

These are checked (default is unchecked) under Page Setup.

However, regardless of whether you check or uncheck Different first page, or Different Odd/Even, the header objects:

wdHeaderFooterFirstPage, and
wdHeaderFooterEvenPage

exist. If they are not set TO something, they = wdHeaderFooterPrimary.

So say you do nothing except enter some text for a header, say "This is a header."

wdHeaderFooterPrimary = "This is a header."
wdHeaderFooterFirstPage = "This is a header."
wdHeaderFooterEvenPage = "This is a header."

Each page header will shows "This is a header."

You go into Page Setup, and check Different first page, then go to the first page header and type in "First Page".

wdHeaderFooterPrimary = "This is a header."
wdHeaderFooterFirstPage = "First Page."
wdHeaderFooterEvenPage = "This is a header."

The first page will show "First Page", all the other pages will show "This is a header."

You go back to Page Setup and uncheck Different first page. All the page headers will go back to showing "This is a header.".......BUT

wdHeaderFooterPrimary = "This is a header."
wdHeaderFooterFirstPage = "First Page."
wdHeaderFooterEvenPage = "This is a header."

In other words, once changed the other header object text remains. It persists.

Now here is the another weird part. wdHeaderFooterPrimary can be set from any page in a section. BUT if you EVER check Different first page, or Different Odd/Even, then Primary becomes the text for either "all other pages", or "odd" pages. There is NO header for Odd Pages - just Primary acting in a "different" role. The kicker is, when this occurs, even though you can write Primary from any page (odd or even), once changed from acting for every page, it can ONLY be changed from an odd page number.

The whole thing is a bit confusing for most. It is this that screws up headers when you have different sections, and different header structure between sections.

That is why when ever I make a new section (other than continuous ones) I always make three blank extra pages. I explicitly change each one to NOT Same as previous (the default), and explicitly make sure the headers for each page are correct. Then I delete the blank pages.

This is because the text of the headers (Primary, FirstPage and EvenPage) are child objects of the Section. Even though the pages are deleted, even though I may uncheck Different first page, Different Odd/Even, the value persists in the Section break. So if I add pages into the Section - a likely occurence - the headers will automatically come back.

There, my rant for a Friday afternoon. I am going home to have a nap...

Gerry
 
Thanks again, hope you had a good nap. You cleared some things up !!!!!!!

Bart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top