Word object model


Word 2013 can now link directly to a “Live ID” account, which includes things like your Office subsription and your SkyDrive account. If you’re hooked up, your account name appears at the right of the application title bar. This functionality is hooked up to the new Comments and Track Changes functionality.

Depending on the activation of an option in “Personalize Microsoft Office” section of the General tab in the Options dialog box, this information will be used to identify the person reviewing a document, or the initials and name given in the options will be used (as in previous versions of Word).

The object model equivalent of the checkbox “Always use these values regardless of sign in to Office” is: Application.Options.UseLocalUserInfo, which returns True/False.

As of May 2013, this remains undocumented in the on-line Help.


The new IsInAutoSave property of the Document object lets us determine whether the DocumentBeforeSave event was triggered by an AutoSave. This property is documented and is of data type Boolean (meaning True/False).

What is not documented, however, is that the numeric value assigned to True is not -1, as has always been used for the Office object model, but 1. VBA continues to use -1 for True. This is more in-line with what .NET developers expect, but makes the Office object model internally inconsistent.

So you run into the situation that, if the VBA developer tests for True in his code, he gets the wrong result:
If Document.IsInAutoSave = True Then
will actually trigger when the Save was NOT triggered by AutoSave.

The VBA developer needs to use either of the two following formulations:
If Document.IsInAutoSave = 1 Then
If Document.IsInAutoSave Then

Or he can test against the False value, which remains 0 in all programming languages.

5 Responses to “Word object model”

  1.   Mark Says:

    Hi Cindy,
    Hoping you can help. I have a custom Word add-in that scans the current document and applies styles to paragraphs based on their content. The problem I am encountering is when I apply a custom paragraph style (via code/addin) to the paragraph, any existing styles (stricken, underline) applied to various text/words within that paragraph are lost – usually. This does not always happen but does most of the time. However, when I manually apply that same style to a paragraph using the Word interface, the paragraph style is applied to the paragraph as desired and any underscored or stricken text remains, which is what I need.

    To get around this I am having to cache the font for every character in the paragraph, apply the paragraph style to the paragraph, and then reapply the character style (stricken, underscore) to each character within the paragraph. As you can imagine, this is very slow and inefficient and is tolerable on small documents, but painfully slow when documents are 50, 100+ pages in length.

    *code snip*
    Word.Paragraph p = range.Paragraphs.First;

    // Save the font for every character in the paragraph
    List fonts = new List(p.Range.Characters.Count);

    foreach (Word.Range eaChar in p.Range.Characters)
    fonts.Add(new FontCache(eaChar.Font));

    // Apply style to paragraph
    object name = styleName;
    Word.Style s = range.Document.Styles[styleName];

    // styles are then reapplied to each character.
    *code snip end*

    Any help/guidance is greatly appreciated.

    Mark Wagner
    mark *at* crsw *dot* com

    •   WordMeister Says:

      Hi Mark
      Many, many years ago I was privileged to sit in on a discussion with the Word team where a question like this came up, i.e.: How can we predict when Word will replace character-level formatting when a paragraph style is applied. The answer was that you can’t, really, as the algorithm is complex.
      You can try asking this in the Word for Developers forum – someone out there may have worked out something more efficient than what you’ve come up with (although nothing occurs to me, off-hand): http://social.msdn.microsoft.com/Forums/office/en-US/home?forum=worddev

      My recommendation, however, would be to work with the Word Open XML directly, which would allow you to replace the reference to the style directly in the underlying XML. That wouldn’t touch the character-level formatting and would be blindingly fast compared to automating via the “Interop”. Only drawback would be that, in order to use the Open XML SDK the file would have to be closed, not opened in Word. Word can, however, write valid Word Open XML directly into an opened document via the Range.InsertXML method (and read the Range using Range.WordOpenXML). So it could be incorporated into your add-in…

  2.   Dawid Says:

    Hi Cindy,

    Is there a way to iterate thru styles used in document like iterating thru paragraphs? And if not, is there a way to create own containers where we can put ranges? I want to create parser based on Word content and i wonder if there is any better way than iterating thru words, which is really slow.

Leave a Reply