views:

10

answers:

1

The application I support is creating an amalgamted Word document by copying couple of Word documents in one document right after each other.

The problem is the format of the some of the fields of the document that gets appended is changed in amalgamated document while the amalgamated document is the copy of AppendDocument (imagine if we have one document to copy in the amalgamated document)

The first and second line of of the Word document looks like:

From: Mr x To: Gary Y

Address: NorkYork Date: 2010/05/01

From:, To:, Address: and Date: are bold and size 10 in the AppendDocument while in amalgamated document they are bold but their size change to 12!

I confused I am not sure why the size for these 4 items are changed even their actual values have the same size!

Please see the below code which 2 documents path are passed. The first one is the BaseDocuemnt or amalgamated document and the second one is the document which is appended.

Private Sub DocumentAppend(ByVal strBaseDocument As String, ByVal strAppendDocument As String)
    Dim FirstDocument As Boolean
    Dim fleBaseDocument As File
    Dim wrdRange As Word.Range
    Dim wrdAppendDocument As Word.DocumentClass
    wrdAppendDocument = New Word.DocumentClass()
    Dim AmalgamatedDocument As Word.DocumentClass
    AmalgamatedDocument = New Word.DocumentClass()
    Dim wrdApp As Word.ApplicationClass
    wrdApp = AmalgamatedDocument.Application
    Dim AmalgamatedDocumentRange As Word.Range
    Try
        wrdApp.Visible = True
        If fleBaseDocument.Exists(strBaseDocument) Then
            FirstDocument = False
            AmalgamatedDocument = wrdApp.Documents.Open(strBaseDocument)
        Else
            FirstDocument = True
            AmalgamatedDocument = wrdApp.Documents.Add()
        End If

        AmalgamatedDocumentRange = AmalgamatedDocument.Content
        AmalgamatedDocumentRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        If Not FirstDocument Then
            AmalgamatedDocumentRange.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
        End If
        ''# get the document to be appended
        wrdAppendDocument = wrdApp.Documents.Open(strAppendDocument)
        wrdAppendDocument.Activate()
        wrdAppendDocument.Select()
        ''# +++++++++++++++++++++++
        wrdApp.Selection.Copy()
        wrdApp.Selection.CopyFormat()
        AmalgamatedDocument.Activate()
        wrdRange = AmalgamatedDocument.Content
        wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        wrdRange.Paste()
        ''# New
        wrdApp.Selection.PasteFormat()
        ''# +++++++++++++++++++++++
        wrdAppendDocument.Close()
        ''# save the new document
        AmalgamatedDocument.SaveAs(FileName:=strBaseDocument)
        AmalgamatedDocument.Close()
End Sub

Any advice would be greatly appreciated!

A: 

Well, first, I'd start by trying to avoid use of the SELECTION object when you're not actually wanting to manipulate the active onscreen document in Word.

I'd also suggest looking into the Range.InsertFile method. Basically, you open or create your "target" document, then obtain a range object of the CONTENT, collapse it to the end, and finally invoke the INSERTFILE to insert the file at that point.

Something like this

        dim rngend = Doc.Content
        rngend.Collapse(WdCollapseDirection.wdCollapseEnd)
        rngend.InsertFile(File, ConfirmConversions:=False, Link:=False, Attachment:=False)

That will usually preserve formatting faithfully, though I've run into some off situations where it's not quite 100%

drventure