简体   繁体   中英

Detect built-in normal style at cursor in Word using VSTO VB.NET

To make a context-sensitive toolbar (buttons that are activated only if the cursor is on certain styles of paragraphs), I want to determine if the paragraph at the cursor is Microsoft.Office.Interop.Word.WdBuiltinStyle.wdStyleNormal (which is a constant of value of -1). My code needs to work even if the user's default style is "Normal" in some other language than English. The variants of the paragraph style names can be found here .

Here's a related question that seems to imply it's not possible to get style information without it being a string.

I have code to find the current style name as a string:

Private Function getSelectionStyle() As String
    Return CType(Globals.ThisDocument.Application.Selection.Paragraphs.Style, Word.Style).NameLocal
End Function

How can I get the integer value to compare it with WdBuiltinStyle.wdStyleNormal (the language-independent style value of Normal)?

Simply compare with the Normal style instance from the collection of styles in your document:

Public Function IsNormalStyle(ByVal style As style, ByVal Document As Document) As Boolean

    Dim normalStyle As style = document.Styles(Word.WdBuiltinStyle.wdStyleNormal)
    Return normalStyle = style

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM