R
Randy MacDonald
I am having a problem with propogating errors through a
series of error handlers in Excel VBA. Errors are
propogated as expected in most cases, but if I close an
Excel Workbook prior to the error first being raised, then
the error does not propogate normally.
To demonstrate this problem, I have created a new Excel
workbook that contains two classes (Topology and System)
and one module. The module contains only the
procedure "Testit" that shows the problem.
Public Sub Testit()
Dim topTest As New Topology
topTest.TopoMethod
Exit Sub
End Sub
TopoMethod is the only method in the Topology class:
Public Sub TopoMethod()
Dim sysTest As New System
On Error GoTo ErrorHandler
sysTest.SysMethod
Exit Sub
ErrorHandler:
Debug.Print "TopoMethod", Err.Number,
Err.Source, _
vbCrLf & " " &
Err.Description, vbCrLf
Exit Sub
End Sub
SysMethod is the only method in the System class:
Public Sub SysMethod()
Dim wbkSystemDB As Workbook
On Error GoTo ErrorHandler
Set wbkSystemDB = Workbooks.Open
(Filename:=ThisWorkbook.Path & "\" & "TestFile.xls")
'~~ wbkSystemDB.Close SaveChanges:=False '
This is the line that causes the problem!
Err.Raise Number:=vbObjectError + 10000, _
Source:="System", _
Description:="This is a test error
message from SysMethod."
Exit Sub
ErrorHandler:
Debug.Print "SysMethod", Err.Number,
Err.Source, vbCrLf & " " & Err.Description, vbCrLf
Err.Raise Err.Number, Err.Source,
Err.Description
End Sub
"TestFile.xls" can be any Excel Workbook. (In this case
it is a dummy Excel Workbook containing only an empty
sheet.) When the statement that closes the workbook is
commented out as shown, then running Testit results in the
following output in the immediate window:
SysMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
TopoMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
This output represents what I expect and want. However,
if the statement that closes the Excel Workbook is not
commented out, then running Testit results in this output
in the immediate window:
SysMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
TopoMethod -2147211504 VBAProject
Method 'SysMethod' of object 'System'
failed
The error Number property is propogated correctly in both
cases. But in the latter case the Source and Description
properties have been lost/replaced.
I have tried some simple solutions, such as adding
DoEvents after the Workbook close, but have had no success
in finding a workaround for this problem. Any ideas or
suggestions would be greatly appreciated. (I am running
Excel 2000 (9.0.4402 SR-1) on a Windows 2000 (5.00.2195
Service Pack 4) system.)
Thanks for any suggestions.
Cheers,
Randy
series of error handlers in Excel VBA. Errors are
propogated as expected in most cases, but if I close an
Excel Workbook prior to the error first being raised, then
the error does not propogate normally.
To demonstrate this problem, I have created a new Excel
workbook that contains two classes (Topology and System)
and one module. The module contains only the
procedure "Testit" that shows the problem.
Public Sub Testit()
Dim topTest As New Topology
topTest.TopoMethod
Exit Sub
End Sub
TopoMethod is the only method in the Topology class:
Public Sub TopoMethod()
Dim sysTest As New System
On Error GoTo ErrorHandler
sysTest.SysMethod
Exit Sub
ErrorHandler:
Debug.Print "TopoMethod", Err.Number,
Err.Source, _
vbCrLf & " " &
Err.Description, vbCrLf
Exit Sub
End Sub
SysMethod is the only method in the System class:
Public Sub SysMethod()
Dim wbkSystemDB As Workbook
On Error GoTo ErrorHandler
Set wbkSystemDB = Workbooks.Open
(Filename:=ThisWorkbook.Path & "\" & "TestFile.xls")
'~~ wbkSystemDB.Close SaveChanges:=False '
This is the line that causes the problem!
Err.Raise Number:=vbObjectError + 10000, _
Source:="System", _
Description:="This is a test error
message from SysMethod."
Exit Sub
ErrorHandler:
Debug.Print "SysMethod", Err.Number,
Err.Source, vbCrLf & " " & Err.Description, vbCrLf
Err.Raise Err.Number, Err.Source,
Err.Description
End Sub
"TestFile.xls" can be any Excel Workbook. (In this case
it is a dummy Excel Workbook containing only an empty
sheet.) When the statement that closes the workbook is
commented out as shown, then running Testit results in the
following output in the immediate window:
SysMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
TopoMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
This output represents what I expect and want. However,
if the statement that closes the Excel Workbook is not
commented out, then running Testit results in this output
in the immediate window:
SysMethod -2147211504 SystemSrc
This is a test error message from
SysMethod.
TopoMethod -2147211504 VBAProject
Method 'SysMethod' of object 'System'
failed
The error Number property is propogated correctly in both
cases. But in the latter case the Source and Description
properties have been lost/replaced.
I have tried some simple solutions, such as adding
DoEvents after the Workbook close, but have had no success
in finding a workaround for this problem. Any ideas or
suggestions would be greatly appreciated. (I am running
Excel 2000 (9.0.4402 SR-1) on a Windows 2000 (5.00.2195
Service Pack 4) system.)
Thanks for any suggestions.
Cheers,
Randy