PDA

View Full Version : BOOKMARKS IN FOOTERS



Greg
11-08-2007, 05:11 PM
Hi all,

Is it possible to insert a bookmark into a footer for use with a userform?

I am used to putting bookmarks into a document with ref fields in the footer but my attempts at putting the bookmark directly into the footer itself have been unsuccessful.

Regards,

Greg.

TonyJollans
11-09-2007, 02:34 AM
Yes, it's possible.

What have you tried and what has happened?

fumei
11-09-2007, 02:37 PM
Greg....we don't know what "unsuccessful" means.

You can put a bookmark anywhere, including footers.

You do not even tell us if you are trying to do this by code, or actually going into the footer and trying to make a bookmark.

I suspect - but of course do not know, as you are not really telling us anything - that you are having issue with Range.

The Add method of bookmarks require a range. Note the index of the footer is 1, as I was too lazy to write wdHeaderFooterPrimary:ActiveDocument.Sections(1).Footers(1) _
.Range.Bookmarks.Add Name:="FooterCrap", _
Range:=ActiveDocument.Sections(1).Footers(1).RangeNote that I had to actually specify the range, AND that the specified range was in fact in the footer intended.
ActiveDocument.Sections(1).Footers(1) _
.Range.Bookmarks.Add Name:="FooterCrap", _
Range:=Selection.RangeThis may be using the footer range to Add a bookmark...but who cares, as the actual range bookmarked will be the Selection. No matter where it is.

Greg
11-12-2007, 04:32 PM
Hi Tony and Gerry,

My hope was to fill a bookmark in the footer via a textbox in a Userform. However, upon executing the OK command, the bookmark in the footer immediately relocates itself into the main document.

Greg.

TonyJollans
11-13-2007, 04:31 AM
Can you post the code?

fumei
11-13-2007, 01:19 PM
Yes please, post your code, because:
However, upon executing the OK command, the bookmark in the footer immediately relocates itself into the main document.that does not happen for me. I can put content into a bookmark, in a footer, from a userform, no problem.

"Relocate" sounds fishy to me.

Greg
11-13-2007, 04:22 PM
Here's the document.

Open the Userform and insert something into the textbox "Our Reference" then press OK.

For me, the bookmark in the footer relocates itself into the main body of the document, usually towards the bottom of the first page.

I hope you can assist.

Many thanks.

Greg.

lucas
11-13-2007, 05:18 PM
It put it in the footer of the first page ok for me. Right after the word Ref:

didn't jump out of the footer.??

Greg
11-13-2007, 05:52 PM
Does it work on successive occassions?

It certainly doesn't work for me!

fumei
11-14-2007, 12:22 PM
Greg, I too am getting this. Very odd. I do not have time right now to work on it, but try stepping through it.

Yes, it does put the Reference text content into the footer bookmark...BUT, for some reason, it is also removing that bookmark and putting it into the body. So now the REF field in the next footer points to the one in the body.

If you refire the userform, it now gets the - in error - content of the bookmark from the body.

So yes, there is something screwy going on.

Wait a second.....it is because the FillABookmark procedure is using Selection, and of course the Selection is NOT in the footer.

Give me a few minutes....

fumei
11-14-2007, 12:47 PM
Yup, yup. That is the problem. You are using an old old old version of my FillABookmark.

Sub FillABookmark(strBM_Name As String, strBM_Text As String)
Dim ThisDoc As Word.Document
Set ThisDoc = ActiveDocument
' following is for formfields, if no formfields can remove
If ThisDoc.ProtectionType = wdAllowOnlyFormFields Then
ThisDoc.Unprotect
End If
On Error Resume Next
With Selection
.GoTo what:=wdGoToBookmark, Name:=strBM_Name
.Collapse Direction:=wdCollapseEnd
ThisDoc.Bookmarks(strBM_Name).Range.Text = strBM_Text
.MoveEnd unit:=wdCharacter, Count:=Len(strBM_Text)
ThisDoc.Bookmarks.Add Name:=strBM_Name, Range:=Selection.Range
.Collapse Direction:=wdCollapseEnd
End With
' to reset formfields
If ThisDoc.FormFields.Count > 0 Then
ThisDoc.Protect wdAllowOnlyFormFields, Password:=""
End If
Set ThisDoc = Nothing
End SubNotice it used Selection. The newer one uses Range. The old version also had stuff for dealing with formfields. As you are not using formfields, why did you not remove that?

In any case, the new version bypasses that issue, by using range.

Sub FillABookmark(strBM_Name As String, strBM_Text As String)
Dim oRange As Word.Range
Set oRange = ActiveDocument.Bookmarks(strBM_Name).Range
ActiveDocument.Bookmarks(strBM_Name).Range.Text = strBM_Text
With oRange
.Collapse Direction:=wdCollapseEnd
.MoveEnd Unit:=wdCharacter, Count:=Len(strBM_Text)
End With
ActiveDocument.Bookmarks.Add strBM_Name, Range:=oRange
End Sub

Updated file attached. I also added a procedure to explicitly update the REF field in the footer on page 2. Did you notice is was not updating?

Greg
11-14-2007, 07:59 PM
That's terrific Gerry. Thanks very much for your help. I'll use that newer code on all my documents now.

Greg.

fumei
11-15-2007, 07:54 AM
Things move forward....sometimes. Just remember, Word is different.

fumei
11-15-2007, 07:55 AM
Greg, BTW, I would recommend, rather than putting your useful chunks of code in every document/template file, you shoukld start collecting them into a global template.

They are the best place to put on-going repeatable code.

Greg
11-15-2007, 04:31 PM
Thanks Gerry, that's good advice.

Can I assume that you would put the module into a global template and apply it using templates and add-ins?

BTW, I notice that the procedure runs much faster now. The only slowness is in loading the document in the first place. Is there a way to make that faster.

The time required to load the menu bar with the "Data Form" button on it seems to be the main cause of the slowness. Is this correct or is there some other factor invloved?

Regards,

Greg.

fumei
11-16-2007, 07:45 AM
"Can I assume that you would put the module into a global template and apply it using templates and add-ins?"

Any code in a global template is available...globally. If it is loaded of course. If it is loaded, you can simply call the procedure from anywhere.

Globals can be loaded automatically (on Startup), or dynamically (either manually: Tools > Templates and addins, or by code).

I am home right now - I had downloaded your file at work. I do not feel like downloading it again. I do not see why )off hand) why a change to the toolbar would cause any significant slowdown.

Is it really slow? Have you tested to see if there is a slowness difference if you remove the button and open the file? In other words, can you pin-point any slowness to that specifically?