Solved

Dynamic footer depending on entry in main document

Posted on 2010-08-23
4
436 Views
Last Modified: 2012-08-13
I'm trying to get a quote templace to show the quote number in the footer.

Most viable way seemed to use a textbox, in the text bpdy and use the textbox/change event to trigger the VBA to rewrite the footer accordingly.

It works perfectly except for one nast detail: when the used taps 2-3 character quickly, the textbox gets deleted. Odd, but it happens on both tested computers.

Looking for a fix or alternate solutions than a text box.

Attached a sample document.
The code is below:

rivate Sub TextBox1_Change()
Dim oRge As Range

ActiveDocument.Sections.Item(ActiveDocument.Sections.Count).Footers(wdHeaderFooterPrimary).Range.Text = ""
Set oRge = EndOfLastFooter()
oRge.Text = "Page "
oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldPage
Set oRge = EndOfLastFooter()
oRge.Text = " of "
Set oRge = EndOfLastFooter()
oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldNumPages
Set oRge = EndOfLastFooter()
oRge.Text = vbTab & TextBox1.Value
Set oRge = EndOfLastFooter()
oRge.Collapse wdCollapseEnd
oRge.Text = vbTab & "   SP/01/03/175/I"

With ActiveDocument.Sections
  Set oRge = .Item(.Count).Footers(wdHeaderFooterPrimary).Range
End With

oRge.Fields.Update
Set oRge = Nothing

End Sub

Private Function EndOfLastFooter() As Range
  Dim oRg As Range
  With ActiveDocument.Sections
    Set oRg = .Item(.Count).Footers(wdHeaderFooterPrimary).Range
  End With
  oRg.Collapse wdCollapseEnd
  Set EndOfLastFooter = oRg
  Set oRg = Nothing
End Function


test.doc
0
Comment
Question by:calacuccia
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 33502319
Yes, I get the same error in Word 2007.

I have tried using the LostFocus event, but that causes Word to Hang in both 2003 and in 2007.

You might be able to use a text input form field in a protected section, and a REF field in the footer, but the restrictions with forms protection might not be acceptable.

Or you could have use a Userform to capture the data and to insert it in the places that you need it.

Incidentally, the Sections collection has a Last method.

  Set oRg = ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).Range

0
 
LVL 17

Author Comment

by:calacuccia
ID: 33508705
Hi Graham,

I certainly find this behaviour very unexpected, for a control to disappear like that. Once again, I loose Word confidence :-)

Formfields is no option: this document is basically edittable from Start to End for some exception, it would become a burden if formfiels had to be used all over this document.

I hope I can prevent to have to use a UserForm, but that would probably work.

Why do you mention the Last method? I did not see where this could help.
0
 
LVL 17

Author Comment

by:calacuccia
ID: 33563344
One last try to bring this to somebody's attention (also known as bump)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question