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

Maintain Headers When Stringing Files Tpgether 1

Status
Not open for further replies.

CPForecast

Programmer
Sep 15, 2008
19
US
Hello,

I am working on a macro that will string a series of files together in a single new document. These files have their own header and footer settings and also have their own page numbers that must be persevered. However, when it currently inserts a new document, the page numbers and header/footer settings are continuous from the last document. In this example, I am simply putting together two documents, but I will eventually be stringing together 15-20 documents at a time. I know there is a way to maintain each document's original properties, but I can't seem to figure it out. Any suggestions from the experts would be very greatly appreciated! Thank you!

Code:
Public Sub MAIN()

Dim wdApp As Word.Application, wdDoc As Word.Document
Dim wip, year, path, Filename, fullname, noextension
Dim sectionNum As Long

    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then 'Word isn't already running
    Set wdApp = CreateObject("Word.Application")
    End If

Set wdDoc = wdApp.Documents.Open("C:\Doc1.doc")


'Create the file
ActiveDocument.SaveAs ("C:\NewDoc.doc")
'Go to the end of the document
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdSectionBreakOddPage


sectionNum = ActiveDocument.Sections.count


'Get current page header and set linktoprevious = false
Dim r As Range
Dim oSection As Section
Dim hfType

'Set r = ActiveDocument.Sections(sectionNum).Headers
Set oSection = ActiveDocument.Sections(sectionNum)

With oSection
    For hfType = 1 To 3
        'oSection.Headers(var).LinkToPrevious = False
        Set r = oSection.Headers(var).Range
        r.Delete
    Next
End With


'     Set r = ActiveDocument.Sections(sectionNum).Headers(hfType).Range
'     r.Delete
     
'     with r
'     .Headers(hfType).LinkToPrevious = False
'     .Footers(hfType).LinkToPrevious = False
'     end with


'Insert next document
Selection.InsertFile ("C:\Doc2.doc")

'etc...

End Sub
 
Also, I apologize for the typo in this thread's title. I can't seem to find a way to edit it.
 
OK, you have also posted this to VBA Express. I will answer here, but mention that in your other thread.

This is not trivial.

There are a number of factors to be considered.

InsertFile inserts the file. However, it inserts the file INTO the destination document using the destination document properties.

Say the file to be inserted has DifferentFirstPage, DifferentOddEven for the headers. Say the destination document does NOT have this. It only uses Primary.

InsertFile will indeed insert the file, but those DifferentFirstPage, DifferentOddEven settings will be lost. You can get around this by opening the incoming file, and getting settings and contents, and then putting those into the destination document. Here is one way.
Code:
Sub MyInsertFile()
Dim ThisDoc As Word.Document
Dim ThatDoc As Word.Document
Dim r As Range
Dim oSection As Section
Dim oHF As HeaderFooter
Dim sectionNum As Long
Dim var

Set ThisDoc = ActiveDocument

ThisDoc.SaveAs ("C:\zzz\yadda_B\NewDoc.doc")

[COLOR=red]' set a range object for the destination document[/color red]
Set r = ThisDoc.Range

[COLOR=red]' Go to the end of the document
' make a Section break[/color red]
With r
   .Collapse 0
   .InsertBreak Type:=wdSectionBreakOddPage
   .Collapse 0
End With

[COLOR=red]' set the Section object, now the LAST Section
' newly made[/color red]
sectionNum = ThisDoc.Sections.Count
Set oSection = ThisDoc.Sections(sectionNum)

[COLOR=red]' open the file to be "inserted"[/color red]
Set ThatDoc = Documents _
   .Open(Filename:="C:\zzz\yadda_B\THead1.doc")
  
[COLOR=red]' set the new section PageSetup to equal
' the incoming file settings[/color red]
oSection.PageSetup.DifferentFirstPageHeaderFooter = _
  ThatDoc.PageSetup.DifferentFirstPageHeaderFooter
oSection.PageSetup.OddAndEvenPagesHeaderFooter = _
   ThatDoc.PageSetup.OddAndEvenPagesHeaderFooter

[COLOR=red]' [b]explicitly[/b] delete any carry-over
' header content in the new Section
' [b]explicitly[/b] de-link to previous headers
' make each header range equal the header range
' of the incoming file[/color red]
var = 1
For Each oHF In oSection.Headers
   With oHF
      .Range.Delete
      .LinkToPrevious = False
      .Range = ThatDoc.Sections(1).Headers(var).Range
   End With
   var = var + 1
Next

[COLOR=red]' now insert the [b]content[/b] of the incoming file
' ThatDoc, at the end the destination dcoument range[/color red]
r.InsertAfter (ThatDoc.Content)

[COLOR=red]' close the incoming document file[/color red]
ThatDoc.Close wdDoNotSaveChanges
End Sub
Unfortunately, while this will bring in the proper Section settings, and the content of the headers (and the content of the document), it will NOT bring in the format of the headers.

This could be done, but takes more tweaking. Further, I have no idea if you are using Styles fully. if you are, then that could make things easier, or it could make things harder. If the Styles used in the headers of the incoming file, do not exist in the destination file...problems.

As for the page numbering...again, it could possibly be done, but you would have to add further code. I do not see how you can get page numbering to shift to the incoming file without opening it. InsertFile will definitely not do it.

Gerry
 
Thanks for the suggestion thus far, Gerry. Yes, I see that all of the formatting in the second document is getting blown out. I thought that just the headers would come in unformatted, but it looks like the formatting for the entire document is cleared out.

The same styles are being used in each document that will be added. Will this make it easier to carry over the formatting for the document headers/footers and body?
 
Yes. If the same styles (the actual styles, not how they are used) are the same in each, it should make it easier.

The formatting is blown away because the contents are being brought in as implicitly unformatted. You can get around this by opening the files (rather than InsertFile) and bring in the ranges as Formattedtext.

Gerry
 
Gerry,

The decision makers have decided to go about this problem a different way. Thanks again for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top