1
votes

Working with Word and VB.net automation to build one Word document comprised of several documents appended together.

So I do Word.Documents.Add(firstDocument), and then go to the end of the file and do Word.Selection.InsertFile(secondDocument) and it works as expected, except that if firstDocument is for example Verdana 10pt and secondDocument is Calibiri 11, it's inserting secondDocument using the font & style of firstDocument.

I couldn't find anything in the documentation for Selection.InsertFile that would have any impact over the formatting, so I believe it must be controlled another way. I also tried inserting a page break and a section break (with page break) before inserting the file, but found that it made no difference on the font.

Currently I have the app copying everything from secondDocument, closing secondDocument, opening firstDocument, move to end and then paste. This is problematic for many reasons, but it preserves the formatting.

Any ideas how I can keep the exact formatting of secondDocument when inserting into firstDocument? I need to get away from using Clipboard!

Thanks!

EDIT: Here's the code I've been testing trying to get the logic right:

 Public Sub TestingWord()
    Dim thisApp As New Word.Application
    Dim SourceDoc As New Word.Document
    Dim DestDoc As New Word.Document

    Try
        thisApp.Visible = False

        DestDoc = thisApp.Documents.Add("X:\Isaac\First.docx")

        thisApp.Selection.WholeStory()
        thisApp.Selection.EndKey(Unit:=6)
        thisApp.Selection.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage)

        thisApp.Selection.InsertFile("X:\Isaac\Second.docx")

        thisApp.Selection.WholeStory()
        thisApp.Selection.EndKey(Unit:=6)
        thisApp.Selection.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage)

        thisApp.Selection.InsertFile("X:\Isaac\Third.docx")

        DestDoc.SaveAs2("X:\Isaac\Yo.docx")

        thisApp.Quit(SaveChanges:=Word.WdSaveOptions.wdSaveChanges)
        releaseObject(DestDoc)
        releaseObject(SourceDoc)
        releaseObject(thisApp)

    Catch ex As Exception
        MsgBox("Error: " & ex.Message.ToString)
    Finally
        MsgBox("Success!")
    End Try

End Sub
1
seems like you can copy paste the document and preserve the original formats stackoverflow.com/questions/32472020/…Slai
Yes, I could copy and paste and it works, but as I said in the original post, I have to get away from using Clipboard for many reasons.LegalEagle

1 Answers

0
votes

Solved it!

It's not in the code. Problem is when you insert a file, Word applies the same style formatting as the destination document. In my case, since both First.docx and Second.docx used a style called "Normal", Word read the "Normal" style of Second.docx to be the same as First.docx when in fact the two styles had different definitions.

Solution:

I went through each of my templates and created unique new style names for every font size and type on all documents. Ran the process again and it worked like a charm!