----VisualBasic5.0 is a powerful integrated development environment that can automatically check for grammatical errors and prompt coders for grammatical formats. These functions greatly improve coding efficiency. However, as a quick development tool, its characteristics are also highlighted in the Add-In technology. Using it, we can strengthen and customize the VB development environment: we can automatically add a module to all PProjects; we can replace a module in each module with another module; we can find a certain piece of code in the program and add it to It replaces it; in theory, we can even use this technology to make an automatic code generator.
----The essence of Add-In technology is that the VB programming environment itself is exposed to users as an object model. We can use a module or a piece of code as an operation object to modify and optimize. Below, we introduce the use of Add-In technology through an example.
----For example, you want to include this piece of code in your program:
dimnIndexasinteger
nIndex=cStr(vsFlex.Text)
Replace with:
dimsIndexasstring
sIndex=Spread.text
----Most editing software, including Word, does not include the multi-line search and replace function, and the VB5.0 editor is no exception. What should I do?
----1. Open VB and select "AddIn" when selecting the Project type. In this way, we opened a Project named MyAddin. Change the Project name to what you want, such as "Test".
----2. Open Addin.bas and you will find a subroutine of AddToIni:
SubAddToINI()
DimErrCodeAsLong
ErrCode=WritePrivateProfileString("Add-Ins32","Myaddin.Connect","0","vbaddin.ini")
EndSub
----In the Windows directory, there is a file called vbaddin.ini. Each time VB starts, it will determine which AddIn programs are available based on the settings of this file. The function of AddToIni is actually to register the current program to vbaddin.ini. Modify "Myaddin.Connect" to "Test.Connect" so that it corresponds to the current Project name.
----3. Execute AddToIni in the Immediate window to complete the registration of the current Addin.
----4. Open Connect.cls, in the variable declaration area: ImplementsIDTExtensibility
----IDTExtensibility is the Interface for interaction between VB and AddIn programs. When VB selects AddIdManager in the Addin menu and refers to the current Addin program "Test", the OnConnect event will be triggered and the IDTExtensibility_OnConnection() program will be executed. Set a breakpoint on the following line:
----Debug.PrintVBInst.FullName
----Run the current program.
----5. Start another VB routine to generate StandardEXE named Project1. Select AddinManager from the Addin menu, we get a list, we select MyAddin and return. At this time, Test is started and stops at
----Debug.PrintVBInst.FullName
----Note that VBInstance (VBInst) refers to the VB integrated development environment (VBEObject) using the Addin. We can operate it as an object. VBEObject contains VBProjects, each VBProject contains Vbcomponents, each VBComponent corresponds to a module, each Form, Class, or Module.
----6. We execute in the immediate window of Test:
----? VBInstance.ActiveProject.Name
----You can get the name of Project1.
----? VBInstance.ActiveProject.VBComponent(0).Name
----You can get the name of the first module Form1 of Project1.
----So far, we have basically explained the technical connotation of AddIn. The rest is easy to understand.
----7. In the AddinManager of Project1, remove MyAddin.
----8. Modify Test again, open frmAddin, and change
PrivateSubOKButton_Click()
MsgBoxVBInstance.FullName
EndSub
Change to:
PrivateSubOKButton_Click()
CallmReplace()
EndSub
PrivateSubmReplace()
DimoProAsVBProject
DimoComAsVBComponent
DimbFindAsBoolean
DimnFindLineasinteger
DimnfindColasinteger
Screen.MousePointer=vbHourglass
OnErrorGoToerrmReplace
SetoPro=VBInstance.ActiveVBProject
'Check if the program has been saved
IfoPro.FileName=""ThenMsgBox"Please save first!";ExitSub
ForEachoComInoPro.VBComponents
bFind=True
DoWhilebFind
'Find the code segment to replace
bFind=oCom.CodeModule.Find("dimnIndexasinteger",nFindLine,nfindCol,oCom.CodeModule.CountOfLines,500,True
IfbFindThen
IfoCom.CodeModule.Lines(nFindLine 1,1)="nIndex=cStr(vsFlex.Text)"
'Replace after finding
oCom.CodeModule.ReplaceLine
nFindLine,"dimsIndexasstring"
oCom.CodeModule.ReplaceLine
nFindLine 1,"sIndex=Spread.text"
Else
bFind=false
EndIf
EndIf
Loop
NextoCom
Screen.MousePointer=vbDefault
ExitSub
errmReplace:
Beep
Screen.MousePointer=vbDefault
MsgBox"ERROR!"
EndSub
----9. Let's put Test into running state.
----10. If we repeat step 5, there will be an additional item "MyAddin" in the Addin menu of Project1. We add some code to be replaced in Project1, and then execute MyAddin, and the replacement work will be completed.
----11. Some friends do not want the word "MyAddin" to appear in the Addin menu, but a logo with a clearer meaning, such as "Replace". Change Test's ConnectClass----IDTExtensibility_OnConnection() to
SetmcbMenuCommandBar=AddToAddInCommandBar("MyAddIn")
Change to SetmcbMenuCommandBar=AddToAddInCommandBar("replace")
If you want to change MyAddin in AddinManager to
"Replace", you can open Connect.cls with notepad and change: AttributeVB_Description="MyAdd-In"
Change to: AttributeVB_Description="Replace"
----The above introduction is just a simple application of AddIn technology. After understanding its mechanism, you can make your own tools according to your needs. For some professional application software developers, the more AddIn tools they accumulate, the greater their coding efficiency will be. ->