如何使用 vb.net 在 Word 中设置/编辑/删除自定义文档属性

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

我正在尝试将旧的 Word VBA 应用程序转换为 VB.Net (winForms/.Net Framework 4.8.1)。到目前为止,我已经成功了,除了以下处理 Word 文档的自定义文档属性的 VBA 代码:

ActiveDocument.CustomDocumentProperties.Add Name:="mc_ver", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=0
ActiveDocument.CustomDocumentProperties("mc_ver").Value = 1
ActiveDocument.CustomDocumentProperties("mc_ver").Delete

我的简化 VB.Net 代码如下:

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
'Imports Microsoft.Office.Core
....
Try
  wordApp = New Word.Application
  oDoc = wordApp.Documents.Open(FileName:=someFile) : oDoc.Activate()
  'Dim mocCustProperties As Microsoft.Office.Core.DocumentProperties
  With oDoc
    Try
      For Each prop In oDoc.Application.CustomDocumentProperties
        Console.WriteLine(prop.name.ToString & " " & prop.Value)
      Next
    Catch ex As Exception
      Console.WriteLine(ex.Message)
      oDoc.Close(False)
      wordApp.Quit(False)
      Exit Function
    End Try
  End With
  oDoc.Close(False)
Catch ex As Exception
  Console.WriteLine(ex.Message)
  MsgBox(ex.Message)
End Try

我无法获取自定义属性的集合。我已经尝试了(可能)我能在网上找到的所有方法。在许多情况下,示例引用 Microsoft.Office.Core 但我不知道如何使用它。也许是因为它是ActiveX类型(不是Assembly),并且是针对VSTO应用程序的!?

vb.net ms-word office-interop
1个回答
0
投票

根据 https://learn.microsoft.com/en-us/office/vba/api/word.document.customdocumentproperties CustomDocumentProperties 集合是 Document 对象的成员,而不是 Application 的成员。那么您是否尝试过将“For Each prop In oDoc.Application.CustomDocumentProperties”更改为“For Each prop As DocumentProperty In oDoc.CustomDocumentProperties”?

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