none
Word insertfile method not inserting all files VB .Net

    Question

  • Hi,

    I am trying to insert multiple documents into one large document so that I can print them in the proper order.  However, while I am able to insert most of the files (all of them in the correct order), some of the files are missing.  My current program inserts 67 documents into one document; about 8 or 10 are missing from the group of documents....  I have a log file with 3 or 4 errors, but this does not account for all of the missing documents.

    Also, the word documents seems to divide or split the documents into two 200+ page documents.  Is there any way to make it only one document?  

    Here is my insertfile code:  


                        m_WordApp.Selection.InsertFile(filepath, ConfirmConversions:=False, Range:="", Link:=False, Attachment:=False)
                        m_WordApp.Selection.InsertParagraphAfter()
                        m_WordApp.Selection.InsertBreak(Type:=WdBreakType.wdPageBreak)
                        m_WordApp.Selection.Collapse(Direction:=WdCollapseDirection.wdCollapseEnd)
    Thursday, August 01, 2013 7:03 PM

Answers

  • Hi Erik,

    I agree with Cindy, I think some of your documents are broken.

    First, you need to identify these broken documents.

    Then, you can follow steps below to repair these documents.

    On the File menu, in the Open dialog box, select the document and then select Open and Repair from the drop-down list available at the Open Command Button.

    When the document opens, a dialog box will appear to show you the repairs the document has made. Don’t worry about what’s listed in that dialog box, even if the list is long. Just close it and then save the document to make the repairs permanent.

    After that, you can run your code and see whether your issue is resolved?


    Saturday, August 03, 2013 10:39 AM
    Moderator

