简体   繁体   English

检查样式名称或获取样式名称VSTO Word VB.NET

[英]Check style name or get style name VSTO Word VB.NET

I wrote the following code in Word VBA and it works. 我在Word VBA中编写了以下代码,并且可以正常工作。

Dim para As Paragraph
Dim nextPara As Paragraph
For Each para In ActiveDocument.Paragraphs
    If para.Style = CMB1.Value Then
        Set nextPara = para.Next
        If nextPara.Style = CMB2.Value Then
            If Not nextPara Is Nothing Then
                para.Style = CMB3.Value
                nextPara.Style = CMB4.Value
            End If
        End If
    End If
Next

I converted that code to VSTO VB.NET: 我将该代码转换为VSTO VB.NET:

    Dim para As Word.Paragraph
    Dim nextPara As Word.Paragraph

    For Each para In activeDoc.Paragraphs
        If para.Style = cmbStyle1.SelectedItem.ToString Then
            nextPara = para.Next
            If nextPara.Style = cmbStyle2.SelectedItem.ToString Then
                If Not nextPara Is Nothing Then
                    para.Style = cmbStyle3.SelectedItem.ToString
                    nextPara.Style = cmbStyle4.SelectedItem.ToString
                End If
            End If
        End If
    Next

But when I run, in the following line, it gives an error. 但是当我运行时,在下一行中,它给出了一个错误。

If Para.Style = cmbStyle1.SelectedItem.ToString Then 如果Para.Style = cmbStyle1.SelectedItem.ToString然后

What should I do? 我该怎么办?

The Paragraph.Style Property in Word is a Variant of the WdBuiltinStyle type. Word中的Paragraph.Style属性是WdBuiltinStyle类型的变体。 You'll have to reference the string Paragraph.Style.NameLocal . 您必须引用字符串Paragraph.Style.NameLocal

Example: 例:

If para.Style.NameLocal = cmbStyle1.SelectedItem.ToString Then

Be sure to include error trapping in all your procedures. 确保在所有过程中都包括错误陷阱。 Here is an example for .NET 这是.NET的示例

Working with the Word PIAs can differ, sometimes, from VBA. 有时,使用Word PIA可能与VBA不同。 Not a lot when you work with VB.NET, but sometimes a bit... 使用VB.NET时不多,但有时有点...

In order to get the style's name you first need a Style object. 为了获得样式的名称,您首先需要一个Style对象。 For example 例如

    Dim para As Word.Paragraph = Globals.ThisAddIn.Application.Selection.Range.Paragraphs(1)
    Dim styl As Word.Style = para.Range.Style
    System.Diagnostics.Debug.Print(styl.NameLocal)

So your code would need to be something like the code that follows. 因此,您的代码将需要类似于以下代码。 Note that it's not necessary to create a Style object in order to assign a style to a Range. 请注意,不必为将样式分配给Range而创建Style对象。 Only when getting the style's properties. 仅在获取样式的属性时。

Dim para As Word.Paragraph
Dim nextPara As Word.Paragraph
Dim paraStyle as Word.Style
Dim paraStyleNext as Word.Style

For Each para In activeDoc.Paragraphs
    paraStyle = para.Style
    If paraStyle.NameLocal = cmbStyle1.SelectedItem.ToString Then
        nextPara = para.Next
        paraStyleNext = nextPara.Style
        If paraStyleNext.NameLocal = cmbStyle2.SelectedItem.ToString Then
            If Not nextPara Is Nothing Then
                para.Style = cmbStyle3.SelectedItem.ToString
                nextPara.Style = cmbStyle4.SelectedItem.ToString
            End If
        End If
    End If
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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