Formatting with InsertFile

F

fsharmeen

I'm trying to combine two word documents into a single file using the
InsertFile method and it seems to be working pretty well, except for
the formatting in the final document.

The individual word docs that need to be combined are styled using a
template that keeps some text on top in a single column while
formatting the rest of the doc in two columms. The problem I'm seeing
is that everthing below the first document's (DocA) header text is
getting styled in two columns - including the header of the second
document which should be in a single column.

Here's my code:

'Loop through all the files in the temp dir and copy them to one file
Dim oFinalDoc As Word.Document = oWord.Documents.Add()
Dim oFinalSelection As Selection = oWord.Selection()
For Each oFile As FileInfo In oDir.GetFiles("*.doc")
oFinalSelection.InsertFile(oFile.FullName)

oFinalSelection.InsertBreak(Word.WdBreakType.wdPageBreak)
Next
oFinalDoc.SaveAs("C:\Windows\Temp\1234\FinalDoc.doc")

Would appreciate any help in figuring out how to retain the formatting
in the final doc exactly as it is in the individual docs.

Thanks,
Fauzia
 
J

Jay Freedman

After each file, instead of an ordinary page break, you need a next-page
section break in order to separate the two-column layout from the following
one-column layout. That is, change
Word.WdBreakType.wdPageBreak
to
Word.WdBreakType.wdSectionBreakNextPage

You may also have a problem with _where_ the break is being inserted. You
can't rely on the Selection being at the right place after the file
insertion. If the code puts the section break in the wrong place, precede
the InsertBreak statement with
oFinalSelection.EndKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
F

fsharmeen

Jay, thanks a lot for your response - I modified the code as you
suggested but I'm still seeing the same issue. Here's my code with the
changes:

'Loop through all the files in the temp dir and copy them to one file
Dim oFinalDoc As Word.Document = oWord.Documents.Add()
Dim oFinalSelection As Selection = oWord.Selection()
For Each oFile As FileInfo In oDir.GetFiles("*.doc")
oFinalSelection.InsertFile(oFile.FullName)
oFinalSelection.EndKey(Word.WdUnits.wdStory,
Word.WdMovementType.wdMove)

oFinalSelection.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage))
Next
oFinalDoc.SaveAs("C:\Windows\Temp\1234\FinalDoc.doc")

Thanks for your help,
Fauzia
 
J

Jay Freedman

OK, I see what's happening. The one-column section from the inserted file
isn't overriding the two-column setting of the new section that's created by
the section break. To fix that, add one more statement immediately after the
InsertBreak statement:

oFinalSelection.Sections(1).PageSetup.TextColumns.SetCount(1)

Because the selection is in the new section after the InsertBreak happens,
the expression oFinalSelection.Sections(1) refers to that new section (that
is, the first section that's in the selection). The SetCount method will
change its number of columns to 1, in preparation for adding the next file.
 

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