none
Word Hangs without giving any error

    Question

  • Hi,

    We need to convert 1000s of xml file to doc files and we are using Interop Word instance for each xml document. When it is iterating through the xml files at some arbirary points the process hangs without giving any errors or warning. For example if I run the process for the same 4000 xml files sometimes it stops at 1100, 1200, 3000 etc. If I make the process visible, when it is not responding I can see a document with some saved text. Is there any way to get com exceptions to our end? Or what can be the reason for this?

    Thanks!


    CJ
    Wednesday, November 24, 2010 1:14 AM

Answers

  • Hi,

    I think we have found the reason for our problem but not sure the answer...

    We have some .txt files that are converted and saved as .Doc; if we try to convert such a file to docx it throws and exception ("Command is not available"). If we save as it as "Docx" rather than converting then it works fine.

    WordInterop.ActiveDocument.Convert();

    Is there any way to identify whether we need SaveAs or Convert ?

    Or is there any way to find the document is in ".txt" format?

    Thank!


    CJ
    • Marked as answer by CharithJ Friday, October 07, 2011 1:30 AM
    Monday, December 06, 2010 4:58 AM

All replies

  • Hi CJ

    Word was never designed to work without user intervention and has always had problems with such high-volume processing...

    Which version of Word is this? And what, more exactly, is your code doing (besides using the Open and SaveAs commands)?

    Is there a particular reason you're not processing the closed documents directly? The binary file format has been open-source for quite a few years, so you wouldn't need to "interop"...


    Cindy Meister, VSTO/Word MVP
    Wednesday, November 24, 2010 5:30 PM
  • Hi Cindy,

    Thank you for the reply!

    We are using Word 2007. We were doing Initiate, Open, SaveAs, Close and Quit...

    Anyway we were decided to use some other third party control to get done our job which I guess much easier than using word-Open-source functionalities.

    Thanks Again!


    CJ
    Thursday, November 25, 2010 12:41 AM
  • Hi Charith

    Yes, the third-party tool is probably the more efficient way to go, but...

    FWIW the most likely cause of your problem was the Initiate/Quit part within the batch. In the future, if you need to use the interop to process multiple files (more than one) it would be better to initiate once, process the files, then quit - once. Word (and the machine resources) can better handle multiple files opening/saving/closing than initializing and quitting itself repeatedly within a short time span.


    Cindy Meister, VSTO/Word MVP
    Thursday, November 25, 2010 8:51 AM
  • Hi Cindy,

     

     

    The first version of the program did exactly that…and after a few hundred documents word was consuming massive amounts of memory and slowed down so much as to not be practical.

     

    The second version opened an instance of word, used it for x documents (500,200,100…tried various figures), then it would close word and open a new instance. This had the problem that the old instances were remaining in memory and the solution had the same stability problems that I currently have.

     

    The current version does one at a time (open word, open doc, save doc, close doc , quite word) – and it doesn’t have the problem with old copies of word staying in memory, and it’s the best of the three approaches, but it still stops occasionally for no apparent reason.

     

    Thanks!

     


    CJ
    Thursday, November 25, 2010 11:30 PM
  • Hi,

    I think we have found the reason for our problem but not sure the answer...

    We have some .txt files that are converted and saved as .Doc; if we try to convert such a file to docx it throws and exception ("Command is not available"). If we save as it as "Docx" rather than converting then it works fine.

    WordInterop.ActiveDocument.Convert();

    Is there any way to identify whether we need SaveAs or Convert ?

    Or is there any way to find the document is in ".txt" format?

    Thank!


    CJ
    • Marked as answer by CharithJ Friday, October 07, 2011 1:30 AM
    Monday, December 06, 2010 4:58 AM
  • Hi CJ

    When you look at such a file in the Word UI, what do you see in the window's Title Bar? Is it just [Document Name] - Microsoft Word, or is there something like Compatibility Mode up there, as well?

    And if you compare that to another file that works properly?

    A common problem with "text" files is that they may use (for Word) "odd" character combinations to generate new "lines". Replacing these in the document can often solve problems. Below, you see one example that works for a client I have where a third-party app generates a document from a database. There's something weird in that Chr(13) (an ANSI carriage return) that makes Word choke.

    In your case, there could also be a Chr$(10) packed in there. You'll need to do some investigating - it might also help to check exactly what's in the original text file in the way of "non-printing ANSI characters".

    Private Sub SubstituteWordParas()
      Dim search As String
      Dim Replace As String
      Dim doc As Word.Document
      
      Set doc = ActiveDocument
      search = Chr$(13)
      Replace = "^p"
      
      SetBasicFindParams doc.Range
      
      'Get true paragraph marks
      With doc.Range.Find
        .Text = search
        .Replacement.Text = Replace
        .Execute Replace:=wdReplaceAll
      End With
    
    End Sub
    

    Cindy Meister, VSTO/Word MVP
    Wednesday, December 08, 2010 6:17 AM