This drill can be created with VB to create a COM add-in. The COM add-in can extend the application of the application without adding complexity to the user. The substance, the COM add-on is a dynamic link library (DLL), requiring registration to load and use Microsoft® Office XP applications. Although the load item can be written as an executable file (.exe), DLL usually provides better performance than .exe files.
This exercise will use VB to create and run a custom toolbar COM add-on. The task in this topic assumes that you are familiar with Office applications, VB projects, and debug and run code.
introduction
You can use any language that supports COM when you create a COM load item, such as VBA, Visual Basic, and Microsoft® Visual C ®. The load item created with Office Developer will be packaged into a DLL and loaded after the host application is registered. You can create an add-in but can be used in multiple applications. For more information. See Creating a COM Add for Multiple Applications.
This exercise will complete a series of steps to create a COM add-in with VB. The basic steps for creating a COM add-on include:
1. Configure the external programmaker. 2. Write the code in the external programmer. 3. Integrate the command strip control into the add-in. 4. Debug and test the COM loading item. 5. Generate a DLL for the COM load. 6. Solve the development of COM add-in. Configure an external programmer
The project created with an external programmer provides the work area for the development and add-in item. Using an external programmer can create a COM add-in to VBA or any Office application. Each external programmer in the project represents a separate and can only be run in an Office application. The created DLL can contain multiple add-on, which use the same form, module, and class modules, but it is different applications. This drill uses Excel as a host office application.
If you want to add items to be used for multiple applications, you must add an external program designer to each host application. You can share code by module; but in each project, you must reference the object model specific to each host application.
Configure an external programmer
1. Open VB2. In the File menu, select "New Project" and select Add Act. 3. Remove the form and double-click Connect in the designer. 3. Type the name Greeting Toolbar in the "Add-Program Display Name" text box, type the name Greeting Toolbar in the "Add-described" text box] Toolbar Add-in That Launches A Hello World Message in Excel. 4. Select Microsoft Excel from the Application list. 5. Select Microsoft Excel 10.0 from the Application Version list. 6. Select "Startup" from the "Initial Load Behaviors" list. For more information on various types of initial loading behaviors, see Specifying the load behavior. The related pictures of this topic are as follows: 7. In the Project menu, select "Reference" to ensure that the following types of libraries are enabled. (This reference list is required. For your add-on, make sure to select the type library required for each Office application that can use the add-on.) ● Visual Basic for Applications ● Ole Automation ● Microsoft Add-in Designer ● Microsoft Office 10.0 Object Library ● Microsoft Excel 10.0 Object Library 9. Select "Save Connect" in the File menu. 10. In the Project Save As dialog box, enter the name Greetings.dsr, select the folder you want to save, and then click Save. Writing the code in the external programmaker In the creation of the project and assigns a value for the Add-Explorer Designer, you can add a code to connect the add-in to the host application. This drill will show you the typical processes and events required to connect the add-in to the host application. The IDTEXTENSibility2 interface provides the COM objects and events that connect to this. Then, the load item can be engaged with the host application displayed by the host application. You can view the object model of a specific application in an object browser.
The code in the external programmer can handle the integration of the add-in and the host application. For example, the code that is running or unloading the add-on is located in the module of the "Outline Designer". If the add item contains a form, the add-in designer can also include code for displaying the form.
Declare variables and setup process stub
1. In the Project Explorer window, select Connect, then open the View menu, click Code. Clear the original code. 2. Reference Scalability Interface in the General Declaration section. Implements IDTextensibility23. Adding a module-level variable for providing communication between add-in and hosts. As long as the COM add-on, the variable assigned to be AS Excel.Application has always exists, so all processes can determine which application is currently running in which application is currently being run. Because the WitHevents keyword is assigned to the CbbButton variable, the Click event process of the menu item will trigger when the user clicks on the new menu item. 'Global Object ReferencesPublic ApphostApp As Excel.ApplicationPrivate WitHevents Cbbbutton As Office.commandbarbutton4. In the Code window, select IDTextensibility2 from the Object list, select OnConnection from the Events list. This will create an OnConnection event process to stub. Note You must include event procedure in each event provided by the IDTEXTENSIBILITY2 interface. If you delete any event procedure, the project cannot be compiled. 5. Add event procedure for each event: ● OnDisconnection ● OnStartupComplete ● Onbeginshutdown ● Onaddinsupdate now you can add a function to your loading item. Integrate the command strip control into the loading.
If your COM loading item has a user interface, you can add code for displaying the command bar to make it easy to run your add-on. This drill will display how to include code for creating a new command strip control (toolbar button or menu item) in the host application. When you load your add-on, you will load the control at the same time, and the user can open and use the add-in to click the button or menu item.
Create a command strip control
1. Find the ONCONNECTION event process. Between the private sub and end sub lines, add code to create a new command strip control and assign it to the CommandBarButton object variable that supports events. The entire process will appear as follows: private sub idtextensibility2_onconnection (Byval _ Application As Object, Byval ConnectMode As _ AddindesignerObjects.ext_ConnectMode, Byval AddininSt_As Object, Custom () AS VARIANT)
'Storage Start Reference Set ApphostApp = Application
'Add Command Bar Set Cbbutton = CreateBar () End Sub If you are familiar with the use of Visual Basic, you may notice that when you create an add-in, you don't have to set the value of the command strip button onaction attribute. This is because the event is automatically hanging.
2. Find the onDisconnection event process. Between the line lines of the private sub and end sub, add the code to delete the command strip control while uninstalling the add-on. The entire process will appear as follows: private sub idtextensibility2_ondisconnection (Byval _ RemoveMode as AddesignerObjects.ext_disconnectMode, _ Custom () AS VARIANT)
RemoveToolbar 'Removes the reference to set apphostapp = nothing set cbbutton = Nothing SUB3. Usually, as long as the function is stored in the code, you will add the code you call the function corresponding to the function you call. This function will create a command bar, set the properties of the command button, and provide the processing of the error message. Public Function CreateBar () AS Office.commandbarButton 'Specify Command Bar Dim CBCMYBAR As Office.commandbar Dim BtnmyButton As Office.commandbarButtonON Error Goto CreateBar_ERR
Set cbcmybar = apphostapp.commandbars.add (name: = "greetingbar")
'Specifies the command bar buttons Set btnMyButton = cbcMyBar.Controls.Add (Type: = msoControlButton, _ Parameter: = "Greetings") With btnMyButton .Style = msoButtonCaption .BeginGroup = True .Caption = "& Greetings" .TooltipText = "Display Hello World ".Width =" 24 "end with
'Display and return to the command bar cbcmybar.visible = true set createbar = btnmyButton EXIT FUNCTION
CreateBar_err: msgBox err.number & vbcrlf & err.descriptionnd Function4. Add the code corresponding to the function you call during the OnDisConnection. This function deletes a command strip when loading a macro is uninstalled. Private function removetoolbar () AppHostapp.comMandbars ("GreetingBar"). Deletend function5. Add a click event process to your COMMANDBARBATTON object. This process will be called when you click the new command strip button. The following code will display a message to indicate that the event is in progress: private sub cbbbutton_click (Byval Ctrl as _ Office.commandbutton, Canceldefault As Boolean) Msgbox ("Hello World!") End Sub6. Save your project. Now, the COM add item is completed. The rest of the step is to debug and test the code to ensure that it is running smoothly and then puts the load item into the DLL file, which can be distributed and used on other Office XP.
Commissioning and testing the loading item
When you develop a COM add-in in VB, you can debug the add-in by placing the project in the run mode. If the project is running mode, you can load the COM load from the Office application and use it, test and debug it by using any VB debugging tool.
Use VB debugging and test COM loading
1. Place any desired breakpoint, STOP statement or monitoring in the code. (You can do this first) 2. On the Projects menu, click Properties, in the Debug dialog box, select "Wait for the Part to be created", click "OK" 2. "Run "On the menu, click" Full Compilation Execution ". This will compile the project (a warning will be made when any compilation error occurs), and then the project is placed in operation mode. 3. Check if [Run] "appears in the title bar of the VB. Note You must publish the add-on, the host application can use it. 4. Start a new Excel instance. Because the loading behavior of the add-on is set to "Startup", as long as the application is launched, the add -hat will be loaded, and the onConnection event will occur, so the Greetings button appears. Now, you can use breakpoints and STOP statements added to debug code. If you click the Greetings button, you will run the project in VB, the Hello World message will appear before the editor (not before Excel). After the engineering is generated as a DLL file, the message will appear before Excel. 5. After completing debugging and testing, open the Run menu, click Termination Engineering. This clears the temporary file and registry key and places the project in the correct state to generate a DLL file. Generate a DLL for COM loading
After writing and debugging the code, you can make your loading item into a DLL to deploy it to other installed procedures.
Package the COM load in VB to DLL
1. From the File menu, select "Generate MyAddin.dll". 2. Save the file name Greetings in the Generation Project dialog box and select the location where you want to save the project. 3. Click OK. (At this point, the load item can be officially used in this step will create a COM add-in, add the appropriate registry key, and make the COM add item can be used in the Office host. When you create an add-on DLL, VB is used to register the DLL as a COM add-in. VB writes the name of the add-in to the registry, description, and initial loading behavior settings. The host application of the Add item reads these registry keys and loads the corresponding add-on.
For information on distributing COM add-on, see Deploying an application and deploying a COM loading.
Commonly added item development problem troubleshooting
Some error messages or accidents may be encountered when they work in the development environment and switching between applications. Here are some common problems and their solutions.
The compilation error or statement ends is invalid in the editor
To ensure that the appropriate object library is referenced for the project.
There is no reaction when running the project and open a new host application instance.
● To make sure that the word "published" appears in the title bar of the running add-on. If this is not this, the project must be run. ● Click Each open application window. Your object may be running, but only when you click the first instance of the open application, it is visible. ● To ensure that the module-level variables are specified correctly and set. ● In the host application, make sure you have selected your add-in in the COM Add-in dialog. You can customize the host application by adding a COM add-in command to the toolbar. This command will open the COM Add-on dialog. Add the COM Add-in dialog to the toolbar
1. Open the Excel Tools menu, click Custom, and select the Commands tab. Under "Category", select "Tools". Roll down down the "Command" list to find the "COM Add". Drag the "COM Add Item" into the toolbar to create a new button, then close the Custom dialog. 2. On this toolbar, click the "COM Add Item" button just added. 3. In the COM Add-in dialog box, confirm the check box next to your load item. To uninstall the add-on, clear the check box. Objects appear in host applications but no response
There may be multiple object instances run. The code for detecting and removing existing names may not run. Check and debug your OnDisconnection code.
(Please contact http://office.9zp.com)