PDA

View Full Version : Removing Headers & Footers



shudder
03-03-2014, 02:31 PM
Hi Guys,

I am trying to create a macro to copy the header & footer from one document to another for which I have the following code which works up to a point:


Sub Add_Letterhead()
'Apply a digital wrap to a document
Dim ThisDoc As String
Dim DocTemp As String
Dim txtBox As Shape
If Not ActiveDocument.ProtectionType = wdNoProtection Then
MsgBox "This document is protected, please unprotect the document and try again." & vbCr & vbCr & _
"NB: Invoices cannot be unprotected.", vbOKOnly + vbCritical, "Bluefin"
Exit Sub
End If
Application.ScreenUpdating = False
ThisDoc = ActiveDocument.Name
DocTemp = TemplateDoc
Documents(ThisDoc).Content.Copy
Documents.Open DocTemp
Selection.EndKey wdStory
Selection.PasteAndFormat wdPasteDefault
Documents(DocTemp).Content.Copy
Documents(ThisDoc).Content.Paste
Documents(DocTemp).Close SaveChanges:=wdDoNotSaveChanges
For Each txtBox In ActiveDocument.Shapes
On Error Resume Next
txtBox.Select
Selection.Font.Name = "Arial"
On Error GoTo 0
Next txtBox
Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = True
End Sub


My problem is when the document already has a header and footer present and therefore have to be deleted.

I have found these two macro's from other posts on this site:

Sub DeleteHeadFoot()
Dim oHF As HeaderFooter
Dim oSection As Section

For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
oHF.Range.Delete
Next
For Each oHF In oSection.Footers
oHF.Range.Delete
Next
Next

End Sub


Sub ClearHeaderFooter()
'
' ClearHeaderFooter Macro
' Macro recorded 10/27/2005 by JohnnyBravo
'
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, count:=1
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, count:=1
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub


However, I do not understand the context and why they both have to be called in order to delete the existing headers and footers. Can anybody offer some guidance?

Thanks in advance.

Stuart

fumei
03-03-2014, 05:26 PM
I have the following code which works up to a point:
What point is that?

You should need to use both DeleteHeadFoot and ClearHeaderFooter to delete the headers. Add_Letterhead does not seem to anything with headers at all. What is TemplateDoc?

shudder
03-04-2014, 08:11 AM
What point is that?

It works so long as there is no header or footer present in the active document.


You should need to use both DeleteHeadFoot and ClearHeaderFooter to delete the headers. Add_Letterhead does not seem to anything with headers at all. What is TemplateDoc?

The TemplateDoc is the source document for the header/footer which is declared in a public variable.

You are correct in that Add_Letterhead doesn't actually do anything with the headers/footers themselves. it simply copies the whole document (maybe I should have provided a different thread title).

I have fairly good VBA knowledge when it comes to Excel however I am totally unfamiliar with Word and the associated document objects.

Would there be a better way of copying the header/footers across?

macropod
03-04-2014, 02:35 PM
Since you appear to be replicating a document, why not simply make a copy of it in Windows, rather than through Word? Even in Word, it's much simpler than your approach; simply open the file the use SaveAs to rename it.

You only need to copy/paste header/footer content when you're working with the insertion of one document's content into another and the final Section of the document being copied does not include the terminating Section break (as inevitably happens with the final Section). Furthermore, in that scenario you have much more to consider than just the header/footer content; there are all the other aspects of the page layout that go with the Section being copied.

shudder
03-05-2014, 03:20 PM
I remember making these sorts of mistakes when I started learning VBA in Excel now!

So I'll take it from the top.

We’re finding more and more that we are having to print letters (headed paper in printers), sign them and then scan to ourselves in order to send via email. To be ‘compliant’ we need to save what we have sent to evidence if it was electronic or postal, having the company header and signature would satisfy this (we currently have to save the email).

The document that I wish to copy across to the 'template' is created from a software package related to my work. These are just letters, some pre-populated, some free format, some mixed. Once we have produced the letters we then have to save them back to the system (via an add-in). In order to save them back to the system correctly the file name (series of random numbers) and path (read only access available) have to be preserved. Some of these documents have the client number in the footer, hence me wanting to delete existing header&footers.

My 'TemplateDoc' is essentially just that, a template with the company logo (png) in the header, a couple of logo's (png) and a textbox in the footer. There is also another textbox on the top right hand side with the office address. There are no section breaks in this document.

From my reading of this and other sites it has led me down the path I have gone to copy back and forth between documents. If there is a more efficient or correct way to undertake this task please point me in the right direction.

I would also be interested in recommendations of some books to give me a better understanding of the Word VBA language and objects.

Many thanks

Stuart

