Custom Document Properties dod not Persist

D

David Taylor

My Access app includes a subroutine that sets custom document properties in
the following manner -

The code below tries to retrieve a custom document property:

wrdDoc.CustomDocumentProperties(strPropName) = varPropVal

If the property does not exist the error handler sets the bolNewProp flag to
true:

Err_SetDocProps:
Select Case Err
Case 5
bolNewProp = True
Resume Next

which causes the custom property to be added:

wrdDoc.CustomDocumentProperties.Add Name:=strPropName, _
Type:=intPropType, _
Value:=varPropVal, _
LinkToContent:=False

This code runs without untrapped errors and I can even see the added custom
property:

?wrddoc.customdocumentproperties("MyVersionDate").value

But when I open the word document (or view its properties in Explorer) I
find that the custom property that was added has disppeared. I would
appreciate comments, commiserations and, yes a
little help.

Thanks,

DT

Sub SetDocProps(strDoc As String, strPropName As String, varPropVal As
Variant)
Dim intPropType As Integer
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim bolNewProp As Boolean

On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = New Word.Application
End If
On Error GoTo Err_SetDocProps

Select Case VarType(varPropVal)
Case vbInteger, vbLong
intPropType = msoPropertyTypeNumber
Case vbBoolean
intPropType = msoPropertyTypeBoolean
Case vbDate
intPropType = msoPropertyTypeDate
Case vbSingle, vbDouble
intPropType = msoPropertyTypeFloat
Case vbString
intPropType = msoPropertyTypeString
End Select

wrdApp.Visible = False
bolNewProp = False
Set wrdDoc = Word.Documents.Open(FileName:=strDoc)
wrdDoc.CustomDocumentProperties(strPropName) = varPropVal
If bolNewProp Then
wrdDoc.CustomDocumentProperties.Add Name:=strPropName, _
Type:=intPropType, _
Value:=varPropVal, _
LinkToContent:=False
End If
Exit_SetDocProps:
wrdDoc.Save
wrdDoc.Close
Set wrdApp = Nothing
Exit Sub

Err_SetDocProps:
Select Case Err
Case 5
bolNewProp = True
Resume Next
Case 5174
MsgBox strDoc & " is not a valid file name.", vbOKOnly, "DocuMAN
Error"
Resume Exit_SetDocProps
Case Else
MsgBox Err.Description
Resume Exit_SetDocProps
End Select

End Sub
 
D

Doug Robbins - Word MVP

If you do not need to be able to see the Custom Document Property, but
accessing the value that you are assigning to it by the use of VBA will
suffice, I would use Document Variables instead. You can cause values
assigned to them to be displayed in the document by the use of {
DOCVARIABLE } fields, but the user cannot, other than by the use of VBA,
change the value assigned to the variable.

I have never found the need to use Custom Document Properties.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

David Taylor

Works fine. Thanks.

DT
BTW Why doesn't Document.CustomDocumentProperties.Add work ?
 
D

David Taylor

Word will not save a document unless it is "dirty" or has been changed. It
seems that Word does not recognize the addition of a CustomDocumentProperty
as a change and therefore does not save the document.
This problem was solved by changing the document (eg. delete a character
then replace it) or just setting the Document.Saved flag to false.

DT
 

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