Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2006

Tip: Looking for answers? Try searching our database.

Sanity check: What's wrong with this code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rhino - 03 Feb 2006 00:47 GMT
This sub worked fine several times then suddenly stopped working. Can anyone
help me figure out what is wrong?

-----------------------------------------------------------------------------------------------------
Public Sub getLastRevisionDate()

'Set the title for the MsgBoxes which will be displayed.
Const TITLE As String = "Custom Properties"

'Set the name of the custom property.
Const REVISION_DATE_KEY As String = "LAST_REVISION_DATE"

'If there is no document open, there is nowhere to get custom properties
either. Inform the user.
If Application.Documents.count = 0 Then
   MsgBox "There are no documents open so no custom properties can be
found. Open a document before trying this macro again.", vbOKOnly, "Custom
Properties"
   Exit Sub
End If

'If the desired property does not exist in this document, inform the user.
'Otherwise, display the value of the desired property.
If (ActiveDocument.CustomDocumentProperties(REVISION_DATE_KEY).value =
False) Then
   MsgBox "The " & REVISION_DATE_KEY & " is not defined for the Active
Document.", vbOKOnly, TITLE
Else
   Dim revisionDate As String
   revisionDate =
ActiveDocument.CustomDocumentProperties(REVISION_DATE_KEY).value
   MsgBox "The value of the " & REVISION_DATE_KEY & " is " & revisionDate &
".", vbOKOnly, TITLE
End If

End Sub
-----------------------------------------------------------------------------------------------------

I get this error message on the second If statement, the one that is
checking the value of the LAST_REVISION_DATE property: "Run-time error '5':
Invalid procedure call or argument

For what it's worth, I tried hardcoding the name of the key in the brackets
and putting quotes around it but it made no difference at all. Also, there
is an active document open within Word.

Before the statement started crashing, it worked fine for several
iterations. Prior to that, it also failed every time but those times the
error message complained about a missing object. Then, it just started
working right about the time I was getting really really frustrated. For the
life of me, I don't know why this statement has been alternating between
working fine and not working at all: I haven't changed it in hours. There's
obviously some kind of technique or practice that I'm not following which is
causing me to fall over my own feet; I just wish I could figure out what it
is....

Anyway, if you can help me figure out what's wrong here, I'd be deeply
grateful.

This code is so close to finished it hurts. I really want to finish this.

--
Rhino
Jezebel - 03 Feb 2006 01:12 GMT
The problem is that your if statement is checking, not whether the value
exists, but whether the value is equal to FALSE. This throws an error if the
value is not defined at all. Simpler is to retrieve the value and trap the
error if not defined ...

On error resume next
revisionDate = ActiveDocument.CustomDocumentProperties(REVISION_DATE_KEY)
On error goto 0

If len(revisionDate) > 0 then
   Msg =  "The value of the " & REVISION_DATE_KEY & " is " & revisionDate &
"."
else
   Msg = "The " & REVISION_DATE_KEY & " is not defined."
end if
Msgbox Msg

Separately, there's no point putting the 'Dim revisionDate' statement within
the If clause. 'Dim' is a compiler directive, not a runtime instruction, so
the variable is always declared anyway.

> This sub worked fine several times then suddenly stopped working. Can
> anyone help me figure out what is wrong?
[quoted text clipped - 59 lines]
> --
> Rhino
Rhino - 03 Feb 2006 04:55 GMT
See remarks interspersed below....

> The problem is that your if statement is checking, not whether the value
> exists, but whether the value is equal to FALSE. This throws an error if
> the value is not defined at all.

As Homer Simpson would say, "D'oh!". Yeah, I should have seen that. I found
a similar fragment in a Google newsgroup search and didn't think through the
difference between what problem the post was handling and what I was doing.
I thought it was the way VBA tests for existence of a field but now I see
that it isn't.

> Simpler is to retrieve the value and trap the error if not defined ...
>
[quoted text clipped - 9 lines]
> end if
> Msgbox Msg

Thanks for the suggestion!

> Separately, there's no point putting the 'Dim revisionDate' statement
> within the If clause. 'Dim' is a compiler directive, not a runtime
> instruction, so the variable is always declared anyway.

Sure, but isn't it always better to declare the variable to be a specific
type rather than let it default to variant? That's really the only reason
I'm doing it.

--
Rhino

>> This sub worked fine several times then suddenly stopped working. Can
>> anyone help me figure out what is wrong?
[quoted text clipped - 60 lines]
>> --
>> Rhino
Jezebel - 03 Feb 2006 07:05 GMT
> Sure, but isn't it always better to declare the variable to be a specific
> type rather than let it default to variant? That's really the only reason
> I'm doing it.

Absolutely. I was just being lazy. My point is that the declarations should
all be at the top of the function, not interspersed with other code.
Rhino - 03 Feb 2006 15:13 GMT
Ahh, okay, thanks for clearing that up :-)

Just for interest sake, why should the declarations always be at the top of
the function? Is there any real functional benefit? In Java, my main
language these days, it's really just a style thing: some programmers feel
that declarations ought to be at the top where they are more visible, others
prefer to do declarations just before the variable is needed but both
approaches work just fine.

By the way, I've got my CustomProperty-related subs working just fine thanks
to your suggestion. Thanks for your help with this (and other questions)!

--
Rhino

>> Sure, but isn't it always better to declare the variable to be a specific
>> type rather than let it default to variant? That's really the only reason
[quoted text clipped - 3 lines]
> should all be at the top of the function, not interspersed with other
> code.
Jezebel - 03 Feb 2006 21:45 GMT
Coding styles and conventions are methods that programmers have found,
through experience, to reduce the opportunities for bugs and to improve
maintainability. Declaring your variables is one (try writing a large app
without using 'option explicit'!); declaring them all before any code is
another.

There is one functional issue: remove the option explicit from your module,
and insert another reference to the variable *before* the declaration (which
is the sort of thing that happens when code gets modified by someone
else) -- the code runs OK if the IF condition is not met, and throws a
'duplicate declaration' error if it is.

> Ahh, okay, thanks for clearing that up :-)
>
[quoted text clipped - 19 lines]
>> should all be at the top of the function, not interspersed with other
>> code.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.