macropod
03-05-2014, 06:49 PM
To be ‘compliant’ we need to save what we have sent to evidence if it was electronic or postal, having the company header and signature would satisfy this (we currently have to save the email).
...
Some of these documents have the client number in the footer, hence me wanting to delete existing header&footers.
I'd have thought deleting content, especially goes against the notion of evidence of compliance...

Nevertheless, you could use code like:

Sub Demo()
Dim wdDoc1 As Document, wdDoc2 As Document
Dim Sctn As Section, HdFt As HeaderFooter
Set wdDoc1 = Documents.Open("C:\Users\" & Environ("Username") & _
"\Documents\Header.docx", AddToRecentFiles:=False)
Set wdDoc2 = Documents.Open("C:\Users\" & Environ("Username") & _
"\Documents\DocumentToModify.docx", AddToRecentFiles:=False)
With wdDoc2
.PageSetup.DifferentFirstPageHeaderFooter = True
wdDoc1.Sections.First.Headers(wdHeaderFooterFirstPage).Range.Copy
.Sections.First.Headers(wdHeaderFooterFirstPage).Range.PasteAndFormat _
(wdFormatOriginalFormatting)
For Each Sctn In .Sections
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then .Range.Text = vbNullString
End With
Next
Next
wdDoc1.Sections.First.Footers(wdHeaderFooterFirstPage).Range.Copy
.Sections.First.Footers(wdHeaderFooterFirstPage).Range.PasteAndFormat _
(wdFormatOriginalFormatting)
.Save
End With
wdDoc1.Close SaveChanges:=False
Set wdDoc1 = Nothing: Set wdDoc2 = Nothing
End Sub
Note: As coded, the macro assumes the 'letterhead' page content exists in the First Page header & footer of the source document and is to go into only the First Page header & footer of the target document. If your target document doesn't already have a Different First Page layout, it should have - and the code ensures it has one. Other than replacing the First Page header, no other header changes are made. All other footer content is erased. Naturally, you'll need to change the file paths & names to suit your purposes - you might want to employ a FileOpen dialogue for the file to be modified.

snb
03-06-2014, 02:46 AM
To 'remove' a footer:


Sub M_snb()
ActiveDocument.StoryRanges(9).Text = ""
End Sub

To 'copy' a footer from 1 document to another:


Sub M_snb()
Documents(2).StoryRanges(9).Text = Documents(1).StoryRanges(9).Text
End Sub

macropod
03-06-2014, 03:14 AM
To 'remove' a footer:


Sub M_snb()
ActiveDocument.StoryRanges(9).Text = ""
End Sub

To 'copy' a footer from 1 document to another:


Sub M_snb()
Documents(2).StoryRanges(9).Text = Documents(1).StoryRanges(9).Text
End Sub

Neither of those comes close to working properly. The first one only deletes the text from only one of the three footers in a Section, the primary one. The second one inserts unformatted text - into the primary footer in every Section - and ignores things like graphics, textboxes, etc., which the OP's source document has. And, given that it's the first page header & footer in only the first Section that letterhead stationery typically uses, your macros target entirely the wrong ranges for updating.

snb
03-06-2014, 03:50 AM
Don't think in solutions but in suggestions how to achieve a goal.
The OP might be interested to explore what storyranges means, what different constants Word uses to indicate certain footers, headers, etc.
Since the OP didn't provide rather exact information on the content of the footer it's up to him/her to explore which method is preferable.

macropod
03-06-2014, 02:36 PM
Don't think in solutions but in suggestions how to achieve a goal.
And that's precisely what your code isn't.

The OP provided plenty of information for anyone who can read a post to know a solution that works with formatted data is required. Anyone who understand Word VBA can also see that what you've offered plainly doesn't work with that and is a giant leap backwards from what has already been offered.

Instead of trying to impress everyone with the brevity of your code, it would be helpful if it actually addressed the issues at hand...

shudder
03-10-2014, 04:28 PM
I'd have thought deleting content, especially goes against the notion of evidence of compliance...



I work for a large group and some of the letters are a 'one size fits all' solution. For our division all the letters are addressed to unique company names and as such we do not need the client numbers as an identifier..........unlike others who may have several A Smith's!

Thanks for providing the solution so far. However, the text box in the footer is coming across in Times New Roman font whereas in wdDoc1 (header template) the font is Arial. Can this be addressed more efficiently than looping through the textboxes?

Many thanks

macropod
03-10-2014, 07:55 PM
I would have thought:
.Sections.First.Footers(wdHeaderFooterFirstPage).Range.PasteAndFormat _
(wdFormatOriginalFormatting)
should preserve the font attributes. That said, you really should have the Style being used in the footer (probably the Footer Style) of both documents defined to use all of the required attributes. The same goes for the header.