on error goto / if no error how to skip?

家住魔仙堡 提交于 2019-12-13 08:59:11

问题


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



来源:https://stackoverflow.com/questions/51795650/on-error-goto-if-no-error-how-to-skip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!