none
Style object slowness on Word 2013 RRS feed

  • Question

  • I developed a Word add-in that needs to read the Style object of each character of the document body. The add-in is working, but in Word 2013 it executes extremely slow. I tested in Word 2007 and Word 2010 and it works alright.


    So I decided to do a test. Given a small text, consider this small piece of code:

    Sub fncSpeedTest()
      Dim prg As Word.Paragraph
      Dim rng As Word.Range
      Dim objStyle As Word.Style
      Dim objBorders As Word.Borders
      Dim sngTime As Single
      
      sngTime = VBA.Timer
      For Each prg In ActiveDocument.Content.Paragraphs
        For Each rng In prg.Range.Characters
          'Test 1: <nothing>
          
          'Test 2:
          'Set objBorders = rng.Borders
          
          'Test 3:
          'Set objStyle = rng.Style
        Next rng
      Next prg
      
      VBA.MsgBox VBA.Format((VBA.Timer - sngTime) * 1000, "#,##0") & " milliseconds.", vbInformation
    End Sub

    Test 1 just run the entire document. I got 238 milliseconds.

    Test 2 accesses the borders object of each character, I got 313 milliseconds. That's fair.

    Test 3 reads the style object of each character, and I got a large number: 51441 milliseconds.

    Do anyone know about this slowness on Word 2013 or there is something I'm missing?

    What results you gort in 2007, 2010 or 2013 versions?

    ---

    By the way, if you want to see the add-in, it is free and open source, the link is http://www.ambienteoffice.com.br/word/word2html/ and the download link is at the bottom of the page. I hope you understand the page after automatic translation. The purpose of the add-in is to convert document body text into HTML body elements, but in a cleaner way than Word saves to HTML. My e-mail is at the bottom of the page. Any feedbacks would be appreciated.


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br

    Sunday, December 29, 2013 2:25 PM

Answers

  • I tested your code in Word 2010 against a document with the following characteristics:

    Several of the paragraphs had borders.

    Test 1 took 203 ms, Test 2 took 281 ms and Test 3 took 1,500 ms. So referring to Style is much slower than referring to Borders, but not by far as slow as you experience in Word 2013...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, December 29, 2013 7:51 PM
  • Thank you, Hans. On my Word 2010 computer I got respectively 94, 129 and 688 milliseconds. I even formatted my Office 2013 PC to redo the test, but I experienced the slowness again. I have Office 2007 on my work's computer and I can confirm that the code runs fast in Word 2007.

    This issue remembered me the Excel Sheet Protect/Unprotect method, which got much times slower in Excel 2013.

    Well, I'll just consider this "by design" unless someone give a different shoot.


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br


    Monday, December 30, 2013 12:28 PM

All replies

  • I tested your code in Word 2010 against a document with the following characteristics:

    Several of the paragraphs had borders.

    Test 1 took 203 ms, Test 2 took 281 ms and Test 3 took 1,500 ms. So referring to Style is much slower than referring to Borders, but not by far as slow as you experience in Word 2013...


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, December 29, 2013 7:51 PM
  • Thank you, Hans. On my Word 2010 computer I got respectively 94, 129 and 688 milliseconds. I even formatted my Office 2013 PC to redo the test, but I experienced the slowness again. I have Office 2007 on my work's computer and I can confirm that the code runs fast in Word 2007.

    This issue remembered me the Excel Sheet Protect/Unprotect method, which got much times slower in Excel 2013.

    Well, I'll just consider this "by design" unless someone give a different shoot.


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br


    Monday, December 30, 2013 12:28 PM
  • Hello Felipe,

    I would recommend storing Word objects locally and not requesting them each time in the loop:

    For Each prg In ActiveDocument.Content.Paragraphs
        For Each rng In prg.Range.Characters
    
    --------------------------------------------------
    
    Dim document as Word.Document
    Dim content as Word.Range
    Dim pars as Word.Paragraphs
    Set document = ActiveDocument
    Set content = document.Content
    Set pars = content.Paragraphs
    
    For Each prg In pars
        Dim paRange as Word.Range
        Dim chars as Word.Characters
        Set paRange = prg.Range
        Set chars = paRange.Characters
        
        For Each rng In chars
       

    Monday, December 30, 2013 2:07 PM
  • I have definitely noticed a distinct slowdown in processing against the Word collections in Office 2013. For instance, any iterations/looping of document paragraphs, ranges, headers/footers, etc.  

    I am referring to already-optimized-as-much-as-possible code which runs acceptably fast when running in Word 2007 and Word 2010. It is MUCH slower when run with Word 2013. 

    Any insights into why this notable decrease in performance between releases of Word would be greatly appreciated.

    PS - I do not have any specific code examples to post, but Felipe's original post above is a perfect, measurable example of what I am referring to.

    Monday, May 12, 2014 7:15 PM
  • A workaround for now is to uncheck both bottom zoom controls when you right click at Word's status bar:


    Felipe Costa Gualberto - http://www.ambienteoffice.com.br

    Monday, May 12, 2014 9:35 PM
  • Thank you - I will try that to see if it brings any improvement.
    Monday, May 12, 2014 10:40 PM
  • It's a known behavior but, as far as I know, Microsoft hasn't revealed anything about the "why" or how to speed things up. If user interaction is not required, fastest will be to work on the closed file, leveraging the Open XML file format.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 13, 2014 10:19 AM
    Moderator