vbams-wordobject-model

Reading the ParagraphFormat.Style property of a Style object crashes Word every time


Word 365 ProPlus v1908 build 11929.20776 on a Win10Pro(1903, build 18362.30) machine with i7-6600@2.6GHz,16G ram,

I'm trying to compare range object formatting to style definitions (flagging formats that are applied without using styles). When I try to read a style object (e.g., "Normal" style), the "Style" property of the "ParagraphFormat" object causes word to crash every time:

Set vPropVal=ActiveDocument.Styles("Normal").ParagraphFormat.Style or

Set vPropVal=CallByName(ActiveDocument.Styles("Normal").ParagraphFormat,"Style",VbGet) (although using variables for the source object and string property name)

If I add a Watch for ActiveDocument.Styles("Normal"), and then try to expand the ParagraphFormat property, Word crashes.

If I try to run Debug.Print ActiveDocument.Styles("Normal").ParagraphFormat.Style.NameLocal in a module or in the Immediate window, Word Crashes.

I can (and have been) just skip over the Style property of the ParagraphFormat, but it's bugging having the problem and not knowing why or how to fix it.

I have not been able to find any web resources that provide insights into why the ParagraphFormat of a Style object might be problematic. I greatly appreciate any insights...

BTW - I'm not a professional coder; I just have some intermediate capabilities.


Solution

  • The crash occurs because of an error (bug) in Word that has been there since Word 2010. When you try to open the ParagraphFormat branch of the Style object tree in a Watch (or Locals) Window, Word will try to enumerate all the members and their values, and will fall foul of the same problem.

    But the .Style value was not available from the ParagraphFormat property of a Paragraph Style object even before that. Even if it was, it's not unreasonable to expect that it would point to the same Style object as you are inspecting. If you need the NameLocal of the Style you can get it directly from ActiveDocument.Styles("stylename").NameLocal

    In Word 2007, the same code to access .Style would not cause a crash but would raise error 91 ("Object Variable or With block variable not set"), and inspection in the Watch or Locals windows would tell you that the Style property was set to Nothing.

    [FWIW Mac Word 2011 raises a different error - 5934, "This operation is not supported by a duplicated ParagraphFormat object."]