Word Interop (COM) 样式语言 ID 不起作用



我有各种文档具有某些样式,并且具有不正确的Style.LanguageID。 我有一些代码应该遍历文档中的所有样式,并将每个 LanguageID 设置为 EnglishUK。

解决:

oWord = CreateObject("Word.Application")
If oWord IsNot Nothing Then
oWord.Visible = False
If oWord.Documents IsNot Nothing Then
Try
oDocument = oWord.Documents.Open(strFilePath)
Catch ex As Exception
iCouldntOpen += 1
bFailedToOpen = True
End Try
If Not bFailedToOpen Then
If oDocument IsNot Nothing Then
If oDocument.ReadOnly Then
iReadonly += 1
ElseIf oDocument.HasPassword Then
iPassword += 1
Else
For Each s As Style In oDocument.Styles
s.LanguageID = WdLanguageID.wdEnglishUK
Next
' save the document
oDocument.Save()
End If
' close the document
oDocument.Close()
oDocument = Nothing
End If
oWord.Quit()
oWord = Nothing
bFailedToOpen = False
End If
Else
iUnknown += 1
End If
Else
iUnknown += 1
End If

使用 Style.Type 获取适当的 Style。

oWord = CreateObject("Word.Application")
If oWord IsNot Nothing Then
oWord.Visible = False
If oWord.Documents IsNot Nothing Then
Try
oDocument = oWord.Documents.Open(strFilePath)
Catch ex As Exception
iCouldntOpen += 1
bFailedToOpen = True
End Try
If Not bFailedToOpen Then
If oDocument IsNot Nothing Then
If oDocument.ReadOnly Then
iReadonly += 1
ElseIf oDocument.HasPassword Then
iPassword += 1
Else
For Each s As Style In oDocument.Styles
Select Case s.Type
Case WdStyleType.wdStyleTypeCharacter, WdStyleType.wdStyleTypeParagraph, WdStyleType.wdStyleTypeTable
s.NoProofing = False
s.LanguageID = WdLanguageID.wdEnglishUK
End Select
Next
' save the document
oDocument.Save()
End If
' close the document
oDocument.Close()
oDocument = Nothing
End If
oWord.Quit()
oWord = Nothing
bFailedToOpen = False
End If
Else
iUnknown += 1
End If
Else
iUnknown += 1
End If

您正在将 Word 应用程序创建为后期绑定对象。 这意味着您将无法访问代码中的 Word 智能感知。 其含义如下

样式未定义。 您没有定义对象样式是什么。 由于后期绑定,代码无法确定您的意思是单词样式。 因此,您看到的错误。

WdLanguageID.wdEnglishUK 是一个 Word 常量,其值为 2507 (&H809(,但由于后期绑定,您的代码将不知道这一点,相反,您的值为 0。

解决您的问题,以正确定义样式和WdEnglishUK。

最新更新