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

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

Status
Not open for further replies.

PMTom

IS-IT--Management
Mar 7, 2017
8
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
 
You may want to re-post it in forum707 which is appropriate for your question.
When you do, please use TGML tags (just above the box where you type your question) to format your code. Use Preview button to make sure your post displays correctly.

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top