Help - can't delete custom document property

A

Alan Silver

Hello,

I have been writing some VBScript that uses a custom document property
to store the ID of the calendar entry in Outlook to which the document
relates.

The problem that I have is that I cannot clear the entry. I have tried
setting it to an empty string, and even deleting it completely, but next
time I open the document, the property is still there !! I can step
through the code, watch it delete the entry, try checking the entry and
get an "invalid procedure call or argument" error, but STILL see the
property next time the document is opened !!

Here is the code ...

Private Sub Document_Close()
Dim olDocProps As DocumentProperties
Dim slCalID As String
Dim flGetDate As frmGetDate

' check the properties for the calendar ID
Set olDocProps = ActiveDocument.CustomDocumentProperties
On Error Resume Next
' see if the property exists. If not, an error will be raised
slCalID = olDocProps.Item("CalItemID").Value
If Err.Number = 0 Then
' the property exists, delete it
olDocProps.Item("CalItemID").Delete
ActiveDocument.Save
End If
' so now we know there isn't a property for the ID.
' If there is already a calendar item, slCalID will be non-empty
On Error GoTo 0
' Only do the business if we are not editing the template
If Right(ActiveDocument.Name, 4) <> ".dot" Then
' editing a real document.
' do the clever Outlook bits here
End If
End Sub

Note that I snipped the clever Outlook bits as they aren't relevant.

My problem is that I don't want to save the calendar entry ID when
editing the template, so that when a new document is created from the
template, it will not have this property. That way the code knows to
create a new entry. If the ID is non-blank, the code modifies an
existing calendar entry.

I originally had the code create a property if one didn't exist, so I
could just update it hen closing the document, but that gave the same
problem. I changed it to deleting the property (as it is above) in an
attempt to get around the problem.

Whenever I open the document again and step through the code, slCalID
gets set to "00000000B694DB5BF6EA2149BA66B7D297C3EE6024AB2000", which is
(presumably) the ID of the calendar entry I was using when I tested the
code.

Please help me remove this property. It's driving me mad !!

TIA
 
A

Alan Silver

Alan Silver said:

(me again)

As a subscript to the previous message, I just tried changing the name
of the custom property in the template to CalendarItemID, which is a new
name. As the code checks (which it didn't when I started testing) to see
if it the template being saved and only creates the property if not,
this got around the previous problem, but raised another !!

I now can't save the property !! Again, I can step through the code and
watch the property being set to a valid ID. The code runs without error,
the document is saved and closes. I open it up again and check the ID
and it's blank !!

HEEEEEEEEEEEEEEELP !!!!!!

Thanx
 
J

Jezebel

I haven't tried diagnosing your code in detail, but part of the problem is
that DocumentProperties are handled slightly differently in templates and in
documents. The issue relates to whether changing the property also changes
the document/template's .Saved property. If .Saved is true (as it is for an
existing document when opened) then the Save method is ignored. Under some
circumstances (which I've never investigated) modifying the
documentproperties does NOT make Word think that the document or template
needs to be saved.

So what might be happening is that you update the property perfectly well,
but Word is not bothering to save the change.

The work-around is very simple: set .Saved to FALSE before trying to save.


Separately, there's no point checking if the property exists before deleting
it. Simpler is:

with ActiveDocument
On error resume next
.CustomDocumentProperties("CalItemID").Delete
On error goto 0

.Saved = FALSE
.Save
End with
 
A

Alan Silver

Jezebel said:
I haven't tried diagnosing your code in detail, but part of the problem is
that DocumentProperties are handled slightly differently in templates and in
documents. The issue relates to whether changing the property also changes
the document/template's .Saved property. If .Saved is true (as it is for an
existing document when opened) then the Save method is ignored. Under some
circumstances (which I've never investigated) modifying the
documentproperties does NOT make Word think that the document or template
needs to be saved.

How confusing !!
So what might be happening is that you update the property perfectly well,
but Word is not bothering to save the change.

The work-around is very simple: set .Saved to FALSE before trying to save.

Thanx, that worked. I now have a different problem, but that's an
Outlook issue, so I guess I'll head off there !!
Separately, there's no point checking if the property exists before deleting
it. Simpler is:
<snip>

Thanx, you're right. That code was from when I was checking to see if
the property existed, and if not creating it. When I discovered this
problem, I changed it to delete it, but didn't change the code structure
as much as I should have.

Thanx again
 

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