Word Interop(COM)样式LanguageID不起作用

问题描述 投票:0回答:1

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

当访问样式时,它不返回任何内容,因此,当尝试访问LanguageID时,它将失败。

        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

错误:s.LanguageID ='s.LanguageID'引发了类型异常'System.Runtime.InteropServices.COMException'

没有值的样式(空)

enter image description here

但是我可以检索Style.Count吗?

vb.net ms-word styles interop comexception
1个回答
0
投票
未定义样式。您没有定义什么对象样式。由于后期绑定,您的代码无法确定您的意思是单词风格。因此,您看到的错误。

[WdLanguageID.wdEnglishUK是一个Word常量,其值为2507(&H809),但是由于绑定晚,您的代码将不知道这一点,而您将获得值为0。

为正确定义Style和WdEnglishUK的两个问题的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.