Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Macro to add headers and 'x of y' footers runs in step through, but not in a template

Status
Not open for further replies.

rb640

Technical User
Aug 29, 2012
2
US
This code will run in step through, but wont run in the template that i have it in. Code runs on the AutoNew. Purpose of the code is to create headers and footers in the document. Header will have the word "Confidential" footer will have the environment name, date, and x of y page numbers. Can anyone tell me why this code produces the following error : "object variable or With block variable not set" Thank you


Public Sub AutoNew()

'Force Save on Open, This allows me to get the right file path in the footer.
'Revised 08/28/12 RB

'*********************Begin Force Save
With Application.Dialogs(wdDialogFileSaveAs)
.Name = ""
.Format = wdFormatDocument
.Show

End With


'********************** Begin Header Formatting

MyHeadersText = "CONFIDENTIAL"

With ActiveDocument.Sections(1)

'BEGIN HEADER FORMATTING
.Headers(wdHeaderFooterPrimary).Range.Text = MyHeadersText
.Headers(wdHeaderFooterPrimary).Range.Font.Size = 12
.Headers(wdHeaderFooterPrimary).Range.Font.Color = wdColorRed
.Headers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphRight

End With



' 'BEGIN FOOTER FORMATTING

MyFootersText = ActiveDocument.Path & Application.PathSeparator & ActiveDocument.Name & Chr(10) & "ANALYST: " & UCase(Environ("UserName")) & Chr(9) & Format(Date, "MM/DD/YY") & Chr(9)

ActiveDocument.Sections(ActiveDocument.Sections.Count) _
.Footers(wdHeaderFooterPrimary).Range.Select
With Selection
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:=MyFootersText & "Page "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
End With

With ActiveWindow

ActiveWindow.ActivePane.Close
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

End With



End Sub
 
Since this is a template, wouldn't it be easier to just have the code in the Header & Footer and set-up the template to be read-only? The only item that you'd need is to have the template update the codes for the location of the file, dates, and number of pages.
 
Since this is a template, wouldn't it be easier to just have the code in the Header & Footer and set-up the template to be read-only? The only item that you'd need is to have the template update the codes for the location of the file, dates, and number of pages.
 
What do you mean by put the code in the header & footer? I am not aware of a way to do this. I do it all the time in excel but I have never seen it done in Word. I also need to populate the Username and Date, thats the reason that i put it on the AutoNew Sub. Thank you for responding.
 
There's 2 ways of getting to the Header/Footer.
1. Just double click in the Header or Footer area
2. Under the Insert Ribbon, Clikc the Header or Footer tab.

Also, under the Insert Ribbon, there's items like Quick Parts that will allow you to insert codes (e.g., Author & Fields) that will do moast of the items you wish to include.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top