Saving document after adding custom property?

D

Derrick

Hi All
This is my first dip into VBA, so apologies if the question is trivial.
I am wanting to add the same set of custom properties to a large number of
documents, and hoped the following code (synthesized from various examples)
would work.
The problem is that the SAVE statement does not have any effect, although
the CLOSE statement does.
If I interrupt execution before the CLOSE, I see that the custom property
has been added successfully. The debugging display before the SAVE shows a
value of TRUE for ActiveDocument.Saved.
What should I do to SAVE the document?
Thanks & regards
Derrick

(Code follows:
Public Sub AddProperty()
' Display FileOpen dialogue box to select file
If Dialogs(wdDialogFileOpen).Show = -1 Then
' OK button selected, process file
With ActiveDocument.CustomDocumentProperties
.Add Name:="MyNewProperty", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="My new value"
' Debugging: following line displays a value of "True"
MsgBox (ActiveDocument.Saved)
ActiveDocument.Save
ActiveDocument.Close
'Also tried following option to save document
'ActiveDocument.Close SaveChanges:=wdSaveChanges
End With
End If
End Sub
 
D

Doug Robbins

Try moving them both outside of the With - End With construction.

--
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
 
D

Derrick

Hi
I tried this but it does not seem to make any difference. The document is
still not being saved.
Thanks for the suggestion, though. It does make the code more logical.
Forgot to mention I'm using Word 2000, SP3.
Rgds
Derrick
 
D

Derrick

Hi
I don't pretend to understand this, but after inserting a "MsgBox" display
of the "Keywords" built-in property just before the WITH construct (for a
completely different reason), the SAVE now works, and the debugging display
before the SAVE now shows a value of TRUE for ActiveDocument.Saved.
The current (working) code is as follows:

Public Sub AddProperty()
Dim DocId As String
' Display FileOpen dialogue box to select file
If Dialogs(wdDialogFileOpen).Show = -1 Then
' OK button selected, process file
' ========== ADDED THESE LINES ================
DocId = ActiveDocument.BuiltInDocumentProperties(wdPropertyKeywords)
MsgBox (DocId)
' ========================================
With ActiveDocument.CustomDocumentProperties
.Add name:="MyNewProperty", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="My new value"
' Debugging: following line displays a value of "True"
MsgBox (ActiveDocument.Saved)
'ActiveDocument.Save
'ActiveDocument.Close
'Also tried following option to save document
'ActiveDocument.Close SaveChanges:=wdSaveChanges
End With
MsgBox (ActiveDocument.name)
ActiveDocument.Save
ActiveDocument.Close
End If
End Sub

Regards
Derrick
 
C

Chuck Henrich

Hi Derrick

An alternative fix is to add Activedocument.Saved=False before saving (as
per the amended code below). Seems like adding a custom document property
doesn't "dirty" a document enough to trigger Saved=False.
HTH
Chuck

Public Sub AddProperty()
' Display FileOpen dialogue box to select file
If Dialogs(wdDialogFileOpen).Show = -1 Then
' OK button selected, process file
With ActiveDocument.CustomDocumentProperties
.Add Name:="MyNewProperty", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="My new value"
'NEW LINE --------------------------------------
ActiveDocument.Saved = False
'-----------------------------------------------
MsgBox (ActiveDocument.Saved)
ActiveDocument.Save
ActiveDocument.Close
'Also tried following option to save document
'ActiveDocument.Close SaveChanges:=wdSaveChanges
End With
End If
End Sub
 
D

Derrick Ackermann

Hi Chuck
Sorry for the belated "thank you". This seems the perfect workaround.
Regards
 

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