INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

(OP)
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


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

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.

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

(OP)
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.

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

Right.

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

Quote:

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]

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

(OP)
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?

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

Quote:

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]

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

(OP)
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.

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

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]

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

(OP)
So the magic question is how do I unlink and delete headers/footers using vba?

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

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]

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

(OP)
It errors out on line
.FormattedText = 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."

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

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]

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

(OP)
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.

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

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]

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close