Home > Software engineering >  Call macros hint when something goes wrong, how to let the program continues to run down automatical
Call macros hint when something goes wrong, how to let the program continues to run down automatical

Time:10-04



Each master, I novice, to write a small program, encounter some problems, also look at the warrior, please people help, thank you for your attention, thank you!
As shown in picture, when the pop-up error box, does not make the debugging process, also is not quit, but directly run down,

Private Sub Command1_Click ()
On the Error Resume Next

If Text14. Text="" Then
MsgBox "error, to execute a program file is empty!"
The Exit Sub 'Exit if the file is null, it is
End the If

Dim xlApp As Object
Dim xlBook As Object: Dim Book1 As Object
Dim xlSheet As Object: Dim xlSheet1 As Object
The Set xlApp=CreateObject (" Excel. Application ")
'xlApp. Visible=False
The Set xlBook=xlApp. Workbooks. Add

The Set Book1=xlApp. Workbooks. Open (Text14)
The Set xlSheet1=Book1. Worksheets (1)
Dim hong As String: Dim GZB As String: Dim HMM As String
Dim WJHS As Integer: Dim ZXGS As Integer
Dim Pid As Long: Dim hWnd As Long 'for judging exe program end time
WJHS=xlSheet1. Cells (1, 2) the number of 'execution

For ZXGS=3 To WJHS + 3

Hong=xlSheet1. Cells (ZXGS, 1) 'definition macro file
If hong="" Then
WJHS=ZXGS - 3
The Exit For
End the If
If Right (hong, 3)="exe" Then
'ShellAndWait hong


Pid=Shell (hong, vbMinimizedFocus)
HWnd=OpenProcess (the SYNCHRONIZE, 0, Pid)
Do
DoEvents
Loop Until WaitForSingleObject (hWnd, 0)=0
MsgBox "the Process Finished!"
The CloseHandle hWnd

The Else
The Set xlBook=xlApp. Workbooks. Open (hong)
The Set xlSheet=xlBook. Worksheets (1)
GZB=Mid $(hong, InStrRev (hong, "") + 1) 'read to path file name


XlSheet. Cells (1, 6)=xlSheet1. Cells (ZXGS, 2) 'to write programs in the list of data into the automatic batch
XlSheet. Cells (2, 6)=xlSheet1. Cells (ZXGS, 3)
XlSheet. Cells (3, 6)=xlSheet1. Cells (ZXGS, 4)
XlSheet. Cells (4, 6)=xlSheet1. Cells (ZXGS, 5)
XlSheet. Cells (5, 6)=xlSheet1. Cells (ZXGS, 6)
XlSheet. Cells (6, 6)=xlSheet1. Cells (ZXGS, 7)
XlSheet. Cells (7, 6)=xlSheet1. Cells (ZXGS, 8)
XlSheet. Cells (8, 6)=xlSheet1. Cells (ZXGS, 9)
XlSheet. Cells (9, 6)=xlSheet1. Cells (ZXGS, 10)
XlSheet. Cells (10, 6)=xlSheet1. Cells (ZXGS, 11)
XlSheet. Cells (11, 6)=xlSheet1. Cells (ZXGS, 12)
HMM.=xlSheet1 Cells (ZXGS, 13) 'macro name

XlApp. Run GZB & amp; "!" & HMM 'run macro program

'error if the macro program prompts, not exit the program, but how the program automatically continue downward?

XlBook. Saved=True
XlBook. Close

End the If
Book1. Saved=True
Next ZXGS

Book1. Close
The Set xlBook=Nothing: Set Book1=Nothing
XlApp. Quit
The Set xlApp=Nothing

'MsgBox "total" & amp; WJHS & amp; "A program, have run over!"
End Sub

CodePudding user response:

In the macro program to use On the Error Resume Next capture the wrong measures

CodePudding user response:

On the Error Resume Next
Use On Error GoTo transfer code execution flow
On Error GoTo 0 to ban any has started in the process of current Error handler
在任何过程中,一旦错误处理程序处理了错误,在当前过程中就会从 Resume 语句指定的位置恢复运行,注意 一个错误处理程序不是 Sub 过程或 Function 过程,它是一段用行标签或行号标记的代码,错误处理程序依靠 Err 对象的 Number 属性中的值来确定错误发生的原因,在其它任何错误发生之前,或在调用一个可能会导致错误发生的过程之前,错误处理程序应该先测试或存储 Err 对象中相关的属性值,Err 对象中的属性值只反映最近发生的错误,Err.Description 中包含有与 Err.Number 相关联的错误信息,On Error Resume Next 会使程序从紧随产生错误的语句之后的语句继续执行,或是从紧随最近一次调用含有 On Error Resume Next 语句的过程的语句继续运行,这个语句可以置运行时错误于不顾,使程序得以继续执行,可以将错误处理程序放置在错误发生的地方,而不必将控件传输到过程中的其它位置,在调用另一个过程时,On Error Resume Next 语句成为非活动的,所以,如果希望在例程中进行嵌入错误处理,则应在每一个调用的例程中执行 On Error Resume Next 语句,注意 当处理在访问其它对象期间产生的错误时,与其使用 On Error GoTo 指令,不如使用 On Error Resume Next,每次和对象打交道,在不知道用代码访问哪个对象时,检查一下 Err 都会打消这种疑虑,可以确定是哪个对象产生错误(Err.Source 中指定的对象),也可以确定是哪个对象将错误代码放在 Err.Number 中,On Error GoTo 0 停止在当前过程中处理错误,即使过程中包含编号为 0 的行,它也不把行 0 指定为处理错误的代码的起点,

CodePudding user response:

Don't claim to do so,

You what is the cause of the fault check for errors, to clear up after the call before check whether the condition is,

CodePudding user response:

In excel macro setting error traps, the macro inside of error handling in a macro, don't let the noise of the macro outage,
If the macro outage, acer is to interrupt the operation period, the time has not yet returned to the vb, vb code won't continue to run,

But, in the macro placed on error resume next, exit excel as a combination of what is going to have a "found in the current position already exists, resume, XLW file,,," or aborts in excel,

Or listen to the upstairs, found the problem, correctly handle better

  • Related