VSTO 2010 Word document automation hang RRS feed

  • Question

  • Hi,

    I had a word 2007 document project built in vs2008. I recently converted it in vs2010 with office 2010 runtime library. What this project does is to grab data from the sql server database and fill them to a template docx file. The result file will contain multiple pages with plenty of tables. 

    The project can be compiled without any errors. However, everytime I ran it, it would hang at arbitrary location.

    It might hang when trying to set the text of a cell in a table(e.g .range.text = "xxxx") or set the background color of a cell etc.

    What's worse is sometimes it will stop my vs2010 from debugging and I had no choice except killing them from the task manager.

    Can anyone help me to solve this problem.



    Monday, February 13, 2012 4:53 AM

All replies

  • Are you getting any error messages? If yes, please provide the complete text of the message.

    Do you have your code in try...catch blocks?

    Cindy Meister, VSTO/Word MVP

    Monday, February 13, 2012 7:02 AM
  • Hi Cindy,

    Thanks for your reply.

    There are no error messages at all. The program wasn't crashing. I don't have try catch blocks on them which I should have done it, but I ran the program in Debug mode, VS should catch any exceptions.

    The last time I ran the program, it hung at the following sentence:

    this.Tables[x].Rows[y + i].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray15;

    I think there is nothing wrong with this sentence, the program will just hang at any arbitrary location.


    Monday, February 13, 2012 10:05 PM
  • Hi Ednu

    So, by "hang" you mean it just stops. How long do you wait?

    If you look in the Task Manager what does it say about the Word appliction?

    If there's no error, how do you know what line of code it's at?

    And you're certain Word's not showing you any kind of message in the UI?

    Based on what you've told me, my inclination would be that Word is running into memory problems. Typically, generating and formatting "plenty of tables" can cause Word's scratch files to overrun. Usually, Word will a message to the effect that it's not going to be able to undo anything from a certain point forward, which is what I was "fishing" for.

    Assuming it is this known problem, the way out is to force a SAVE of the file at regular intervals (well before the hang happens) and to run Document.Undo.Clear to flush the buffers.

    Cindy Meister, VSTO/Word MVP

    Tuesday, February 14, 2012 7:34 AM
  • Thx Cindy and sorry for the late reply as I was off yesterday.

    The last time I ran the problem I waited more than half a day and then I needed to kill the process via task manager.

    The cpu usage is between 25% and 50%.(quad system). The memory was 70Mb, and the system had more than a GB memory left.

    As I mentioned before, I ran the problem in debug mode in VS2010. Thus once I suspected the hang, I can break the debugging and VS would tell me the current line of code of execuation.

    I tried to build a release binary and ran it. It gave me the same result, it hung, with the "Non responding" title appear in the title bar and there was no error message. But maybe there are some trace files generated by the word during the automation?

    I will try the suggestion that you provided. will let you know the result.


    • Edited by ednujiam Thursday, February 16, 2012 4:52 AM
    Thursday, February 16, 2012 3:05 AM
  • Hi Cindy,

    I've tried the suggestion that you provided, but the problem hasn't been solved. 

    The way I implemented it was a little bit different to what you suggested. Instead of saving the file at regular interval and then Clear undo, I just called Document.UndoClear() to flush the buffers after each page's generation. 

    One thing bothering me is why this program worked perfectly in VS2008 with office 2007. I think this may be a key point to solve this problem.

    Thursday, February 16, 2012 4:09 AM
  • Please try it the way I suggested.

    Cindy Meister, VSTO/Word MVP

    Thursday, February 16, 2012 5:11 AM
  • I did save the doc before UndoClear this time, but didn't solve the problem either.

    Thursday, February 16, 2012 11:14 PM