PDA

View Full Version : [SOLVED:] CustomDocumentProperty anomoly really kicked my butt!



gmaxey
09-25-2014, 10:13 PM
I have spent the better part of two hours sitting surrounded by clumps of hair and bloody scalp over what appears to be a anomaly with CustomDocumentProperties.

It seems that simply modifying a property value with code isn't enough. You must also have a "quantifiable" change occur in the document.

To demo, create and save a new document and add the following code in a standard module:


Sub AutoOpen()
On Error Resume Next
MsgBox ThisDocument.CustomDocumentProperties("Testing")
On Error GoTo 0
End Sub
Sub CreateModCP()
Dim oCP As DocumentProperty
Dim strText As String
On Error Resume Next
Set oCP = ThisDocument.CustomDocumentProperties("Testing")
strText = InputBox("Enter text value", "Property Value", "text 1, 2, 3, 4")
If Err.Number <> 0 Then
Set oCP = ThisDocument.CustomDocumentProperties.Add(Name:="Testing", LinkToContent:=False, Value:=strText, Type:=msoPropertyTypeString)
Else
oCP.Value = strText
End If
MsgBox "See, the property value is changed to: " & ThisDocument.CustomDocumentProperties("Testing")
' ThisDocument.Variables("Save").Value = "False"
' ThisDocument.Variables("Save").Value = "True"
ThisDocument.Save
End Sub


Run the CreateModCP procedure, use the default inputbox value, then close and reopen the document. When the document opens you should get a msgbox reporting the property value.

Now run the CreateModCP procedure again and change the value in the input box. Close and reopen the document. If you see what I've seen for two hours, the property was not updated.

Now unstet the two lines in the CreateModCP procedure and run it again. Change the value in the input box, close then reopen the file.

You should see the CP now correctly updated.

Is this issue documented anywhere? Do you think this is bug or by design?

Thanks.

macropod
09-25-2014, 11:11 PM
You could set:
ThisDocument.Saved = False
after changing the value & before saving...

And, yes, I think the behaviour reflects a bug.

snb
09-26-2014, 01:25 AM
No problems encountered with:


Private Sub Document_Open()
MsgBox ThisDocument.CustomDocumentProperties("Testing")
End Sub

Sub CreateModCP()
On Error Resume Next

ThisDocument.CustomDocumentProperties("Testing").Value = InputBox("new value", "actual value: " & ThisDocument.CustomDocumentProperties("Testing"))
If Err.Number <> 0 Then ThisDocument.CustomDocumentProperties.Add "Testing", False, 4, InputBox("Enter text value", "Property Value", "text 1, 2, 3, 4")

MsgBox ThisDocument.CustomDocumentProperties("Testing"), , "Actual value"
ThisDocument.Close True
End Sub

gmaxey
09-26-2014, 05:12 AM
Paul, snb

Thanks for your input. I'll adapt my cobbled code with Paul's ThisDocument.Saved = False as in actual use I won't be closing the document with code.

snb, your method does work and it will even work if you remove the ThisDocument.Close line and manual Close and save changes with the UI. However, if you Save then Close with the UI the property change is not saved.

Very odd.

snb
09-26-2014, 08:25 AM
It is.

Apparently Word doesn't perceive the changing of a customdocumentproperty as a change in the Worddocument itself.

I 'solved' it changing something that Word does recognise as a 'real' change.


Sub CreateModCP()
on error resume next

With ThisDocument
.CustomDocumentProperties("Testing").Delete
.CustomDocumentProperties.Add "Testing", False, 4, InputBox("Enter text value", "Property Value", "text 1, 2, 3, 4")
.Variables("snb") = " "
.Save
End With
End Sub

gmaxey
09-26-2014, 11:43 AM
Apparently ;-)