Excel Cannot Close Word When Stepping Through Code (F8) - excel

When running the following code from Excel, it does what I expect it to do:
It opens Word, raises an error which gets handled via CleanError and closes Word via CleanExit.
Sub testWord()
' VBE > Tools > References > Microsoft Word 1?.0 Object Library
Dim wdApp As New Word.Application
' 'Open' Word.
wdApp.Visible = False
' To prevent Word from staying open if an error occurs.
On Error GoTo CleanError
' Raise an error.
Err.Raise 13
CleanExit:
' Close Word.
wdApp.Quit
Exit Sub
CleanError:
MsgBox "Run-time Error '" & Err.Number & "': " & Err.Description, vbCritical
Resume CleanExit
End Sub
When stepping through the code using F8, Excel hangs (is still running the code) i.e. doesn't close Word after wdApp.Quit. When closing Word via Task Manager, the line Exit Sub is highlighted and stepping further is 'allowed' again.
Am I doing something wrong here?

I'm guessing you have a Watch set on wdApp in the Watches window, or something along those lines.
I only see that behavior with a Watch set on wdApp.

Related

How to use error handling for my Excel VBA code

I am trying to add error handling for one of the code. I am assigning worksheet with specific name to the sheet objects, now I am trying to add OnError statements if in case user changes the names of sheets then code should gracefully exit the sub after displaying a message to correct the file names.
But the problem is as I provide the structure as below then the sub exit code always executes even if the file name is ok.
The problem is, as I provide the details in the structure as below then the sub exit code always executes even if the file name is ok.
Private Sub DoSomething()
On Error GoTo CleanFail
'...code...
CleanExit:
'cleanup code here
Exit Sub
CleanFail:
If Err.Number = 9 Then 'subscript out of range
Err.Clear
Resume Next
Else
MsgBox Err.Description
Resume CleanExit
End If
End Sub
On Error GoTo ErrorFailLoad
Set LoadSheet = Application.ActiveWorkbook.Worksheets("Load_Template")
ErrorExit:
End Sub
ErrorFailLoad:
MsgBox "Main File name must be Load_Template. Please change and re-run Macro"
Resume ErrorExit
On Error GoTo ErrorFailEquip
Set EquipSheet = Application.ActiveWorkbook.Worksheets("EQUIP")
ErrorExit:
End Sub
ErrorFailEquip:
MsgBox "Equipment File name must be EQUIP. Please change and re-run Macro"
Resume ErrorExit
I expect the code to just check if the file name is not available then just show error message and exit. Actual results is code exit even after correct file name.

Not able to handle runtime error 9 with on error go to [label]

I have the followin code. But the error does not get handled
Sub Some_sub()
Some code here
On Error GoTo resumeCode
If Workbooks("Some file.xlsm").ReadOnly Then
GoTo readOnlyError
End If
resumeCode:
On Error GoTo fileNotOpen
Workbooks("Some file.xlsm").Activate
some more code here
Exit Sub
fileNotOpen:
MsgBox "Error: Claims followup.xlsm is not open." & Chr(10) & Chr(10) &
"Open the file in read/write"
Exit Sub
End Sub
When I run debug mode it shows me this line: Workbooks("Some file.xlsm").Activate in yellow. Instead of handling the error and going to the label.
Within VBA under Tools -> Options -> General Tab: Break on Unhandled Errors active.
When I have the file open it runs the code like it should. When it is closed it does not handle the error.
What am I doing wrong?
Thanks in advance for your help
That's it. As I said in the comments, when an error occurs, and you handle it, you cannot setup a new On Error mechanism until you explicitly invoke the Resume keyword in any way.
One possible way to achieve it, if you dont want to change the flow of your routine, is just to add a label before your new On Error statement and Resume on it. Like this, for example:
Sub Some_sub()
' Some code ...
On Error GoTo resumeCode
If Workbooks("Some file.xlsm").ReadOnly Then
GoTo readOnlyError
End If
' Some more code ...
resumeCode:
Resume ResumeHere ' <---------------------------------------- Invoke Resume
ResumeHere: ' <------------------------------------- Add some label
On Error GoTo fileNotOpen
Workbooks("Some file.xlsm").Activate
Exit Sub
fileNotOpen:
msgBox "Error: Claims followup.xlsm is not open." & Chr(10) & Chr(10) & "Open the file in read/write"
Exit Sub
End Sub
You should be careful though, the keyword Resume will itself raise an error if the error status is blank and it is reached from normal flow, In that case you should put the error handling sections, each apart, in the end of your routine and resume at any labels within the normal flow from there. This is generally the usual approach.

