R
Rhino
I'm having some variable scope problems in VBA and would love to get an
explanation of how to avoid these problems.
I'm writing macros in Word 2002. At the moment, I'm polishing a series of
related procedures. I have a main macro that farms out much of its work to
other procedures. I also have some error handling which is centralized in
the main macro. I'd like to move some of the error handling into the lesser
procedures but this is causing me problems that look like variable scope
issues.
Specifically, the current version of the main macro opens a log file, writes
messages to that log file when it encounters errors, and then closes the log
file. I'd like to leave the open and close in the main macro but do the
writing of the error messages in a lesser procedure. For instance, I'd like
to be able to call a procedure like this from any of the other procedures:
public sub logError(message As String)
logTextStream.Write (message)
end sub
and then invoke it like this:
If ....
msg = "The warp pattern buffer is full."
logError (msg)
End If
However, when I change the code along these lines and then execute it, the
logTextStream.Write method crashes in the logError procedure with "Runtime
error '424': Object required." It appears that logTextStream is not visible
in the logError procedure. What do I need to do to make it visible in
logError? It is currently defined in the main macro as follows:
Set logFileObject = CreateObject("Scripting.FileSystemObject")
logFileObject.CreateTextFile (LOG_FILE_NAME)
Set logFile = logFileObject.GetFile(LOG_FILE_NAME)
Set logTextStream = logFile.OpenAsTextStream(WRITEONLY,
TRISTATE_USE_DEFAULT)
Clearly, logTextStream is a Variant since there is no 'Dim' statement for
it. I suspect that's my problem. I tried to write a 'Dim' statement for it
but VBA rejected my attempts:
- when I tried 'Dim logTextStream as TextStream', I found that there is no
TextStream type
- when I tried 'Dim logTextStream as Stream', I found that there is no
Stream type
- when I tried 'Dim logTextStream as Object', I got no compile error but I
got runtime error '424' when I wrote to logTextStream.
- when I tried 'Dim logTextStream as Object' and put that statement in the
Declarations area of Normal, I got no compile error but I got a runtime
error '91' when I wrote to logTextStream
So, what is the _right_ way to do this? I think I've found all the wrong
ways and can't think of anything else to try.
explanation of how to avoid these problems.
I'm writing macros in Word 2002. At the moment, I'm polishing a series of
related procedures. I have a main macro that farms out much of its work to
other procedures. I also have some error handling which is centralized in
the main macro. I'd like to move some of the error handling into the lesser
procedures but this is causing me problems that look like variable scope
issues.
Specifically, the current version of the main macro opens a log file, writes
messages to that log file when it encounters errors, and then closes the log
file. I'd like to leave the open and close in the main macro but do the
writing of the error messages in a lesser procedure. For instance, I'd like
to be able to call a procedure like this from any of the other procedures:
public sub logError(message As String)
logTextStream.Write (message)
end sub
and then invoke it like this:
If ....
msg = "The warp pattern buffer is full."
logError (msg)
End If
However, when I change the code along these lines and then execute it, the
logTextStream.Write method crashes in the logError procedure with "Runtime
error '424': Object required." It appears that logTextStream is not visible
in the logError procedure. What do I need to do to make it visible in
logError? It is currently defined in the main macro as follows:
Set logFileObject = CreateObject("Scripting.FileSystemObject")
logFileObject.CreateTextFile (LOG_FILE_NAME)
Set logFile = logFileObject.GetFile(LOG_FILE_NAME)
Set logTextStream = logFile.OpenAsTextStream(WRITEONLY,
TRISTATE_USE_DEFAULT)
Clearly, logTextStream is a Variant since there is no 'Dim' statement for
it. I suspect that's my problem. I tried to write a 'Dim' statement for it
but VBA rejected my attempts:
- when I tried 'Dim logTextStream as TextStream', I found that there is no
TextStream type
- when I tried 'Dim logTextStream as Stream', I found that there is no
Stream type
- when I tried 'Dim logTextStream as Object', I got no compile error but I
got runtime error '424' when I wrote to logTextStream.
- when I tried 'Dim logTextStream as Object' and put that statement in the
Declarations area of Normal, I got no compile error but I got a runtime
error '91' when I wrote to logTextStream
So, what is the _right_ way to do this? I think I've found all the wrong
ways and can't think of anything else to try.