All replies

  • I've got an app for that!

    It's called zAPPs-apps and you try it free for a week.

    http://www.greatcirclelearning.com/productivity-automation-tools-for-Microsoft%20Office

    I'd be interest to know if it has the same issue with your documents because I don't see any issue with the InsertFile code you have shown us. Thus if my app works fine, then it points to a problem somewhere else in your solution's code. Or if my app ends up with the same issue, that says there is something wrong in those 8 - 10 documents that neither solution are picking up.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, August 01, 2013 7:46 PM
  • Hi Rich, 

    Thanks for sharing your app with me.  However, I would prefer to stick with my custom code for now...

    the loop writes to a log file each iteration, and all of the files are listed as having been inserted by the code I have shown you.

    Have you ever experienced a similar problem, or heard of anyone else who has?  If so, what was their solution?

    Thanks again,

    -Erik

    Thursday, August 01, 2013 7:57 PM
  • Hi Erik,

    No I have not heard of something occurring like that... I would have told you if I did.

    I only suggested my app as a pure test to help you possibly find what's going on in your solution OR is it those files. Have you tried inserting those missing files separately?

    I put your code into a VBA routine and ran it against a folder of documents, only about 20, on my system and the inserts worked without issue so that is why I was suggesting some other tests on your end.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, August 01, 2013 8:05 PM
  • Hi Rich, good to know.  Thanks for trying out my code btw...

    I simply have a for loop, in which I call my function each iteration like this:  

    MyClass.mergeDocs(filepath, i) where I is the counter and filepath is the path to the file to merge.

    The function then writes to the log file and uses the code I showed you merge the new document into one main document.

    I initialize the word document and Word application as follows:

                WordApplication = New Word.Application
                WordApplication.ActivePrinter = printername
                Dim doc As Document = WordApplication.Documents.Add()
                Dim paragraph1 As Paragraph = doc.Paragraphs.Add()
                paragraph1.Range.PageSetup.Orientation = WdOrientation.wdOrientLandscape
                paragraph1.Range.PageSetup.LeftMargin = 25.85F

    -Erik

    Thursday, August 01, 2013 8:37 PM
  • One other element:  

    I have an array of the filenames I have already added, that checks for duplicates:

    If Not Arrayoffiles.Contains(filepath) Then

    do the insert....

    End if

    -Erik

    Thursday, August 01, 2013 8:40 PM
  • Hi Rich,

    Thanks for your help!  I loaded just 4 files, two of which weren't merging.  They don't merge even when there are just 4 files....

    I opened one of them with word, and it opened fine.  How can I tell if the file has an issue?

    Also, I removed the filter for duplicates, just in case...

    I am getting some errors in the output window I noticed.  Here are a couple of them:

    Loaded 'C:\Windows\SysWOW64\cryptsp.dll', Cannot find or open the PDB file
    Loaded 'C:\Windows\SysWOW64\rsaenh.dll', Cannot find or open the PDB file

    Loaded 'C:\Windows\SysWOW64\RpcRtRemote.dll', Cannot find or open the PDB file

    Not sure what they mean.

    -Erik

    Thursday, August 01, 2013 10:14 PM
  • You can try the command mentioned in this link and see if it suppress these automatic routines from running.

    http://word.mvps.org/faqs/interdev/DisableAutoMacros.htm


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, August 01, 2013 10:36 PM
  • Hi NordicViking,

    I failed to reproduce your issue on my lab machine (Office 2013). Below is my sample code:

    Imports Microsoft.Office.Interop.Word
    
    Module Module1
    
        Sub Main()
    	GenerateSampleDocuments()
            CombineDocuments()
        End Sub
    
        Sub CombineDocuments()
            Dim app As New Application
    
            Dim doc As Document = app.Documents.Add(Type.Missing, Type.Missing, WdNewDocumentType.wdNewBlankDocument, False)
    
            doc.Activate()
    
            doc.Select()
    
            Try
                For i As Integer = 0 To 99
    
                    app.Selection.InsertFile("{PATH}\\Sample\\" + String.Format("FileToInsert_{0}.docx", i), "", False, False, False)
    
                    app.Selection.InsertParagraphAfter()
    
                    app.Selection.InsertBreak(WdBreakType.wdPageBreak)
    
                    app.Selection.Collapse(WdCollapseDirection.wdCollapseEnd)
                Next
    
                doc.SaveAs2("{PATH}\\HelloWorld.docx")
            Catch ex As Exception
    
            End Try
    
            app.Quit()
        End Sub
    
        Sub GenerateSampleDocuments()
            Dim app As New Application
    
            Try
                For i As Integer = 0 To 99
                    Dim doc As Document = app.Documents.Add(Type.Missing, Type.Missing, WdNewDocumentType.wdNewBlankDocument, False)
                    doc.Activate()
    
                    doc.Select()
    
                    app.Selection.InsertAfter(String.Format("Insert Content {0}", i))
    
                    doc.SaveAs2("{PATH}\\Sample\\" + String.Format("FileToInsert_{0}.docx", i))
    
                    doc.Close()
                Next
            Catch ex As Exception
    
            End Try
    
            app.Quit()
        End Sub
    End Module

    Could you tell me which version of Office you are currently using?

    You motioned that there are 3-4 errors in log file, could you also provide me these error messages?

    Regards,

    Jeffrey

       

    Jeffrey Chen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 02, 2013 7:42 AM
    Moderator
  • Hi Erik

    And you sure the check of the array of files isn't discarding some you think it shouldn't? Are you logging that?

    Do these "missed" files have anything in common?

    Have you tried using Insert file as an end-user to see if Word is showing any messages when it tries to open these files?

    Do you have try...catch blocks in your code to ensure you're getting all the exceptions COM may be sending back to your managed code?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 11:37 AM
    Moderator
  • Hi Cindy,

    Thanks you so much for your reply.

    1.  I have removed the array, to avoid any issues; the files are still missing. 

    2.  The missed files are all of the same type, but otherwise don't seem to have anything in common that I can tell.  I'll think about this though....

    3.  No, But I will try using inseret file as an end user

    4.  I have try...catch blocks in my code, and even log the errors that come up.  Many of the missing files do not have any errors associated with them

    -Erik

    Friday, August 02, 2013 2:23 PM
  • The errors with the PDB files means there's a problem with your .NET application's build. I don't know exactly what it means, you'd have to ask in a .NET Framework forum about that. I also don't know if it could have any effect on this particular problem.

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 2:28 PM
    Moderator
  • Hi Erik

    What is the file type of these problem files?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 2:29 PM
    Moderator
  • Hi Jeffrey,

    Thank you for your reply.

    One error message I get during the merge is

    Exception during execution: A table in this document has become corrupted.  at Microsoft.Office.Interop.Word.Selection.InsertFile

    Another is:

    Exception during execution: Word has encountered a problem.   at Microsoft.Office.Interop.Word.Selection.InsertFile

    But the errors don't correlate with all of the missing files

    I am using word 2003 btw....

    -Erik

    Friday, August 02, 2013 2:29 PM
  • It's a word document, but the file extension is .BIL or .PBIL
    Friday, August 02, 2013 2:30 PM
  • Thanks.  I'll ask in the .net framework forum.

    -Erik

    Friday, August 02, 2013 2:31 PM
  • Sorry, .PBL, not .PBIL
    Friday, August 02, 2013 2:32 PM
  • Just as a test, try renaming a couple of these files to .DOC, just to see if it makes a difference.

    Reading your note to Jeffrey, the fact that you're getting corruption and "encountered a problem" reports is important. Even if it appears these don't correlate with the missing files I'd be very suprised if there wasn't some connection.

    You might also try identifying the files that are causing these errors and taking them completely out of your loop, to see if that has any effect. When the message "encountered a problem" shows up it usually mean Word has to stop and restart. If your code is continuing to process, that could account for files not getting imported.

    FWIW, as soon as I read your problem description I was thinking "damaged files". All files from a specific template, for example, where the template is damaged and is generating damaged files.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, August 02, 2013 2:50 PM
    Moderator
  • Hi Erik,

    I agree with Cindy, I think some of your documents are broken.

    First, you need to identify these broken documents.

    Then, you can follow steps below to repair these documents.

    On the File menu, in the Open dialog box, select the document and then select Open and Repair from the drop-down list available at the Open Command Button.

    When the document opens, a dialog box will appear to show you the repairs the document has made. Don’t worry about what’s listed in that dialog box, even if the list is long. Just close it and then save the document to make the repairs permanent.

    After that, you can run your code and see whether your issue is resolved?


    Saturday, August 03, 2013 10:39 AM
    Moderator
  • Hi Jeffrey,

    Thanks to you and all who have responded.  I have run the application without the problem files and it works as expected, correctly that is.

    I also believe the files were corrupted, which was causing the problem.

    Please accept my sincerest appreciation to all of you for your assistance.

    -Erik

    Monday, August 05, 2013 4:53 PM