Close a Workbook if opened

I would like an error handler to handle closing an Excel workbook that is not open.
I tried below code.
If Workbooks("Combo.xlsx").IsOpen Then
Workbooks("Combo.xlsx").Close SaveChanges:=False
Else: Resume Next
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End If
This gives me an error message:
Run time Error: 9 Subscript Out of Range.
All you really need is
On Error Resume Next
Workbooks("Combo.xlsx").Close SaveChanges:=False
On Error Goto 0
You can ignore the error if there is no workbook open with that name.
I had the same problem that the On Error Resume Next didn't seem to work for this. My settings are only to break on unhandled errors so I can't understand why it keeps breaking when trying to close.
Anyway, I made this simple workaround subroutine to call when closing the workbook:
Private Sub closeWorkbookIfOpen(wb As Workbook)
On Error GoTo NotOpen
wb.Close
NotOpen:
End Sub
Not sure why the Resume Next doesn't work, but this seems to do the trick!

Simple Error handling in Excel VBA

I need a simple error handling code for my small macro, I have search the web but have nothing simple, seems to be all very complicated.
I down load sales reports in .txt form on a weekly basis, I run separate macro to do stuff and then add to a master page. Not every week do sales reports download as there may not have been sales for that particular region.
I need a simple error handler so that if it does not find the report, it moves to the next sub.
Any help appreciated
Sub MXM_POS()
Workbooks.OpenText Filename:="C:\Users\903270\Documents\Excel\MXMPOS*.txt"
‘Run macro code
Run ("DLK_POS")
End Sub
Here is a simple basic structure that you can expand on as needed:
Sub MXM_POS()
On Error GoTo ErrHandler
' code here
ExitSub:
' shutdown code here
Exit Sub
ErrHandler:
If Err.Number <> 0 Then
Dim mbr As VbMsgBoxResult
mbr = MsgBox( _
"Error #" & Err.Number & ": " & Err.Description & vbNewLine & _
"Would you like to continue?", vbExclamation Or vbYesNo)
If mbr = vbYes Then Resume ExitSub
Application.Quit
End If
End Sub
When I desire a stack dump I construct that within the Source property of the Err object using concatenation with a newline, and then only display the MsgBox result at the top of the calling stack, usually either the Event Handler that launched the code or the top-level macro invoked by the user.

Excel Macro : How to get notification when a VBA script stops execution

I am running a macro on a remote PC which executes every 5 seconds throughout the day.
Now I want that if something goes wrong and the macro stops execution I should be informed or notified via an Email.
How can I do that?
Error handling.
Sub MySub()
On Error GoTo ErrorHandler
' Work done here...
' Screen for an expected error
If somethingWentWrong = True Then
Err.Raise Number:=myErrorNumber, Source:="MySub", _
Description:="This thing went wrong."
' Will now go to ErrorHandler
End If
' More work done here...
ExitProcedure:
On Error Resume Next
'Cleanup code goes here...
Exit Sub
ErrorHandler:
' If an error occurs (anticipated or not), the following will be executed.
Call SendEmailNotification( _
Recipient:="you#there.com", _
Subject:="Something went wrong.", _
Message:=Err.Number & vbCrLf & Err.Description & vbCrLf & Err.Source)
' Any other error handling goes here...
Resume ExitProcedure
End Sub
To send an e-mail, there are various solutions out there. Search for CDO, MAPI, Sockmail. Searching on those will give you examples for how to write your SendEmailNotification sub. Heads up: None of these are exactly straightforward.
You can add a mail send functionality within your error handling. If the machine the macro is being executed has Outlook properly set, I believe you can use ActiveWorkbook.SendMail.
If this PC doesn't have the Outlook set, you'll need to find out for a mail solution that fits your environment.
It would then looks like:
Sub MySub()
On Error GoTo ProcError
'Your stuff
ProcExit:
Exit Sub
ProcError:
ActiveWorkbook.SendMail "your.mail#yourdomain.com", "Application failed!"
Resume ProcExit
End Sub

Resources