If bookmark exists within footer

K

Kenny Bones

Hi, I just ran into a bit of a problem here.
I've got a document template which contains alot of bookmarks, some of them
are in the footer of the template.

Now, I found during testing that I cannot just GoTo Bookmark if the bookmark
is within a footer. It just doesn't go there, but the code continues to the
next bookmark in the main document and inserts the value which the bookmark
in the footer was supposed to get.

So, how can I write an If statement asking if the bookmark lies within a
footer, then insert, if not, then go out of footer and continue on the main
document?
 
G

Greg Maxey

Without seeing your code is is hard to predict just what you are after.
Something like this perhaps:

Sub ScratchMacro()
Dim oFtr As HeaderFooter
Dim oSec As Section
For Each oSec In ActiveDocument.Sections
For Each oFtr In oSec.Footers
If ActiveDocument.Bookmarks("TestBookmark").Range.InRange(oFtr.Range)
Then
MsgBox "Bookmark is in range."
End If
Next oFtr
Next oSec
End Sub
 
K

Kenny Bones

Hi, mm, this may be a little complex really.
I'm not supersteady at this, and cannot wrap my head around the whole code
as a whole.

You see, the code consists of an array which gets values from the registry.
And then puts the values inside the bookmarks, if they exist.
But now, when I have to concider the fact that some bookmarks may be in the
footer, then the code changes quite a bit. And I want the code to be as
efficient as possible as well, especially since you can see what the macro
does if it does alot of stuff.

Anyway, here is the code after it's gotten values from the array:

Dim sBookMarkName, sValue, Fields
Fields = Array(regString1, regString2, regString3, regString4)
sBookMarkNametemp = Fields(iTeller)
sValue = ""
sValue = objShell.RegRead(Fields(iTeller)
sBookMarkName = ""
sBookMarkName = ActiveDocument.Bookmakrs(sBookMarkNametemp).Name

If ActiveDocument.Bookmarks.Exist(sBookMarkNametemp) Then
Selection.GoTo What:=wdGoToBookmark, Name:=sBookMarkName
Selection.Delete Unit:=wdCharacter, Cound:=0
Selection.InsertAfter sValue
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=sBookMarkName
End if


Could you help me incorporate the macro code you just wrote into this?
I would be enourmously greatful! :)
 
C

Cindy M.

Hi Kenny,
You see, the code consists of an array which gets values from the registry.
And then puts the values inside the bookmarks, if they exist.
But now, when I have to concider the fact that some bookmarks may be in the
footer, then the code changes quite a bit. And I want the code to be as
efficient as possible as wel

In that case, avoid using Selection. You can write directly to the RANGE of
any bookmark, no matter if in the document body, the footer, etc. by
addressing the Range. You only run into trouble if you try to select it. So
try something more like this:

If ActiveDocument.Bookmarks.Exist(sBookMarkNametemp) Then
Dim bkm as Word.Bookmark, Dim bkmRange as Word.Range
Set bkm = ActiveDocument.Bookmarks(sBookMarkName)
Set bkmRange = bkm.Range
bkmRange.InsertAfter sValue
Set bkm = ActiveDocument.Bookmarks.Add( _
Range:=bkmRange, Name:=sBookMarkName)
End if



Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

Gordon Bentley-Mix

I do this often enough that I've written a "generic" procedure that's now
part of my standard toolset in almost all of my templates. It looks like
this:

Public Sub InsertBookmarkValue(BkmkName As String, ByVal InsertValue As
String)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
myRange.Text = InsertValue
.Bookmarks.Add BkmkName, myRange
End If
End With
End Sub

This procedure accepts arguments for the Bookmark name and the value to be
inserted into the Bookmark. It also restores the Bookmark after inserting
the value so it's available if the code is rerun. This line could be deleted
if your macro is "run once".
--
Cheers!

Gordon Bentley-Mix
Word MVP

UNSOLICITED EMAIL CONTACT WILL BE MARKED AS SPAM AND DELETED! Please post
all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
K

Kenny Bones

Dear God! This is perfect! Thank you SOO much!
The macro even runs better now, I don't need to watch the code select the
bookmarks either, so the overall experience is much smoother :)
 

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