Search code examples
vb.netms-wordoffice-interop

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


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


Solution

  • 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.