PDA

Click to See Complete Forum and Search --> : [RESOLVED] Programmatically Updating Footer In Word Document


Hack
Mar 22nd, 2006, 08:13 AM
I have a Word document that will be utilized as a Template.

I have the following place holders as a Footer:

Originator:
Use Cases For:

What I would like to do is prompt the user for his/name and, once entered, update the footer information so that after entering the name, the footer would read:

Originator: Hack

I would like to do the same thing for the second line as well.

I guess my first question would be is this even possible?

Webtest
Mar 22nd, 2006, 09:34 AM
Hack:

I don't claim to be a Pro in Word, but here is something I recorded and then munged with. It seems to work ... except that it doesn't close the Header/Footer view. You can figure that out. Just do whatever you need to do to set up the 2 tStr variables that get inserted into the Footer.Sub Macro3()
Dim tStr1 As String
Dim tStr2 As String
'
tStr1 = "Hack"
tStr2 = "This is a TEST"

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Originator:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" " & tStr1
With Selection.Find
.Text = "Use Cases For:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" " & tStr2
End Sub
I hope this helps!

DKenny
Mar 22nd, 2006, 09:48 AM
Hack
Basically you will need a variable in the document that can contain each of these values. The easiest way I have found to do this in the past is to use the "Custom Document Properties" feature in Word.
Goto File-->>Properties and select the custom tab. Here you can add any properties you need, e.g. Originator. The in your _Open event, you can check to see if that property has a value other than a single space (it has to have some value, space is a good default) and prompt for one if its missing.


Private Sub Document_Open()
Dim MyProp As DocumentProperty

For Each MyProp In ThisDocument.CustomDocumentProperties
With MyProp
If .Name = "Originator" Then
If .Value = " " Then
'get your value here,
'updating the value property
' and update header/footer
End If
End If
End With
Next MyProp
End Sub

Hack
Mar 22nd, 2006, 11:33 AM
Two questions:

Once I get a value for Originator, how do I tell it to update the Footer with that value?

Does Word VBA support the InputBox?

Hack
Mar 22nd, 2006, 11:42 AM
Two questions:

Once I get a value for Originator, how do I tell it to update the Footer with that value?

Does Word VBA support the InputBox?Strike the InputBox question. I have that working and I have a name stored in a string.

I just don't know what to do with it in order to make it show up in the footer as:

Originator: Name_I_Typed

RobDog888
Mar 22nd, 2006, 12:26 PM
Webtest has posted the code necessary for accessing the header or footer. ;)

zaza
Mar 22nd, 2006, 12:32 PM
Incidentally, you can use document variables to store anything you need - just like a variable but stored in the document.

ActiveDocument.Variables.Add Name:="Value1", Value:="1"
MsgBox ActiveDocument.Variables("Value1")


I find them much easier to use than custom properties. Also, you can put them straight into a document using the DOCVARIABLE field. So I'd just insert a DOCVARIABLE field into your footer and then prompt the user for their name (or copy the string) into that document variable, then just update the fields. You could easily do the same for the second case too.


zaza

Hack
Mar 22nd, 2006, 12:52 PM
Webtest has posted the code necessary for accessing the header or footer. ;):blush: So he has, and it works.

Ok...final question...how do I automatically close the Header/Footer view?

RobDog888
Mar 22nd, 2006, 12:54 PM
Something like so.
ActiveWindow.ActivePane.View.Type = wdPrintView

Webtest
Mar 22nd, 2006, 12:55 PM
WUZZAMATTA Hack ... you don't like the code I dished up especially for you?

Webtest
Mar 22nd, 2006, 12:56 PM
Record a macro while you close the Header/Footer view. I did nearly the whole code in my post in Record Macro! (That's why it looks so kludgy!)

Edit: RobDog made it too easy for you!

Thanks, RobDog!

RobDog888
Mar 22nd, 2006, 12:58 PM
And I plagerized it in my previous post :D

Hack
Mar 22nd, 2006, 01:09 PM
WUZZAMATTA Hack ... you don't like the code I dished up especially for you?I love your code, am using it. :thumb:

Hack
Mar 22nd, 2006, 01:11 PM
Edit: RobDog made it too easy for you!I'm sure I could have figured it out, but after all this time I felt that it was my turn to play noob. :D

It actually felt kinda good. http://www.vbforums.com/attachment.php?attachmentid=38370&stc=1

RobDog888
Mar 22nd, 2006, 04:58 PM
Glad I'm not the only one that gets to feel like a noob. Currently being a noob at ASP.NET :D

Ps Hack - 22,222 posts :)