PDA

View Full Version : Different header error when less than 2 pages



dro^
07-24-2012, 06:51 AM
Morning Folks,
Nice looking forums lurked around a bit but couldn't find my solution. I am currently creating a macro to drop in the header and footer from 3 files ( header file, footer1, and footer2 ) just because it makes it easier to edit the text on the server once.

The problem: if there is only one page then Active window does not see "wdSeekPrimaryFooter" returns an error and stops the macro. I need the macro to either possibly activate the field so that when a 2nd page does become present the 2nd footer is properly displayed or some other means I know there is a million different ways to do things with code. Any help with this or any tips on how this is written or could be improved I greatly appreciate trying to get a hang of this. -dro^
Error code: :beerchug:

'2nd page footer "CurrentPageFooter"
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False




Full code:



Sub LHMacroTemp()



'Sets multiheader enviroment

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True

End With








'2nd page footer "CurrentPageFooter"
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False


'Set FirstPageFooter
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

'Set FirstPageHeader
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If



End Sub

fumei
07-24-2012, 05:14 PM
Just to explain...

Your code - if it is a one page document - makes that first page DifferentFirstPage. There is NO second page, therefore: '2nd page footer "CurrentPageFooter"
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooterdoes indeed error out. Part of the issue is that you are using SeekView.


It is farfar better to action ANY action with headers and footer using ranges, not Selection. However, the code is trying to view Primary...and there is no Primary there to view. Thus, an error.

So, either add another page so there IS a secondf page, OR action the Primary header directly. You can do that with code even though the page does not exist yet.
Sub LHMacroTemp()
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
End With
'2nd page footer "CurrentPageFooter"
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

'Set FirstPageFooter
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

'Set FirstPageHeader
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
' center it
.ParagraphFormat.Alignment = wdAlignParagraphCenter
' insert file
.InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
End With
End SubThis sets the Primary header and footer even though the page does not exist yet. The moment a second page is made, the header and footer will be there automatically.

dro^
07-24-2012, 05:26 PM
Just to explain...

Your code - if it is a one page document - makes that first page DifferentFirstPage. There is NO second page, therefore: '2nd page footer "CurrentPageFooter"
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooterdoes indeed error out. Part of the issue is that you are using SeekView.


It is farfar better to action ANY action with headers and footer using ranges, not Selection. However, the code is trying to view Primary...and there is no Primary there to view. Thus, an error.

So, either add another page so there IS a second page, OR action the Primary header directly. You can do that with code even though the page does not exist yet.

I am sorry I am noob still, can you please show me an example of code to action the headers directly. Learning curve is steep with VB. Thank you for your help and response they kinda told me hey figure this out and left me in the wind at work. -_-

fumei
07-24-2012, 05:32 PM
Sub LHMacroTemp()
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
End With
'2nd page footer "CurrentPageFooter"
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

'Set FirstPageFooter
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

'Set FirstPageHeader
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
' center it
.ParagraphFormat.Alignment = wdAlignParagraphCenter
' insert file
.InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
End With
End SubEven though there is no second page yet, its header and footer are set. When a second page is made, the header and footer will be there automatically.


BTW, I find it a little odd that you ae inserting a .DOTM file into a header.

dro^
07-25-2012, 05:46 AM
Sub LHMacroTemp()
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
End With
'2nd page footer "CurrentPageFooter"
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

'Set FirstPageFooter
ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

'Set FirstPageHeader
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
' center it
.ParagraphFormat.Alignment = wdAlignParagraphCenter
' insert file
.InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
End With
End SubEven though there is no second page yet, its header and footer are set. When a second page is made, the header and footer will be there automatically.


BTW, I find it a little odd that you are inserting a .DOTM file into a header.


With a little swap of .footer and .header everything worked perfectly. :rotlaugh::rofl::bow:

Well honestly I had no idea what I was doing so I was trying to figure out how word exports its macros and was creating all sorts of files in the beginning. Now I have one dotm file with the code inside a module which will be pushed with a login script to the word startup folder. I mean I was going crazy trying to figure out the linking using macro recorder. I have a vb book I am reading now but by the time I finish reading that I think they would have pushed this project to the back burner. I really thank you for the help this is exactly what I was looking for. Recommend any tutorials?
:beerchug: