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
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