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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Header issues using VBA to insert Word documents into a Word Template

Status
Not open for further replies.

PMTom

IS-IT--Management
Mar 7, 2017
8
0
0
US
I have the following VBA code in a Word Template to insert a document at the cursor position when someone selects a button on the Word Ribbon (In this case it would be a document named 04-10A.docx). The code does this with no issues. The inserted document has a header and footer. The error occurs when the next document is inserted. The Header and Footer from the previous document is added to the 2nd document when it should not be. I thought this would not happen by using the SectionBreakNextPage but this is not working. Any ideas on how to fix this issue?

Public Sub 04_10A()
'
' Inserts at cursor location
Dim currentPosition As Range
Set currentPosition = Selection.Range 'pick up current cursor position
Selection.InsertBreak Type:=wdSectionBreakNextPage
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="FormInsert"
End With
'
Dim baseDoc As Document
Dim sfile As String
Set baseDoc = ActiveDocument
Set sdoc = Application.Documents.Open("X:\WorkTemplate\Forms\04-10A.docx")
Selection.WholeStory
Selection.Copy
ActiveWindow.Close savechanges:=wdDoNotSaveChanges
baseDoc.Activate
Set BMRange = ActiveDocument.Bookmarks("FormInsert").Range
BMRange.Application.Selection.PasteAndFormat (wdFormatOriginalFormatting)
'
End Sub


 
When you Selection.Wholestory you get the whole story. Headers and Footers too.

If you don't want them, don't select them.

And there is no need to actually open sdoc.

Just .InsertFile it.
 
The documents that get inserted have different headers so I do want them. I don't want the header from the previous inserted document to get added to the new inserted document that may have its own header or may not have one at all. They are all different.
 
The documents that get inserted have different headers so I do want them. I don't want the header from the previous inserted document to get added to the new inserted document that may have its own header or may not have one at all. They are all different.
For a source document with a single Section:
Code:
Dim RngSel As Range, DocSrc As Document
Selection.InsertBreak Type:=wdSectionBreakNextPage
Set RngSel = Selection.Range 'pick up current cursor position
Set DocSrc = Documents.Open("X:\WorkTemplate\Forms\04-10A.docx")
RngSel.FormattedText = DocSrc.Range.FormattedText
DocSrc.Close savechanges:=wdDoNotSaveChanges
Set DocSrc = Nothing: Set RngSel = Nothing

For a multi-Section source document, you need to loop through each Section in the source document and replicate all except the Section break in the target document (you may need to add new Section breaks there, with the same configuration), using the FormattedText
method as demonstrated above. The alternative is to fix the headers & footers afterwards.

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks Paul. This seems to work except it is adding the header of the base document to the document that gets inserted. What do I need to do to keep this from happening?
 
This seems to work except it is adding the header of the base document to the document that gets inserted. What do I need to do to keep this from happening?
It does not and cannot do anything of the kind. No changes are made to the source document (i.e. the document that gets inserted) and, even if they were, it is closed without saving.

Cheers
Paul Edstein
[MS MVP - Word]
 
Maybe I'm not explaining this clearly. I have a Word template(dotm) that users insert a Word document (docx) into that has headers. Once this first document is inserted the users place their cursor where they want the next document inserted then pick from several other Word documents to insert based on their needs that may or may not have headers by selecting a ribbon button that runs the code listed above. The issue is the header of the first document that was inserted carriers over onto the next sheet/document that gets inserted.
 
That is to be expected. For what you want, not only do you need to insert a Section break (as the code already does) but also you need to unlink the new Section's headers & footers (3 of each) from the previous Section - and delete the now-unlinked new Section's headers & footers (3 of each) - before inserting the content from the new document. Your previous posts said nothing of what was to happen in that regard except that you didn't want the inserted documents' header & footers being carried over. Mind you, there's far more than just headers & footers you should be considering when you're doing this; there's also page layouts (e.g. landscape vs portrait & margins), tables of contents, indexes, bookmarks, cross-references, footnotes, endnotes, etc., etc., that may be present in the source documents, any and all of which can upset your nice little applecart.

Cheers
Paul Edstein
[MS MVP - Word]
 
So the magic question is how do I unlink and delete headers/footers using vba?
 
Try:

Code:
Dim RngSel As Range, DocSrc As Document, HdFt As HeaderFooter
Selection.InsertBreak Type:=wdSectionBreakNextPage
Set RngSel = Selection.Range 'pick up current cursor position
With RngSel.Sections.First
  For Each HdFt In .Headers
    HdFt.LinkToPrevious = False
    HdFt.Range.Text = vbNullString
  Next
  For Each HdFt In .Footers
    HdFt.LinkToPrevious = False
    HdFt.Range.Text = vbNullString
  Next
  Set DocSrc = Documents.Open("X:\WorkTemplate\Forms\04-10A.docx")
  .FormattedText = DocSrc.Range.FormattedText
End With
DocSrc.Close savechanges:=wdDoNotSaveChanges
Set DocSrc = Nothing: Set RngSel = Nothing

Cheers
Paul Edstein
[MS MVP - Word]
 
It errors out on line
[highlight #FCE94F].FormattedText =[/highlight] DocSrc.Range.FormattedText

Compile Error states method or data member not found and highlights .FormattedText =

I tried changing it to RngSel.FormattedText = DocSrc.Range.FormattedText
but then I get an error "Cannot copy between these two ranges."
 
Oops - that should have been:
.Range.FormattedText = DocSrc.Range.FormattedText
and should have been fairly obvious from the previous iteration of the code.


Cheers
Paul Edstein
[MS MVP - Word]
 
That took care of the error but the Header from the previous pages is still getting placed on the new page created by the section break where the copied text gets inserted.
 
That can only occur if your inserted document contains Section breaks of its own. See my advice about that in post #4. The alternative to the approach suggested there is to delete the extra headers & footers after inserting the document, as in:
Code:
Dim RngSel As Range, DocSrc As Document
Dim HdFt As HeaderFooter, Sctn As Section
Selection.InsertBreak Type:=wdSectionBreakNextPage
Set RngSel = Selection.Range
With RngSel
  For Each HdFt In .Sections.First.Headers
    HdFt.LinkToPrevious = False
  Next
  For Each HdFt In .Sections.First.Footers
    HdFt.LinkToPrevious = False
  Next
  Set DocSrc = Documents.Open("X:\WorkTemplate\Forms\04-10A.docx")
  .FormattedText = DocSrc.Range.FormattedText
  For Each Sctn In .Sections
    For Each HdFt In Sctn.Headers
      With HdFt
        If .LinkToPrevious = False Then .Range.Text = vbNullString
      End With
    Next
    For Each HdFt In Sctn.Footers
      With HdFt
        If .LinkToPrevious = False Then .Range.Text = vbNullString
      End With
    Next
  Next
End With
DocSrc.Close savechanges:=wdDoNotSaveChanges
Set DocSrc = Nothing: Set RngSel = Nothing


Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top