Solved

Dynamic footer depending on entry in main document

Posted on 2010-08-23
4
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
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 video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

724 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