Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

excel - on error goto / if no error how to skip?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...