inserting doc w header into another doc

T

Ted

hi, i'm creating a word document on the fly w VBA from Access. i'm trying to
insert a word document, which has a header and footer, into a word document
that does not have a header and footer, at the end of this document i'm
inserting i will be inserting another document wo a header and footer. can
someone point me in the right direction?

TIA
Ted
 
D

Doug Robbins - Word MVP

You will need to insert a Section Break after the document that has the
header and then when you insert the next document into the section at the
end of the document, unlink the header in that section from that in the
previous section and delete the content of the header if you do not want it
to appear.

If you want to prevent the header of the first inserted document from
appearing in the first part of the document that you are creating, you will
need to insert a section break before the document with the header, unlink
the header in that section from that in the first section of the document so
that you can delete the header from the first section without deleting it
from the second section.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
T

Ted

Thank you Doug! That helped a lot. One other question. The middle document
has page numbers but i only want them to count the pages in the middle
document. Even with the section break the first page of the middle document
is coming up as page 2. Any ideas? Thanks Doug
 
S

Stefan Blom

You can restart page numbers within a given section. Activate the
header/footer view, for example by double-clicking the header. Verify that
the insertion point is in the correct section. Then use the Page Number
Format dialog box to specify the desired starting page number for the
section.

To open the dialog box: If you are using Word 2007, on the Header & Footer
Tools Design tab, click Page Number, and then click Format Page Numbers. In
Word 97-2003, click the Format Page Number button on the Header and Footer
toolbar.
 
T

Ted

Thanks again Doug, how do i "unlink" the header or footer. i inserted the
Section Break but the page numbers in the footer are still counting the
pages of the entire document not the pages of the document being inserted.
i've included a snipet of my code below...I'm merging a lot of documents
together and I'd like for each document being inserted to preserve the
header.footer info as if it was being printed out. any help would be very
much appreciated. thank you for your time.

i also tried the following code which kept the formatting of the document
better but did not bring in the headers or footers. is it possible to use
the AddOLEObject method and it bring in the header and footer?
~~~~~~~~~~~~~~~~~~~
Selection.InlineShapes.AddOLEObject ClassType:="Word.Document.8", FileName _
:="C:\Test\Forms\FormName1.doc", LinkToFile:=False, _
DisplayAsIcon:=False
~~~~~~~~~~~~~~~~~~~

objWord.Documents.Open FileName:="C:\Test\Form1.doc"
Selection.EndKey Unit:=wdStory
objWord.Selection.InsertBreak Type:=wdSectionBreakNextPage
objWord.Selection.InsertBreak Type:=7
'Dynamically create document
*Run code*
objWord.Selection.InsertBreak Type:=7
objWord.ActiveDocument.SaveAs FileName:="C:\Test\Policies\" &
txtPolNumber & ".doc"

With Selection.PageSetup
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
End With

objWord.Selection.InsertFile "C:\Test\\Form2.doc"

objWord.Selection.EndKey Unit:=wdStory
objWord.Selection.InsertBreak Type:=wdSectionBreakNextPage
objWord.Selection.InsertBreak Type:=7

objWord.Selection.InsertFile "C:\Test\Forms\FormName1.doc"
objWord.Selection.EndKey Unit:=wdStory
objWord.Selection.InsertBreak Type:=7
objWord.Selection.InsertBreak Type:=wdSectionBreakNextPage

objWord.Selection.InsertFile "C:\Test\Forms\FormName2.doc"
 
D

Doug Robbins - Word MVP

To get the pages in the documents being inserted, the { NUMPAGES } field in
those documents would need to be replaced by a { SECTIONPAGES } field

To have the numbering in each section commence at 1, you would have to
format the pages numbers so that they start at that number rather than
continue from the previous section.

That can be done with code as follows:

Dim s As Section
For Each s In ActiveDocument.Sections
With s.Headers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
Next s


--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
T

Ted

Thanks Doug, that is very informative. The documents i'll be inserting into
the document i'm creating have been given to us. there are hundreds to
choose from. i checked the code and they have the following in the footer.

Page { PAGE \* MERGEFORMAT} of {NUMPAGES \* MERGEFORMAT}

is there anyway to have it count the page numbers by section wo changing the
footer code for page numbers? i'm not sure we're supposed to be altering
these documents.

Thanks
Ted
 
D

Doug Robbins - Word MVP

Hi Ted,

I addition to setting the page numbering to start at 1 for each section, the
following will change the NUMPAGES field to a SECTIONPAGES field so that the
pages would be number 1 of # where # is the number of pages in the Section:

Dim s As Section
Dim f As Field
Dim fcode As Range
For Each s In ActiveDocument.Sections
With s.Headers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
With s.Footers(wdHeaderFooterPrimary).Range
For Each f In .Fields
If f.Type = wdFieldNumPages Then
Set fcode = f.Code
fcode.text = "SECTIONPAGES"
End If
Next f
.Fields.Update
End With
Next s


--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top