none
Optimizing Word Operation RRS feed

  • Question

  • Hi,

     

    I have been searching around and not finding tips for optimizing a MS Word application (written in C#). I see lots of articles for optimizing the code itself but I am looking for ways to change Word that will result in the code running faster.

    For example, I see the suggestion to set pagination=false.

    Are there other Word options what will help?

    I'm working with large documents with lots of tables, charts and graphics.

    Thanks for you help!

     

    yubeck

    Sunday, October 30, 2011 6:31 PM

Answers

  • Hi yubeck

    OK, thanks for the additional information.

    When you paste a range of cells from Excel into Word what you usually get is a table. The first performance hit is going to be the conversion taking place to turn what's coming in from the Clipboard into a Word table (usually, HTML to Word's native file format). The second just has to do with this being a table. Tables are resource-intensive when it comes to Word's calculating the page layout. It has to calculate the optimal column widths, then the line breaks within the columns, then where things break across the page... If these are long tables, especially, that would explain the performance hit and I don't think there's a lot you can do about it if you want to continue using this approach.

    A possible alternative - which would involve a lot of recoding - would be to save the document to the Word 2003 XML file format, then work with the WordProcessingML. You'd do this with the standard .Net Framework XML tools. You'd have to convert the Excel data to valid WordProcessigML for a table, then drop that into the document. The finished product should open a lot faster than the processing being done in the UI.


    Cindy Meister, VSTO/Word MVP
    Monday, October 31, 2011 3:24 PM
    Moderator
  • Hi yubeck,

    Having 'pagination' appearing in the statusbar while you're doing the updates suggests you're:
    1. not using Application.ScreenUpdating = False; and/or
    2. selecting the bookmarked ranges before updating them, rather than simply specifying them as the ranges to update.

    Turning off ScreenUpdating at the start of the update procedure (and turning it back on again at the end) can make the code run much faster, and using ranges instead of selections is also much more efficient.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, November 2, 2011 10:49 PM

All replies

  • Hi yubeck

    Your question is much too general...

    What, exactly, does your application do? Does it create new documents? Does it read or change the content of existing documents? In what way does it interact with the user (if any)? Which version of Word are you targeting?


    Cindy Meister, VSTO/Word MVP
    Monday, October 31, 2011 6:10 AM
    Moderator
  • Thanks for asking Cindy,

     

    It is taking ranges from Excel and putting them into the Word document, using the clipboard for copy/paste. The locations to paste to are identified by bookmarks or Alt Text and metadata is stored in Word document variables.

    I have excluded the interaction with Excel as the time lag. It seems to be something the code is doing is causing word to perform updates to the document. At times the status bar says something about pagination.

    The Word document is complex, containing a TOC linked to styles and a second TOC linked to captions. There are also page numbers. I has many pictures and tables, each with a caption. The document is just under 30 pages.

    I'm working with Word 2003. 

    Thanks again!


    • Edited by yubeck1 Monday, October 31, 2011 2:02 PM
    Monday, October 31, 2011 1:29 PM
  • Hi yubeck

    OK, thanks for the additional information.

    When you paste a range of cells from Excel into Word what you usually get is a table. The first performance hit is going to be the conversion taking place to turn what's coming in from the Clipboard into a Word table (usually, HTML to Word's native file format). The second just has to do with this being a table. Tables are resource-intensive when it comes to Word's calculating the page layout. It has to calculate the optimal column widths, then the line breaks within the columns, then where things break across the page... If these are long tables, especially, that would explain the performance hit and I don't think there's a lot you can do about it if you want to continue using this approach.

    A possible alternative - which would involve a lot of recoding - would be to save the document to the Word 2003 XML file format, then work with the WordProcessingML. You'd do this with the standard .Net Framework XML tools. You'd have to convert the Excel data to valid WordProcessigML for a table, then drop that into the document. The finished product should open a lot faster than the processing being done in the UI.


    Cindy Meister, VSTO/Word MVP
    Monday, October 31, 2011 3:24 PM
    Moderator
  • Hi yubeck,

    You can use LINK fields in the Word document to point to static Excel ranges.

    If you give the Excel ranges a defined name, you can modify the LINK fields in the Word document to point to those ranges.

    The first is useful if the values in a static range might change. The second is useful if the range itself might change.

    Then, provided Word is configured with the 'Update Automatic Links at open' option, any changes to the Excel ranges will automatically be reflected in Word. No vba or Word bookmarks required.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, November 2, 2011 1:33 AM
  • Hi Paul

    And have you ever checked whether that gives better performance laying out pages in Word than copying and pasting? Word is still going to have to calculate the page layout...


    Cindy Meister, VSTO/Word MVP
    Wednesday, November 2, 2011 6:52 AM
    Moderator
  • Hi Cindy,

    Of course Word will still have to calculate the page layout. From an end-user perspective, though, opening a document with automatic links may indeed represent an improvement over having to tell Word allow a macro to run whenever the document is opened, then wait while it executes.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, November 2, 2011 7:30 AM
  • Hi Paul,

    Thanks for your suggestion!

    I had not mentioned that our users do not want automatic updates. They want to be able to specify when to update and item by item.

    We have accepted the overhead of having code run and working with bookmarks. The Excel ranges are being inserted into Word as Pictures and Word Tables. We also insert Excel charts as pictures. Our client dissatisfaction with performance comes in when they have 20+ such items (sometimes as high as 50) and, upon occasion, they want them to update all at once. It seems in this scenario Word itself gets very busy. "Pagination" appears in the status bar. We are wondering if there are other things Word is doing that is taking time. More importantly, if there were a way to say to Word 'don't recalculate or paginate until we have finished refreshing all 20+ items' I think that would solve the problem. I suspect that each individual item we refresh (re-insert) is causing Word to recalculate and having that happen in a loop of 20+ times is inefficient.

    Thanks again!

     

    Wednesday, November 2, 2011 4:23 PM
  • Hi yubeck,

    Having 'pagination' appearing in the statusbar while you're doing the updates suggests you're:
    1. not using Application.ScreenUpdating = False; and/or
    2. selecting the bookmarked ranges before updating them, rather than simply specifying them as the ranges to update.

    Turning off ScreenUpdating at the start of the update procedure (and turning it back on again at the end) can make the code run much faster, and using ranges instead of selections is also much more efficient.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, November 2, 2011 10:49 PM