CustomDocumentProperty not working correctly

D

Dhananjay

Hi All,
I am developing word Addin using VB 2005 / VSTO / Office 2007.
I want to write some CustomDocumentProperties to the document. So I
used the code snippet below
-----------------------------------------------------------

doc = Application.ActiveDocument
Dim myProperties(,) As String = {{"ID", "ID1"}, {"NAME",
"Name1"}}
prps = doc.CustomDocumentProperties
For i As Int16 = 0 To myProperties.GetUpperBound(0)
prps.Add(myProperties(i, 0), False,
Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
myProperties(i, 1))
Next
doc.Save ' Save as File.Docx
' Copy File from doc.FullName to some other folder
dim myFolder = "c:\temp\"
File.Copy doc.FullName,myFolder

' Open document from myFolder
dim myTempDoc as Document = Application.Documents.Open
(myFolder & "File.docx")
myTempDoc.CustomDocumentProperties("Name").value= "Name
changed"
myTempDoc.Save
myTempDoc.Close

-----------------------------------------------------------
But when I open myTempDoc again, it shows me value for Name & ID as
"Name1" & "ID1"

Please help me to resolve this problem.

Thanks in advance,
Dhananjay
 
P

Peter Jamieson

I believe that what happens in this case is
a. you add the properties
b. the properties appear to have been created
c. however, the properties are not actually saved when you save the
document.

This seems to be because adding a custom property does not appear to be
enough to "dirty" the document. If you actually do anything like add a
bit of text into the document body, the new properties will be saved.
There may a correct way to mark the document as dirty but if not I guess
there may be slightly less destructive ways to do it than that.

In fact if you
a. programmatically add the properties
b. finish there, leaving the document open in Word
c. inspect the custom properties in Word - you will see them there
d. close the document manually

then the properties will disappear.

This is not how things appear to work when you add these properties in
VBA so I assume that there is some fault in the way that the Word
Interop stuff handles custom properties.

Peter Jamieson

http://tips.pjmsn.me.uk
 
L

Lorenz Hölscher

Hi Dhananjay,

Peter said:
This seems to be because adding a custom property does not appear to be
enough to "dirty" the document. If you actually do anything like add a
bit of text into the document body, the new properties will be saved.
There may a correct way to mark the document as dirty but if not I guess
there may be slightly less destructive ways to do it than that.

There is an easy way to force Word to save the doc (as this is a known
problem). Right before saving the document you make Word think that
it's unsaved:

myTempDoc.Saved = False
myTempDoc.Save

Hope this helps,
Lorenz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top