VBA:如何使VBE中的当前光标跳转到最后一个错误发生的行?

时间:2022-02-21 00:37:26

This is still related with my previous questions, VBA: How to display an error message just like the standard error message which has a “Debug” button?

这仍然与我之前的问题有关,VBA:如何显示错误消息,就像标准错误消息有一个“调试”按钮?

Now, I successfully make the current cursor in VBE jump to a particular procedure in VBE. I used Application.Goto to achieve this. However, what actually I want is to make the current cursor in VBE jump to the line where the last error occured. I suspected there should be something useful for this purpose in Application.VBE object but didn't know which?

现在,我成功地使VBE中的当前光标跳转到VBE中的特定过程。我使用Application.Goto来实现这一目标。但是,实际上我想要的是让VBE中的当前光标跳转到发生最后一次错误的行。我怀疑在Application.VBE对象中应该有一些有用的东西,但不知道哪个?

Solving this also means satisfying my previous question entirely. Any hints or even dirty tricks?

解决这个问题也意味着完全满足我之前的问题。任何提示甚至是肮脏的技巧?

1 个解决方案

#1


5  

Continued from your previous question :)

继续你以前的问题:)

I suppose you are already using line numbering (as answered in the previous question).

我想你已经在使用行号(如上一个问题所述)。

So, modify your error handling routine to something like:

因此,将错误处理例程修改为:

Sub aa()
Dim zz As Long

10:     On Error GoTo ErrorHandler
20:     DivisionByZero = 1 / 0
30:     Exit Sub
ErrorHandler:
41:  If Err.Number <> 0 Then
42:     Msg = "Error # " & Str(Err.Number) & " was generated by " _
         & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
43:     MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
        zz = CodeFind("", "", Str(Erl), 0)
44:     End If
50:     Resume Next
End Sub

Now for the CodeFind() thing. I found it here (at the bottom, the last one), but had to modify it a bit, so I'm posting a lot of code ... sorry.

现在为CodeFind()的事情。我在这里找到它(在底部,最后一个),但不得不修改它,所以我发布了很多代码......对不起。

Insert this code in a new module and be sure you have the reference to "Microsoft Visual Basic For Applications Extensibility 5.3" checked, and the project is not protected. In doubt see here.

将此代码插入新模块中,并确保已选中“Microsoft Visual Basic For Applications Extensibility 5.3”,并且该项目不受保护。有疑问见这里。

HTH!

HTH!

Here is the code

这是代码

 Option Explicit
 '---------------------------------------------------------------------------------------
 ' Procedure : CodeFind
 ' DateTime  : 7/5/2005 18:32
 ' Author    : Nelson Hochberg
 ' Purpose   : Find a module, a procedure and/or a string in code and highlight it
 ' Returns   : 0 if not found,  line number in module if found
 ' Syntax    : lngReturn = CodeFind ([FindMod],[FindProc],[FindStr],[TypeOfSearch])
 ' Arguments : Optional FindMod As String: Part of a name of a module
 '             Optional FindProc As String: Part of a name of a procedure
 '             Optional FindStr As String: Part of a string to search
 '             NOTE: One of the above three is required
 '             Optional TypeOfSearch As Long: -1 Find line number, 0 Find string,
 '                      >0 Continue search starting at line number: TypeOfSearch + 1
 ' Thanks    : To stevbe at Experts Exchange for the initial code.
 '---------------------------------------------------------------------------------------
 '
 Public Function CodeFind( _
 Optional FindMod As String = "", _
 Optional FindProc As String = "", _
 Optional FindStr As String = "", _
 Optional TypeOfSearch As Long = 0 _
 ) As Long

 Dim vbc As VBIDE.VBComponent
 Dim cm As VBIDE.CodeModule
 Dim VBAEditor As VBIDE.VBE
 Dim VBProj As VBIDE.VBProject


 Dim startline As Long, startcol As Long, endline As Long, endcol As Long

 If FindMod <> "" Then
     CodeFind = FindModule(FindMod, vbc, cm)
         If CodeFind = False Then Exit Function
     If FindProc <> "" Then
         CodeFind = FindProcedure(FindProc, startline, startcol, endline, endcol, cm)
             If CodeFind = False Then Exit Function
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
                 If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     Else
         startline = 1
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
             If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     End If
 Else
     Set VBAEditor = Application.VBE
 '''''''''''''''''''''''''''''''''''''''''''
     Set VBProj = VBAEditor.ActiveVBProject
     For Each vbc In VBProj.VBComponents


         Set cm = vbc.CodeModule
         If FindProc <> "" Then
             CodeFind = FindProcedure(FindProc, startline, startcol, endline, endcol, cm)
             If CodeFind = False Then GoTo Nextvbc2 Else Exit For
         Else
             startline = 1
             If FindStr <> "" Then
                 CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
                     If CodeFind = False Then GoTo Nextvbc2 Else Exit For
             Else
                 MsgBox "CodeFind: At least one of the following is required:" & vbCrLf & _
                     "    Module" & vbCrLf & "    Procedure" & vbCrLf & "    String"
                 CodeFind = False
                 Exit Function
             End If
         End If
 Nextvbc2:
     Next vbc
     If CodeFind <> False Then
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
             If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     End If
 End If

 CodeLineFound:
 If CodeFind <> False Then
     If endline = -1 Then endline = 1
     If endcol = -1 Then endcol = 1
     cm.CodePane.Show
     cm.CodePane.SetSelection startline, startcol, endline, endcol
 End If

 End Function

