检查样式名称或将样式名称VSTO Word VB.NET
我用Word VBA编写了以下代码,它可以正常工作检查样式名称或将样式名称VSTO Word VB.NET,vb.net,ms-word,vsto,word-style,Vb.net,Ms Word,Vsto,Word Style,我用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 n
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
我将该代码转换为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
但是当我运行时,在下面的行中,它给出了一个错误
如果Para.Style=cmbStyle1.SelectedItem.ToString,则
我该怎么办?Word中的属性是该类型的变体。您必须引用该字符串
例如:
If para.Style.NameLocal = cmbStyle1.SelectedItem.ToString Then
确保在所有过程中都包含错误捕获
使用PIAs这个词有时可能与VBA不同。当你使用VB.NET的时候不是很多,但是有时候有点 为了获得样式的名称,首先需要一个样式对象。比如说
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)
因此,您的代码需要类似于下面的代码。请注意,为某个范围指定样式时,无需创建样式对象。仅当获取样式的属性时
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
请编辑您的问题以包含错误消息或异常。我会这样做。但是“para.Style.ToString”return“system.\u comobject”值。
NameLocal
属性在属性列表中不可用。