Roman Benko

I am working with VSTO, Orcas, and Word 2007. I set a customDocumentProperty called AuditID and save the document. When I close the document and go back into it, the custom property no longer exists. But if I ¡°Save As¡± to a different document name and then open the new document, it DOES exist. I have two command buttons that I have set up on a ribbon that perform the Set and Display functionality. The code for these two buttons is shown below.

Private Sub btnSetAuditID_OnAction(ByVal sender As System.Object, ByVal e As Microsoft.VisualStudio.OfficeTools.Ribbon.ComponentModel.ControlEventArgs) Handles btnSetAuditID.OnAction

'prompt the user for the Audit ID

Dim newID As Integer = InputBox("Audit ID:")

'If the auditID exists, update it, else add it

Try

Dim prop As Office.DocumentProperty

Dim found As Boolean = False

For Each prop In Globals.CastleDocument.CustomDocumentProperties

If prop.Name = "AuditID" Then

prop.Value = newID

found = True

End If

Next

If Not found Then

'add the new property

Globals.CastleDocument.CustomDocumentProperties.add("AuditID", False, Office.MsoDocProperties.msoPropertyTypeNumber, newID)

End If

'save the document

Globals.CastleDocument.Save()

MsgBox("Audit ID " & newID & " was saved")

'if I exit the app now and come back in, it will not be there

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Private Sub btnDisplayAuditID_OnAction(ByVal sender As System.Object, ByVal e As Microsoft.VisualStudio.OfficeTools.Ribbon.ComponentModel.ControlEventArgs) Handles btnDisplayAuditID.OnAction

'display the value of the custom property

Try

Dim prop As Office.DocumentProperty

Dim found As Boolean = False

For Each prop In Globals.CastleDocument.CustomDocumentProperties

If prop.Name = "AuditID" Then

found = True

MsgBox("Value of AuditID is " & prop.Value)

End If

Next

If Not found Then

MsgBox("AuditID property was not found")

End If

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub



Re: Visual Studio Tools for Office CustomDocumentProperty not working properly?

Dennis Wallentin

Hi Roman,


I am working with VSTO, Orcas, and Word 2007.

Is it the beta version of VS 2008

I don't work with Word (not even when I write) so it's beyond my knowledge but You can test after You have added AuditID to the document but before saving it if it exist or not. BTW, do You get any error message






Re: Visual Studio Tools for Office CustomDocumentProperty not working properly?

Ji Zhou ¨C MSFT

Hi Benko,

I think the problem is: the statement below does not save the document in fact.

Code Snippet
Globals.CastleDocument.Save()

Document.Save() method save the document only when Document.Saved is false. After the document opened, if we only add "AuditID" to DocumentProperties by clicking the corresponding button, not change any content of the document. Document.Saved stands to be true by default. So Save() method does not save the document. Try these codes:

Code Snippet

Private Sub btnSetAuditID_OnAction(ByVal sender As System.Object, ByVal e As Microsoft.VisualStudio.OfficeTools.Ribbon.ComponentModel.ControlEventArgs) Handles btnSetAuditID.OnAction

'prompt the user for the Audit ID

Dim newID As Integer = InputBox("Audit ID:")

'If the auditID exists, update it, else add it

Try

Dim prop As Office.DocumentProperty

Dim found As Boolean = False

For Each prop In Globals.CastleDocument.CustomDocumentProperties

If prop.Name = "AuditID" Then

prop.Value = newID

found = True

End If

Next

If Not found Then

'add the new property

Globals.CastleDocument.CustomDocumentProperties.add("AuditID", False, Office.MsoDocProperties.msoPropertyTypeNumber, newID)

End If

'save the document

Globals.CastleDocument.Saved = false

Globals.CastleDocument.Save()

MsgBox("Audit ID " & newID & " was saved")

'if I exit the app now and come back in, it will not be there

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Thanks

Ji