#1


5  

Continued from your previous question :)

继续你以前的问题:)

I suppose you are already using line numbering (as answered in the previous question).

我想你已经在使用行号(如上一个问题所述)。

So, modify your error handling routine to something like:

因此,将错误处理例程修改为:

Sub aa()
Dim zz As Long

10:     On Error GoTo ErrorHandler
20:     DivisionByZero = 1 / 0
30:     Exit Sub
ErrorHandler:
41:  If Err.Number <> 0 Then
42:     Msg = "Error # " & Str(Err.Number) & " was generated by " _
         & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
43:     MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
        zz = CodeFind("", "", Str(Erl), 0)
44:     End If
50:     Resume Next
End Sub

Now for the CodeFind() thing. I found it here (at the bottom, the last one), but had to modify it a bit, so I'm posting a lot of code ... sorry.

现在为CodeFind()的事情。我在这里找到它(在底部,最后一个),但不得不修改它,所以我发布了很多代码......对不起。

Insert this code in a new module and be sure you have the reference to "Microsoft Visual Basic For Applications Extensibility 5.3" checked, and the project is not protected. In doubt see here.

将此代码插入新模块中,并确保已选中“Microsoft Visual Basic For Applications Extensibility 5.3”,并且该项目不受保护。有疑问见这里。

HTH!

HTH!

Here is the code

这是代码

 Option Explicit
 '---------------------------------------------------------------------------------------
 ' Procedure : CodeFind
 ' DateTime  : 7/5/2005 18:32
 ' Author    : Nelson Hochberg
 ' Purpose   : Find a module, a procedure and/or a string in code and highlight it
 ' Returns   : 0 if not found,  line number in module if found
 ' Syntax    : lngReturn = CodeFind ([FindMod],[FindProc],[FindStr],[TypeOfSearch])
 ' Arguments : Optional FindMod As String: Part of a name of a module
 '             Optional FindProc As String: Part of a name of a procedure
 '             Optional FindStr As String: Part of a string to search
 '             NOTE: One of the above three is required
 '             Optional TypeOfSearch As Long: -1 Find line number, 0 Find string,
 '                      >0 Continue search starting at line number: TypeOfSearch + 1
 ' Thanks    : To stevbe at Experts Exchange for the initial code.
 '---------------------------------------------------------------------------------------
 '
 Public Function CodeFind( _
 Optional FindMod As String = "", _
 Optional FindProc As String = "", _
 Optional FindStr As String = "", _
 Optional TypeOfSearch As Long = 0 _
 ) As Long

 Dim vbc As VBIDE.VBComponent
 Dim cm As VBIDE.CodeModule
 Dim VBAEditor As VBIDE.VBE
 Dim VBProj As VBIDE.VBProject


 Dim startline As Long, startcol As Long, endline As Long, endcol As Long

 If FindMod <> "" Then
     CodeFind = FindModule(FindMod, vbc, cm)
         If CodeFind = False Then Exit Function
     If FindProc <> "" Then
         CodeFind = FindProcedure(FindProc, startline, startcol, endline, endcol, cm)
             If CodeFind = False Then Exit Function
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
                 If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     Else
         startline = 1
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
             If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     End If
 Else
     Set VBAEditor = Application.VBE
 '''''''''''''''''''''''''''''''''''''''''''
     Set VBProj = VBAEditor.ActiveVBProject
     For Each vbc In VBProj.VBComponents


         Set cm = vbc.CodeModule
         If FindProc <> "" Then
             CodeFind = FindProcedure(FindProc, startline, startcol, endline, endcol, cm)
             If CodeFind = False Then GoTo Nextvbc2 Else Exit For
         Else
             startline = 1
             If FindStr <> "" Then
                 CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
                     If CodeFind = False Then GoTo Nextvbc2 Else Exit For
             Else
                 MsgBox "CodeFind: At least one of the following is required:" & vbCrLf & _
                     "    Module" & vbCrLf & "    Procedure" & vbCrLf & "    String"
                 CodeFind = False
                 Exit Function
             End If
         End If
 Nextvbc2:
     Next vbc
     If CodeFind <> False Then
         If FindStr <> "" Then
             CodeFind = FindString(FindStr, startline, startcol, endline, endcol, cm, TypeOfSearch)
             If CodeFind = False Then Exit Function
         Else
             GoTo CodeLineFound
         End If
     End If
 End If

 CodeLineFound:
 If CodeFind <> False Then
     If endline = -1 Then endline = 1
     If endcol = -1 Then endcol = 1
     cm.CodePane.Show
     cm.CodePane.SetSelection startline, startcol, endline, endcol
 End If

 End Function