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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Bookmarks in Footer 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,486
5
38
US
I have this Word document that I create from VB6 application (where I have a reference to Microsoft Word 16.0 Object Library). I have several Bookmarks in the body of the text that I populate with the data from my data base. And all of that works OK.

I can Delete all Bookmarks just fine with:

Code:
Dim bmk As Word.Bookmark

Set ObjWord = CreateObject("Word.Application")

With ObjWord
    ...
    For Each bmk In .ActiveDocument.Bookmarks
        bmk.Delete
    Next bmk
    ...
End With

And that Deletes Bookmarks from the body and the footer.

I can populate any Bookmark in the body of the text just fine with:

Code:
With objW.Selection
    .GoTo What:=wdGoToBookmark, Name:="MyBookmark"
    .TypeText Text:="My Text"
End With

Now I want to populate 2 Bookmarks in the Footer of this document, but all of my attempts fail :-(

How do you place a text in the Bookmark in the Footer of the Word document using VBA?


---- Andy

There is a great need for a sarcasm font.
 
Hi Andy,

This is really weird. The macro recorder simply uses this kind of GoTo. But it will not work on its own from within VBA.
The only way I found to access a bookmark (select in this case) in a footer is like this:
Code:
ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks("myBookmark").Select

This way you can manipulate it, also directly without using select.

Best,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Yes, it is weird.
You can record a macro, and then when you run it, it crashes.

Your code [highlight #FCE94F]gave me an error[/highlight]: "The requested member of the collection does not exist" even tho I do have two bookmarks: [blue]F_PHONE[/blue] and [blue]F_EMAIL[/blue] that I can find with my code [ponder]

Code:
For Each bmk In .ActiveDocument.Bookmarks
    Select Case bmk.Name
        Case "[blue]F_PHONE[/blue]"
            [highlight #FCE94F].ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks("[blue]F_PHONE[/blue]").Select
[/highlight]        Case "[blue]F_EMAIL[/blue]"
            [highlight #FCE94F].ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks("[blue]F_EMAIL[/blue]").Select[/highlight]
    End Select
Next bmk



---- Andy

There is a great need for a sarcasm font.
 
Have you set a different HeaderFooter for the first page? In this case, the wdPrimaryFooterStory might be the wrong StoryRange. [ponder]

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Does something like the following not work, then?

.ActiveDocument.Bookmarks("F_PHONE").Range.Text = "spoon
 
It is just one page document.
strongm's code did the trick. Thank you [wavey3]


---- Andy

There is a great need for a sarcasm font.
 
Your code for populating bookmarks:
Code:
With objW.Selection
    .GoTo What:=wdGoToBookmark, Name:="MyBookmark"
    .TypeText Text:="My Text"
End With
is overly complicated and can be reduced to:
Code:
objW.Bookmarks("MyBookmark").Range.Text = "My Text"
You should note, though, that deleting the bookmark after you've written to it this way won't delete the text. Similarly, updating the bookmark with new text will simply add that to the text that's already there. To be able to edit the bookmark's content, you need to use code like:
Code:
Sub UpdateBookmark(wdDoc as Object, StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With wdDoc
  If .Bookmarks.Exists(StrBkMk) Then
    Set BkMkRng = .Bookmarks(StrBkMk).Range
    BkMkRng.Text = StrTxt
    .Bookmarks.Add StrBkMk, BkMkRng
  End If
End With
Set BkMkRng = Nothing
End Sub
which you'd call with code like:
Code:
Call UpdateBookmark(objW.ActiveDocument, "MyBookmark", "My Text")



Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks macropod.
The "overly complicated" code is what Word's macro gives you when you record it to show you how to access a bookmark. So that's what I used (lazy, or.... easy way out :) ) But, now that I know how to do it better, I've modified my code and have 'one-liners' to do it - thanks to strongm.

I know "that deleting the bookmark after you've written to it this way won't delete the text", that's what I was counting on. The deal is, this is a one page document (I use a Word's template to do it, well, my template), but my user wants me to build several documents in one file, i.e. several pages of the same document. That's why, after I fill the data in first 'go-around', I delete all bookmarks, add a new template (with bookmarks) and populate the bookmarks again. Works just fine, and the user is happy. The number of 'pages' varies, depending on the number of records in the data base.


---- Andy

There is a great need for a sarcasm font.
 
In that case, the approach I'd take would be to repeatedly create new documents from the template, populate the bookmarks in each new document, then combine them all at the end. That way, there's no need to delete any bookmarks, only one set of which will remain in the final document.

Better still would be to use a mailmerge...

Cheers
Paul Edstein
[MS MVP - Word]
 
use a mailmerge" - good idea, but...
User do not have (direct) access to the data base (Oracle). Plus the requirements of the context of the document are way, waaaay too complicated and include - among many others - building an 'on-the-fly' link to the ftp server.


---- Andy

There is a great need for a sarcasm font.
 
One doesn't need access to the Oracle DB for a mailmerge - the same data source you're using to compile to documents would likely suffice. As for "building an 'on-the-fly' link to the ftp server", provided the data for that are present in the data source, that should be doable with a mailmerge.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top