Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot read empty CustomDocumentProperties 2

Status
Not open for further replies.

eman6

Programmer
Dec 7, 2004
578
CH
I am trying to check properties of a series of Word2000 documents.
For some reason, the Department property is giving me hard time:

<code>
Set objWord = CreateObject("Word.Application")
Set myWdoc = objWord.Documents.Open(myNew)

If myWdoc.CustomDocumentProperties ("Department").Value = Empty Then
</code>
myNew is a valid word document path, I checked it.
It stops at the if statement and gives an "Invalid procedure call or argument" error.
I tried all combinations of .Text instead of .Value or "" instead of Empty, or Null instead of Empty, all at no avail.
This happens when no initial value has been set to the Custome Property 'Department'.
What am I doing wrong, or missing??


______________________________________
Eman2005
Technical User
 
You can't test against something that isn't there, hence the error.
Two options:[ol]
[li]Trap/Handle the error and do nothing with it. This way it is not shown to the user.
Code:
...
On Error Goto Err_Handler
...
Err_Handler:
If Err.Number = 5 Then
   Resume Next 'Do nothing
End If
...
[/li]
[li]Iterate through all the [tt]CustomDocumentProperties[/tt] to check for a match.
Code:
...
Dim itmDocProp As DocumentProperty
...
For Each itmDocProp In myWDoc.CustomDocumentProperties
  If itmDocProp.Name = "Department" Then
    'Do yer stuff here
  End If
Next itmDocProp
...
[/li][/ol]

Option 1 is probably not the best becasue Error '5' is so generic.

Hope this helps,
CMP


Instant programmer, just add coffee.
 
Thank you for the two suggestions.
The second solution is not useful because it does not check if Department has a value or not, which os what I need to check first and in case the value is empty then (and only then), I create the property.

I therefore will probably have to try the first solution.

I'll let you know the result, but I would welcome any intermediate comment or advice :)


______________________________________
Eman2005
Technical User
 
Yes, the first solution worked (why shouldn't it).

I must say this is not the kind of programming technique that I like, but I guess I will have to accept it for now.

A star^is there for you :)

Many thanks.

______________________________________
Eman2005
Technical User
 
Hi Eman6,

To use the second of CautionMP's solutions, all you need to do is to add a test of the contents of the property. For example:
Code:
Sub Test()
Dim itmDocProp As DocumentProperty
For Each itmDocProp In ActiveDocument.CustomDocumentProperties
  If itmDocProp.Name = "Department" Then
    If itmDocProp.Value <> "" Then
        MsgBox itmDocProp.Name & vbCrLf & itmDocProp.Value
    End If
  End If
Next
End Sub

Cheers
 
[ol][li]Handling errors is perfectly acceptable (if the error is descriptive enough), I'm convinced that 30-40% of the MS help files are devoted to this.[/li][li][blue]macropod[/blue] thank you for expanding on do yer stuff here in my absence. That's what I love about this community, someone always has your (yer?) back.[/li][/ol]
CMP

Instant programmer, just add coffee.
 
Hi Macropod
Thank you for the comment.
How does this differ from checking the value the first place?
I mean, "Department" does exist by default as CustomeDocumentProperty anyway; just no value is assigned to it, and this makes attempting to read its value giving an error (oddly). So, in your routine, the same thing would happen, wouldn't it?

I am not arguing your proposal, but trying to understand.

Thank you.

______________________________________
Eman2005
Technical User
 
Hi,

Well, as CautionMP said, "You can't test against something that isn't there".
So, by looping through all the CustomDocumentProperties, we can test for the existence of the the "Department" DocumentProperty with the line:
If itmDocProp.Name = "Department" Then
This is different to merely testing it's contents.

If the "Department" DocumentProperty doesn't exist, the test will never return true and the next test (for the property's contents) won't be executed. If the "Department" DocumentProperty does exist, we can then test whether it's empty with:
If itmDocProp.Value <> "" Then

HTH.
 
Again, the Department property ALWAYS exists.
Just start Word and check for yourself.
It exists.
The problem is, its value is not initiated.

______________________________________
Eman2005
Technical User
 
the Department property ALWAYS exists
Really ?
Why have I the following in the debug window ?
? ActiveDocument.CustomDocumentProperties.Count
0
 
PH is correct, and this can easily be seen with:
Code:
Dim oProp As CustomProperty
For Each oProp In ActiveDocument.CustomDocumentProperties
    MsgBox oProp.Name & "  " & oProp.Value
Next

On a saved document, but with no custom properties set, absolutely no messages are displayed because there are no custom document properties. Not until you make one. The dialog displayed list is nothing more than a list that Word shows. When a user (or programmer) puts a value into the dialog and saves it, then and only then, is the custom document property created (and set).

"Department" does NOT always exist.

Gerry
 
eman6 said:
Again, the Department property ALWAYS exists
Ok, I took a step back and I think I found the disconnect.

If you look at the Custom tab in the Properties window for the document, under the Name: box Department is listed an option in the list box. This list box contains Custom Property suggestions provided by Microsoft (Thank you Bill), they don't actually exist in the [tt]CustomDocumentProperties[/tt] Collection.

Once you select one and click Add, it gets appended to the collection.

The multi-column list box (Properties:) at the bottom of the Custom tab show what [tt]CustomDocumentProperties[/tt] have actually been assigned, and these are what we are iterating through with the above code.

Sorry I missed this, sometimes I start writing code and forget that the Word window even exists.

CMP

Instant programmer, just add coffee.
 
Another way of looking at this is that the properties exist in Word, but not in the document until you add them to it.

Cheers
 
Fair enough.
This all makes more sense now.
Thanks everybody. :)


______________________________________
Eman2005
Technical User
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top