Macro to remove header and footer, print, then add them back and save

I currently have a few Microsoft Word (2007) macros for printing to different trays. We have a new document format being rolled out soon and I'd like to enhance one of the macros to include the process:

1.

Remove Header AND Footer for page 1 ONLY (leaving other headers and footers intact)

2.

Print the document (to the tray locations specified by the macro)

3.

Add the page 1 Header AND Footer back as they were before the print

4.

Save the document

The current macros are stored in a "macro-enabled document template" (.dotm) which is located in the %appdata%>Microsoft>Word>Startup directory which means the macros are available for ANY document that loads in MS Word.

There are three ways (so far) that I can work out how to do this be recording macros and editing my existing print macros to suit. These are:

Open a blank document > cut/paste the header into it > cut/paste the footer into it > print > copy/paste the header back > copy/paste the footer back > save > close the blank document without saving.
The problem with this is that (for document switching) the macro refers to the document by name. This won't work if they open a document with a different name out of the system.

Delete the header and footer > print> add the header and footer from "Building Blocks" (or QuickParts) > save.
The problem with this is that the building blocks or quick parts for the header and footer must exist already.

Open the MS Office Clipboard Collector > copy the Header and then the Footer to the clipboard > print > Paste the Footer then the Header back into the correct locations > clear the Clipboard Collector and close it > Save the document.
The problem with this is if the document has more than one page and the other pages have different headers... it will copy/paste the header and footer for the page currently being viewed when the macro button is pressed.

Is there a way to attach "building blocks" or "quick parts" to my custom .dotm template? This would be the quickest fix I can see.

If not, is there a way to automate the creation of these "building blocks" in the macro so that if they don't exist when the document is opened, they will be created and used. Will this create an issue for future use of the macro if the building blocks already exist from previous use of the macro?

Or is there any other way to achieve what I want?

Thanks in advance
LVL 8
ReeceICT ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,
pls try

Sub PrintNoFirstHeaderOrFooter()

For Each stry In ActiveDocument.StoryRanges
    If stry.StoryType = wdFirstPageHeaderStory Or stry.StoryType = wdFirstPageFooterStory Then
        stry.Font.Hidden = True
    End If
Next
With ActiveDocument.PageSetup
    .FirstPageTray = wdPrinterUpperBin
    .OtherPagesTray = wdPrinterDefaultBin
End With
ActiveDocument.PrintOut
For Each stry In ActiveDocument.StoryRanges
    If stry.StoryType = wdFirstPageHeaderStory Or stry.StoryType = wdFirstPageFooterStory Then
        stry.Font.Hidden = False
    End If
Next
ActiveDocument.Save
End Sub

Open in new window

Regards

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rob HensonFinance AnalystCommented:
Another option, but don't know enough about VBA for word to put it into practice.

Save > Delete Header & Footer on Page 1 > Print > Close without saving.

Thanks
Rob H
Rgonzo1971Commented:
That would be
Sub PrintNoFirstHeaderOrFooterV1()
ActiveDocument.Save
For Each stry In ActiveDocument.StoryRanges
    If stry.StoryType = wdFirstPageHeaderStory Or stry.StoryType = wdFirstPageFooterStory Then
        stry.Delete
    End If
Next
With ActiveDocument.PageSetup
    .FirstPageTray = wdPrinterUpperBin
    .OtherPagesTray = wdPrinterDefaultBin
End With
ActiveDocument.PrintOut

End Sub

Open in new window

ReeceICT ConsultantAuthor Commented:
Thanks Rob
Unfortunately this isn't a viable option because of the possibility (albeit remote) that they make additional changes after printing and then save the document.  Most staff are trained to "ctrl+s" regularly to avoid document loss - so the chance that they'd save the document without the legal header and footer on the first page is too risky.

The only reason the header and footer are removed for the print job is because they have printed letterhead sitting in tray 2 of all the printers.

I've added my current macro (which is the third method I mentioned) to below in case there is a way to improve on it, but I will give Rgonzo's first suggestion a try and see how I get on.

Sub PrintNoElectronicLetterhead()
'
' Print NoElectronicLetterhead Macro
'
'
    WordBasic.EditOfficeClipboard
    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
    Selection.WholeStory
    Selection.Copy
    Selection.Delete Unit:=wdCharacter, Count:=1
    WordBasic.GoToFooter
    Selection.WholeStory
    Selection.Copy
    Selection.Delete Unit:=wdCharacter, Count:=1
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    With ActiveDocument.PageSetup
        .PageWidth = CentimetersToPoints(21)
        .PageHeight = CentimetersToPoints(29.7)
        .FirstPageTray = wdPrinterUpperBin
        .OtherPagesTray = wdPrinterMiddleBin
    End With
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
        False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
        PrintZoomPaperHeight:=0
    With ActiveDocument.PageSetup
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
    End With
    WordBasic.ViewFooterOnly
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeBackspace
    WordBasic.GoToHeader
    Selection.PasteAndFormat (wdPasteDefault)
    Selection.TypeBackspace
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    CommandBars("Office Clipboard").Visible = False
    ActiveDocument.Save
End Sub

Open in new window

Rob HensonFinance AnalystCommented:
With rgonzo's suggestion, if you add

ActiveDocument.Close Save:= False

 at the end, there wouldn't be the option to amend after Printing. Syntax may not be quite right.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.