none
Merging multiple word documents in a folder to a single word document in a particular order RRS feed

  • Question

  • I need to create a word document which is a combination of word documents in a particular folder. I need to create this new document such that File1 in the folder is inserted first and File2 is inserted second in the new word document. I tried two approaches which kind of works but still have some issues.

    Approach 1.

            Dim wdPageBreak As Object = 7
            Dim wdStory As Object = 6
            Dim oMissing As Object = System.Reflection.Missing.Value
            Dim oFalse As Object = False
            Dim oTrue As Object = True

            Dim WordApp As New Microsoft.Office.Interop.Word.Application()
            Dim wDoc As Microsoft.Office.Interop.Word.Document = WordApp.Documents.Add(oMissing, oMissing, oMissing, oMissing)

                If dirInfo.Exists Then
                    Dim files As FileInfo() = dirInfo.GetFiles()
                    Dim iCount As Integer = 0

                    For Each file As FileInfo In files
                        fileName = file.FullName
                        wDoc.Application.Selection.Range.InsertFile(fileName, oMissing, oMissing, oMissing, oFalse)
                        If iCount > 0 Then
                            wDoc.Application.Selection.Range.InsertBreak(wdPageBreak)
                        End If
                        wDoc.Application.Selection.EndKey(wdStory, oMissing)
                        iCount = iCount + 1
                    Next
                End If

    Problem with this approach is it is working fine in terms of creating a single word document but it adds an extra line when it inserts a word document from the folder. Can anyone please let me know how i can make sure that no extra line is inserted?

    Approach 2.

                Dim rng As Microsoft.Office.Interop.Word.Range = wDoc.Content
                Dim oEnd As Object = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd
                Dim oBreak As Object = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak

                If dirInfo.Exists Then
                    Dim files As FileInfo() = dirInfo.GetFiles()

                    For Each file As FileInfo In files
                        fileName = file.FullName
                        rng.InsertFile(FileName:=fileName)
                        rng.InsertBreak(oBreak)
                        rng.Collapse(oEnd)
                    Next
                End If

    In this approach i am not able to control the order of word documents inserted i.e it doesnt insert File 2 after File 1 and so forth and i end up with a word document which has the order jumbled. Also it adds page breaks after the first file insert instead of every file insert and i end up with blank pages after first file insert.

    In order to have the Files in order while parsing through the folder i name them 1a.doc ,1b.doc, 2a.doc and dirInfo.GetFiles() gives me the array in the order.

    Any inputs and suggestions will be appreciated. Thanks in advance.

     

     

    Tuesday, January 17, 2012 4:38 PM

All replies

  • When you open a new blank document there is always one paragraph mark.  In the first iteration of the new content insert loop all of the content from File 1, including its last paragraph mark is placed into the new file.  Thus you end up with an extra ending paragraph mark.

    What to need to when you collapse the range to the end of what was just inserted, is to delete one paragraph.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, January 17, 2012 10:12 PM
  • Approach 2 i am having problem controlling the order of insert. Approach 1 works for me except for the extra line during insert. Can you please give me some code sample which allows to remove the paragraph or anything which will allow me to combine word docs in a folder and add a page break after each insert? I really appreciate any help. Thanks
    Wednesday, January 18, 2012 3:03 PM