Bookmarks in header

Z

zamdrist

Having troubles changing my Selection to a bookmark in a header. Here's
what I tried:

With objDocument
If .Bookmarks.Exists("Applicant1") = True Then
.ActiveWindow.View.Type = wdPrintView
.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
.ActiveWindow.ActivePane.Selection.GoTo wdGoToBookmark, , ,
"Applicant1"
.ActiveWindow.ActivePane.Selection.InsertAfter txtApplicant.Value
End If
End With

Although the bookmark exists, I still get an error that it does not,
presumably not in the 'view' it's looking for it in. I've stepped
though the code and watched the document's view change to show the
header, but once it tries to change the selection to the bookmark found
in the header, the header closes...and therefore it cannot find the
bookmark.

Ideas? Thoughts? Ridicule? :)

Thanks!
 
J

Jay Freedman

Having troubles changing my Selection to a bookmark in a header. Here's
what I tried:

With objDocument
If .Bookmarks.Exists("Applicant1") = True Then
.ActiveWindow.View.Type = wdPrintView
.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
.ActiveWindow.ActivePane.Selection.GoTo wdGoToBookmark, , ,
"Applicant1"
.ActiveWindow.ActivePane.Selection.InsertAfter txtApplicant.Value
End If
End With

Although the bookmark exists, I still get an error that it does not,
presumably not in the 'view' it's looking for it in. I've stepped
though the code and watched the document's view change to show the
header, but once it tries to change the selection to the bookmark found
in the header, the header closes...and therefore it cannot find the
bookmark.

Ideas? Thoughts? Ridicule? :)

Thanks!

The main idea is that when using VBA, selections and views and headers
don't mix very well. Instead, use a Range object and the Headers
collection, something like this:

Dim oRg As Range

With objDocument.Sections(1).Headers( _
wdHeaderFooterPrimary).Range
If .Bookmarks.Exists("Applicant1") Then
Set oRg = .Bookmarks("Applicant1").Range
oRg.Text = txtApplicant.Value
.Bookmarks.Add Name:="Applicant1", Range:=oRg
End If
End With

If the document has more than one section, or if the bookmark is in a
first-page or even-page header, you'd need to make some adjustments.

The reason for using .Bookmarks.Add to make sure the bookmark covers
the inserted text is explained at
http://www.word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
Z

zamdrist

Thank you TonyS & Jay...selecting the bookmark did the trick!

Have a great New Year! :)

Steve
 

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