Solved

Dynamic footer depending on entry in main document

Posted on 2010-08-23
4
435 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

810 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