Need Help With Run-Time Error 91

T

Thomas M

Word 2000

I have the following subroutine. A couple of the variables in the Write
command are set in another routine that calls this one. Those variables
are declared at the module level.

******************
Option Base 1
Option Explicit

Sub ErrorLogging()

Dim ErrDateTime As Date
Dim ErrorLog As String
Dim ErrorLogName As String
Dim vbErrLogPath As String
Dim FileNumber As Long
Dim FSO As Object

On Error GoTo ErrorHandler

FileNumber = FreeFile
Set FSO = CreateObject("Scripting.FileSystemObject")
ErrorLogName = "Error.log"
vbErrLogPath = ActiveDocument.Variables.Item("ErrorLogPath").Value & "\"
ErrorLog = vbErrLogPath & ErrorLogName
ErrDateTime = FormatDateTime(Now, vbGeneralDate)

On Error Resume Next
If IsError(FSO.getFile(ErrorLog)) Then
Open ErrorLog For Output As FileNumber
Else
Open ErrorLog For Append As FileNumber
End If

On Error GoTo ErrorHandler

Write #FileNumber, ErrDateTime, ModuleName, Subroutine, Err.Number,
Err.Context, Err.Description, Err.HelpFile, cbDataClip
Write #FileNumber,

Close #FileNumber
Exit Sub

ErrorHandler:

If Err.Number = 53 Or Err.Number = 5825 Then
Dialogs(wdDialogFileOpen).Display
ActiveDocument.Variables("ErrorLogPath").Value = CurDir
End If
Resume

End Sub
******************

Before I test this code I make sure that the ErrorLogPath document
variable does NOT exist. This produces an error with the vbErrLogPath =
command, which causes execution to move to ErrorHandler. ErrorHandler
should allow the user to specify the path to the error log when
Err.Number = 53 (File not found) or 5825 (Application-defined or object-
defined error--i.e. Document variable doesn't exist). Instead, the IF
statement in ErrorHandler causes the following error:

Run-time error '91':
Object variable or With block variable not set

This code was working earlier tonight, and I'm not sure what I did to
break it. I've tried to isolate the problem by changing the way that the
routine handles errors, but each time there is a reference to the Err
object, I get Run-time error 91.

So the problem seems to be related to the Err object. But at the same
time, that doesn't seem to make any sense because Err is an intrinsic
object and does not need to be declared.

I'm stumped. Can someone tell me why I'm getting Run-time error 91?

--Tom
 
T

Thomas M

Word 2000

I have some additional information on this problem. First, the code
posted in the previous message is in the ThisDocument module. Second,
the problem seems to be related to that module. I tested this with the
following code.

Sub MyTestRoutine()

On Error GoTo ErrorHandler
Err.Raise 13

Exit Sub

ErrorHandler:

MsgBox Err.Number

End Sub

When placed in a new module--either within the same project, or in a new
project--this code works fine. However, when placed in the ThisDocument
module, the MsgBox command in ErrorHandler produces Run-time error 91.

I decided that the ThisDocument module must be corrupt. So I copied all
the macros into a text file, closed my project, opened a new document,
and pasted the code from the text file into the ThisDocument module of
the new project. I don't know if placing the code into a text file
temporarily really accomplishes anything, but my thought was that if I
exported the code to and file and then imported it into the new project,
garbage from the old project might carried carried over. I figured that
running it through a text file would eliminate that possibility.

Anyway, doing that did not cure the problem. So I downloaded and ran the
Word Code Cleaner, which also did not solve the problem. Any help would
be greatly appreciated.

--Tom
 
M

Montana DOJ Help Desk

It looks like I've solved this problem. A few days back, while trying to
debug something, I put the following in my declarations:

Dim Err as ErrObject

I realize that the Err object is intrinsic and does not need to be declared,
but I was grasping at straws by that point, and decided to try it. Adding
that to the declarations made no difference in solving my problem, so I
moved on to the next idea and just forgot to remove it.

Tonight, I cleaned my project and then removed the above line from the
declarations, and that fixed the problem. So I think the problem was
two-fold. First, the project needed to be cleaned, and second, once I
cleaned the project, the above line needed to be removed.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 

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