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

huangapple go评论88阅读模式
英文:

How to set/edit/delete custom document properties in Word using vb.net

问题

我正在尝试将一个旧的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

'...

Try
  wordApp = New Word.Application ' CreateObject("Word.Application")
  oDoc = wordApp.Documents.Open(FileName:=someFile)
  With oDoc
    Dim cdps = .CustomDocumentProperties
    Try
      cdps("mc_ver").delete 'works
    Catch ex As Exception
    End Try
    Try
      'cdps.add("mc_ver", "0")
      '=============================
      ' the .add method produces the following error
      ' The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
      ' The following works!
      '=============================
      Dim typeDocCustomProps As Type = cdps.[GetType]()
      Dim oArgs As Object() = {"mc_ver", False, 1, 0}
      ' The following works
      typeDocCustomProps.InvokeMember("Add", BindingFlags.[Default] Or BindingFlags.InvokeMethod, Nothing, cdps, oArgs)
    Catch ex As Exception
    End Try
    For Each prop In .CustomDocumentProperties
      Try
        If prop.Name = "mc_ver" Then prop.Value = prop.Value + 2 'works
      Catch ex As Exception
        Console.WriteLine(ex.Message)
      End Try
    Next
    '=============================
    ' add character at the beginning of the doc and then delete it
    ' to be able to save the document?!
    .Range(0, 0).Select()
    .Application.Selection.TypeText("1")
    .Application.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
    .Application.Selection.Characters(1).Delete()
    .Save()
    .Close(False)
  End With
Catch ex As Exception
  Console.WriteLine(ex.Message)
  MsgBox(ex.Message)
End Try

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

编辑:

我已经更新了问题。我找到了如何实现编辑/删除部分的方法。问题在于,与“真正的”Word不同,Word COM对象不会保存文档,除非内容发生了更改!

但是,问题的设置(添加)部分仍然没有解决。

更新:

代码已经更新,通过提供的逻辑解决了设置/添加问题,具体请参考以下链接

英文:

I'm trying to translate an old Word VBA application into VB.Net (winForms/.Net Framework 4.8.1). So far I've been successfull, except for the following VBA code that deals with custom document properties of a Word document:

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

My simplified VB.Net code follows:

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
'Imports Microsoft.Office.Core
....
Try
  wordApp = New Word.Application ' CreateObject("Word.Application")
  oDoc = wordApp.Documents.Open(FileName:=someFile)
  With oDoc
    Dim cdps = .CustomDocumentProperties
    Try
      cdps("mc_ver").delete 'works
    Catch ex As Exception
    End Try
    Try
      'cdps.add("mc_ver", "0")
      '=============================
      ' the .add method produces the following error
      ' The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
      ' The following works!
      '=============================
      Dim typeDocCustomProps As Type = cdps.[GetType]()
      Dim oArgs As Object() = {"mc_ver", False, 1, 0}
      ' The following works
      typeDocCustomProps.InvokeMember("Add", BindingFlags.[Default] Or BindingFlags.InvokeMethod, Nothing, cdps, oArgs)
    Catch ex As Exception
    End Try
    For Each prop In .CustomDocumentProperties
      Try
        If prop.Name = "mc_ver" Then prop.Value = prop.Value + 2 'works
      Catch ex As Exception
        Console.WriteLine(ex.Message)
        End Try
    Next
    '=============================
    ' add character at the begining of the doc and then delete it
    ' to be able to save the document?!
    .Range(0, 0).Select()
    .Application.Selection.TypeText("1")
    .Application.Selection.HomeKey(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
    .Application.Selection.Characters(1).Delete()
    .Save()
    .Close(False)
  End With
Catch ex As Exception
  Console.WriteLine(ex.Message)
  MsgBox(ex.Message)
End Try

I'm not able to work with the collection of custom document properties. I've tried (probably) everything I could find on the net. In many cases the examples refer to Microsoft.Office.Core but I don't know how to use it. Perhaps because it is ActiveX type (not Assembly), and is for VSTO applications!?

EDIT:

I have updated the question. I figured it out how to implement edit/delete part of the problem. The catch is that Word COM object doesn't save the document (unlike the "real" Word) unless there are changes in the content!

However, the set (add) part of the question remains unresolved still.

UPDATE

The code has been updated and the set/add issue is solved using the logic provided in the following link

答案1

得分: 1

根据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"吗?

英文:

According to https://learn.microsoft.com/en-us/office/vba/api/word.document.customdocumentproperties the CustomDocumentProperties collection is a member of the Document object, not the Application. So have you tried changing "For Each prop In oDoc.Application.CustomDocumentProperties" to "For Each prop As DocumentProperty In oDoc.CustomDocumentProperties"?

答案2

得分: 0

经过更多的研究,我在以下链接Update1找到了与Interop.Excel类似问题的解决方案。我采用了这种方法,并使用可工作的代码更新了问题。

英文:

After more research I found a solution to a similar problem with Interop.Excel at the following link, Update1. I used that approach, and updated the question with a working code.

huangapple
  • 本文由 发表于 2023年8月10日 16:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定