
|
- To create an add-in that closes all the current project windows start a new project and select Add-In Type. Enter the following code into the frmAddIn that's created for you.
|
Private Sub Form_Load()
Dim w As Window
For Each w In VBInstance.Windows
' close either code or form design windows
If (w.Type = vbext_wt_CodeWindow Or w.Type = vbext_wt_Designer)
And w.Visible Then
w.Close
End If
Next
Unload Me
End Sub
|
Highlight the Connect Class in the object Browser, right click on it, and edit the Description property to change the name and description of the add-in. Also, search the template code and replace "My Add-In" with whatever you decided to call it. After building the DLL, you can add it from the Add-In Manager.
|
- The following code creates an VB add-in that provides an extended code search. The results list can be clicked using the mouse and the code module and line is displayed.
|
Start a New Project using the add-in template and create three command buttons on frmAddIn, and name them cmdSearch, cmdCancel, cmdClose.
Set the cmdCancel's Visible property to False and place it on top of the cmdSearch button.
Then add a text box for entry of the search term and a list box to display the results.
Finally, add a check box and the appropriate labels.
Add the following code to the cmdSearch_Click event.
|
Dim Proj As VBProject
Dim Comp As VBComponent
Dim n As Integer
Dim Item
Dim ProcName As String
'clear the list
List1.Clear
' set up cancel
cmdCancel.Visible = True
Label3.Caption = "Searching..."
DoEvents
mCancel = False
' This code line search through all projects
For Each Proj In VBInstance.VBProjects
' This line enables the search of the active project
Set Proj = VBInstance.ActiveVBProject
Label3.Caption = "Searching ..."
Label2.Caption = "Search Results in " & Proj.Name
For Each Comp In Proj.VBComponents
' Should I Quit
Label3.Caption = "Searching... " & Comp.Name
DoEvents
If mCancel Then Exit For
' Check to see if search target is in file at all
If Comp.CodeModule.Find(Text1, 1, 1, Comp.CodeModule.CountOfLines, 1) Then
' if found then look for the line(s)
For n = 1 To Comp.CodeModule.CountOfLines
' should I Quit
DoEvents
If mCancel Then Exit For
If InStr(1, Comp.CodeModule.Lines(n, 1), Text1, vbTextCompare) <> 0 Then
' show the module, procedure and line
ProcName = Comp.CodeModule.ProcOfLine(n, vbext_pk_Proc)
If ProcName = "" Then ProcName = "(Declarations)"
List1.AddItem
Comp.Name & " - " & ProcName & " - " &
Trim$(Comp.CodeModule.Lines(n, 1))
' Store the line number for retrieval
List1.ItemData(List1.ListCount - 1) = n
End If
End If
End If
Next n
End If
End If
Next
Next
Set Proj = Nothing
Set Comp = Nothing
cmdCancel.Visible = False
Label3.Caption = "Search Complete ... " & List1.ListCount & " Lines Found"
|
Now add code to jump to the appropriate code window when the list box is clicked.
|
Private Sub List1_Click()
Dim Proj As VBProject
Dim Comp As VBComponent
Dim Pane As CodePane
Dim StartText As Integer
Dim EndText As Integer
Dim n As Integer
Set Proj = VBInstance.ActiveVBProject
Set Comp = Proj.VBComponents(Mid$(List1, 1, InStr(1, List1, "-", vbTextCompare) - 2))
Set Pane = Comp.CodeModule.CodePane
Pane.Show
Pane.TopLine = List1.ItemData(List1.ListIndex) - Int(Pane.CountOfVisibleLines / 2)
StartText = InStr(1, Comp.CodeModule.Lines(List1.ItemData(List1.ListIndex), 1),
Text1, vbTextCompare)
EndText = StartText + Len(Text1)
Pane.SetSelection List1.ItemData(List1.ListIndex),
StartText, List1.ItemData(List1.ListIndex), EndText
If Check1 = 1 Then
Me.Show
Else
Me.WindowState = vbMinimized
End If
Set Pane = Nothing
Set Comp = Nothing
Set Proj = Nothing
End Sub
|
Finally add code that cancels a search and closes the form.
|
Private Sub cmdCancel_Click()
' setting this will stop the search
mCancel = True
End Sub
Private Sub cmdClose_Click()
'Stop the Search
mCancel = True
DoEvents
' hide the addin
Connect.Hide
End Sub
|
|
|