×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Word - Bookmarks in Footer

Word - Bookmarks in Footer

Word - Bookmarks in Footer

(OP)
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 sad

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.

RE: Word - Bookmarks in Footer

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 --> VBA

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.

RE: Word - Bookmarks in Footer

(OP)
Yes, it is weird.
You can record a macro, and then when you run it, it crashes.

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

CODE

For Each bmk In .ActiveDocument.Bookmarks
    Select Case bmk.Name
        Case "F_PHONE"
            .ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks("F_PHONE").Select
        Case "F_EMAIL"
            .ActiveDocument.StoryRanges(wdPrimaryFooterStory).Bookmarks("F_EMAIL").Select
    End Select
Next bmk 


---- Andy

There is a great need for a sarcasm font.

RE: Word - Bookmarks in Footer

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.

RE: Word - Bookmarks in Footer

Does something like the following not work, then?

.ActiveDocument.Bookmarks("F_PHONE").Range.Text = "spoon"

RE: Word - Bookmarks in Footer

(OP)
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.

RE: Word - Bookmarks in Footer

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]

RE: Word - Bookmarks in Footer

(OP)
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 smile ) 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.

RE: Word - Bookmarks in Footer

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]

RE: Word - Bookmarks in Footer

(OP)
"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.

RE: Word - Bookmarks in Footer

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]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close