This one's killing me ladies and gents.
I had this working fine for a while. I added error checking and now it's all messed up and I don't know why (other than my grasp of error control is tenuous at best).
Here's the situation: I have a userform, on activation, sets excel to application.visible = false. Therefore, if the userform errors out for any reason, I get an open instance of excel that I can't do anything about but log out and log back in.
Here's my submit code. When I get to the part about the "ALL YOUR DATA WILL BE LOST" Yes works fine. No closes the userform, when it should just exit the sub and allow the user to continue to input data.
Here's the code:
Any help would be very much appreciated
I had this working fine for a while. I added error checking and now it's all messed up and I don't know why (other than my grasp of error control is tenuous at best).
Here's the situation: I have a userform, on activation, sets excel to application.visible = false. Therefore, if the userform errors out for any reason, I get an open instance of excel that I can't do anything about but log out and log back in.
Here's my submit code. When I get to the part about the "ALL YOUR DATA WILL BE LOST" Yes works fine. No closes the userform, when it should just exit the sub and allow the user to continue to input data.
Here's the code:
Code:
Private Sub cbClose_Click()
'On Error GoTo Errhandler
On Error GoTo Errhandler
If MsgBox("Attach and send as email?", vbInformation + vbYesNo) = vbYes Then
'Variable declaration
Dim oApp As Object, _
oMail As Object, _
WB As Workbook, _
FileName As String, _
UserName As String
Dim irow As Long
Dim lr As Long
UserName = UserNameWindows()
'Turn off screen updating
Application.ScreenUpdating = False
'Make a copy of the active sheet and save it to
'a temporary file
Sheets(Array("OCCData", "UPGData", "DISData")).Copy
Set WB = ActiveWorkbook
FileName = UserName & " " & Format(Now, "mmddyyyy") & ".xls"
On Error Resume Next
Kill "H:\" & FileName
On Error GoTo 0
WB.SaveAs FileName:="H:\" & FileName
'Create and show the outlook mail item
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.Subject = UserName & " " & Format(Now, "mmddyyyy") & " Tracking Results"
.Attachments.Add WB.FullName
.Display
End With
'Delete the temporary file
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close savechanges:=False
'Restore screen updating and release Outlook
Application.ScreenUpdating = True
Set oMail = Nothing
Set oApp = Nothing
Application.IgnoreRemoteRequests = False 'deactivate
ThisWorkbook.Close savechanges:=False
Application.Quit
Unload Me
Else
If MsgBox("ALL OF YOUR RESULTS WILL BE LOST. Are you sure?", vbInformation + vbYesNo) = vbYes _
Then
ThisWorkbook.Close savechanges:=False
Application.IgnoreRemoteRequests = False 'deactivate
Application.Quit
Unload Me
Else
Exit Sub
End If
End If
Exit Sub
Errhandler: GetMeOut
End Sub
Any help would be very much appreciated