Combining word documents in VB.NET

M

Martin

I am new to VB so be nice :)

I am trying to merge a number of word documents together, and am
trying to use the master/subdocument concept.

However, the documents are appearing at the top of the master
document, instead of being added sequentially at the end of the
document (which is what I want).

I have a template document which I wish to append these subdocuments
to. This template will contain a cover sheet, table of contents,
styles, headers/footers, etc etc.

The code I have is

Dim oDoc As Word.Document
Dim oPara As Word.Paragraph

oPara =
oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara.Range.Text = "Heading 1"
oPara.Range.InsertParagraphAfter()

oDoc.Application.ActiveDocument.Subdocuments.AddFromFile("c:
\document1.doc")

oPara =
oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara.Range.Text = "Heading 2"
oPara.Range.InsertParagraphAfter()

oDoc.Application.ActiveDocument.Subdocuments.AddFromFile("c:
\document2.doc")

However, the document generated looks like

c:\document1.doc <------ these are the hyperlinks to the subdocuments
c:\document2.doc

Template document stuff

Heading 1

Heading 2




I have tried making use of the endofdoc bookmark to try to get the
subdocuments at the end using the following:

oDoc.Application.ActiveDocument.Subdocuments.Expanded = True
oDoc.Application.Selection.InsertParagraphAfter()
oDoc.Bookmarks.Item("\endofdoc").Range.Subdocuments.AddFromFile(vReports(vCount))

but this doesn't help either.

Can anyone please help me?

Thanks

Martin
 
M

Martin

I've got it resolved now. I added the following:

oDoc.Application.Selection.EndKey(Word.WdUnits.wdStory)
oDoc.Application.ActiveDocument.Subdocuments.AddFromFile("c:
\document1.doc")

This moves to the last page and adds the subdocument.
 

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