-->

on error goto / if no error how to skip?

2019-09-20 12:55发布

问题:

so i have a program that has an email function however, some of the computer dont have outlook and so i need the code to open a folder on error... All of that i have working! but... now the code runs and even if no error it will run my "workaround". See code. How do i skip if no error?

'NO OUTLOOK APP WORK AROUND
On Error GoTo WORKAROUND

'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display
.To = ""
.CC = ""
.Subject = "QUOTE FOR -" + xSht.Name + xStr
.Attachments.Add xFolder
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If

'SKIP TO END OF WORKAROUND IF NO ERROR
WORKAROUND:
Dim PID As Double
Dim strRootPath As String

Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "

'// Change rootpath here
strRootPath = "C:\Data Files"

PID = Shell(strExpExe & strArg & strRootPath, 3)

'THIS IS WHERE IT NEEDS TO PICK UP AGAIN.

ActiveSheet.Name = "ACTUAL"

Unload Me
CLOSE1.Show

End Sub

回答1:

You might want to try this

On Error GoTo WORKAROUND

    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = "QUOTE FOR -" + xSht.Name + xStr
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
    MsgBox "The active worksheet cannot be blank"
    Exit Sub
End If

' Seems this should always run
ActiveSheet.Name = "ACTUAL"

Unload Me
CLOSE1.Show

Exit Sub

'SKIP TO END OF WORKAROUND IF NO ERROR
WORKAROUND:
Dim PID As Double
Dim strRootPath As String

Const strExpExe = "explorer.exe"
Const strArg = " "    '" /e,/root, "

'// Change rootpath here
strRootPath = "C:\Data Files"

PID = Shell(strExpExe & strArg & strRootPath, 3)

'THIS IS WHERE IT NEEDS TO PICK UP AGAIN.

ActiveSheet.Name = "ACTUAL"

Unload Me
CLOSE1.Show

For a comprehensive guide of error handling have a look here. It also seems you unload a userform within the code of the userform itself. Have a look here why that is not a good idea and again look here for a guide