PDA

View Full Version : [SOLVED:] Custom Document Properties Don't Always Save



dorkboy
12-13-2007, 10:04 AM
After users create a Document in Word, they run a Macro that brings up a form that collects some basic information (name, dept, etc.) These values from the form are written to the file's Custom Document Properties.

Code looks like:

ActiveDocument.CustomDocumentProperties.Add Name:="Department", _
LinkToContent:=False, Type:=msoPropertyTypeString, Value:=strDept

Everything works fine when the users Saves the Document.

Code looks like:

ActiveDocument.Save


Problem:

User changes a value on the form WITHOUT making any changes to the document. The file's Custom Document Properties do NOT reflect the change(s) made by the user.

It is almost like Word does not see any changes to the Document so therefore, it will not change the file's Custom Document Properties.

Any suggestions?

Thanks in advance.

fumei
12-13-2007, 11:14 AM
This is one of the slightly peculiar things with Word.

Department is an "existing" CustomProperty. However, it does not really exist unless it has a Value. It exists as Null. In other words:


ActiveDocument.CustomDocumentProperties("Department") _
.Value = strDeptwill fail IF it has not been previous set to a string.

If it has been previously set to a string (by using the .Add of your code), then the above code will indeed work.

So....

Sub MyCustom()
Dim strDept As String
strDept = InputBox("Blah blah blah")
On Error GoTo AddIt
ActiveDocument.CustomDocumentProperties("Department") _
.Value = strDept
Exit Sub
AddIt:
ActiveDocument.CustomDocumentProperties.Add _
Name:="Department", _
LinkToContent:=False, Type:=msoPropertyTypeString, _
Value:=strDept
End Sub

What happens:

1. code tries to set the Value of "Department"

2. IF "Department" is not Null - ie. it has been previously set - then there is no error, and "Department" is set as strDept.

3. If there IS an error - ie. it has not been previously set - the code jumps to the error part of the code. This uses .Add to set the value.

fumei
12-13-2007, 11:17 AM
Note: you still have to do a Save to actually save the change!

dorkboy
12-13-2007, 12:08 PM
Thanks fumei. I understand exactly what you are saying.

However, I don't think this is my problem. I forgot to mention that I clear all Custom Document Properties BEFORE I save the new values. I have set a breakpoint and can see that the Custom Document Properties have been removed.

I remove "old" Custom Document Properties
I write "new" Custom Document Properties
Do a(n) ActiveDocument.Save

If nothing has changed in the Word Document, these values are NOT saved!

TonyJollans
12-13-2007, 12:19 PM
Interesting. I hadn't realised that changed properties were not considered as changes to the document. I presume this is because they are a separate stream in the file that can be manipulated outside Word (e.g. with dsofile) and not considered part of the document proper, rather just something that is stored with it in the same container. That said, the problem persists in 2007 where properties could not be said to be different from any other component of the zip package and where dsofile no longer works and where any component of the package can be manipulated independently.

Anyway, if you are setting your property in code, just add this line after it and Word will think the document then needs saving:



ActiveDocument.Saved = False

dorkboy
12-13-2007, 12:37 PM
Tony,

Thank you so much. That was all that I needed!