Transition from Excel plug-in development to VSTO in VB.net

Basic knowledge of Com DLL

 Com is essentially a dynamic link library, which is implemented by DLL in windows. This DLL implements the IUnknown interface, etc., as well as functions such as DllRegisterServer, DllRegisterServer, DllCanUnloadNow, and DllGetClassObject. The computer assigns a GUI identification code to each DLL, and then uses the DllRegisterServer function to write the Dll information into the registry, and uses DllUnregisterServer to uninstall the DLL. It is very cumbersome to implement a COM component in C and C++. The functions DllRegisterServer, DllRegisterServer, DllCanUnloadNow and DllGetClassObject must be exported.

Com DLL in Office

 Each Com DLL must provide several export functions mentioned above, and must implement the IDTExtensibility2 interface to become a DLL in an Office extension. To provide Ribbon tabs, you must implement the IRibbonExtensibility interface.
Insert picture description here
To become an Office plug-in, Com DLL must implement the IDTExtensibility2 interface provided in the figure above.
Insert picture description here
 This interface provides the above two functions, which can obtain the running instance of Office in Com DLL and clean up resources when uninstalling. Let's use Excel to illustrate.
 To provide a tab or Ribbon interface in Office, you must implement the IRibbonExtensibility interface in Office.
Insert picture description here
Insert picture description here
 The design of the Ribbon option interface is realized through this interface.

VB 6.0 Excel Com DLL

 Start VB6.0 and select the Addin project, double-click the Conect designer, set the interface according to the following figure and save the code in the situation, the designer has implemented the IDTExtensibility2 interface. Go to the saved directory and open the Connect.Dsr file with Notepad, which essentially contains it.
Insert picture description here
Insert picture description here
 This file saves the contents to be modified for the registration of DllRegisterServer and DllRegisterServer functions. The DLL in VB6.0 automatically realizes 4 functions that need to be exported in C and C++. Follow the above steps to implement the interface to implement the Excel add-in program.

Regrets of VB6.0

 VB 6.0 is no longer updated by Microsoft, and can only write Com add-ins for 32-bit Office programs and the compatibility is very poor now. Its solution is upgraded to the .net platform. Microsoft provides templates for VSTO specifically reserved for Office add-ins.

VSTO template in VB.net

Insert picture description here
 It is recommended to install VS above 2017 and install Visual Basic language and VSTO template. Its grammatical rules are the same as VB6.0, but the programming ideas have changed. The DLL add-in developed in VB.net is compatible with Office 32 and 64-bit versions, and its compatibility is very good. After setting up the project, as shown in the figure:
Insert picture description here
 VB.net's code encapsulation place is similar to VB6.0 in class modules or standard modules, but in .net, objects are everywhere, and it takes time to adapt. In the VSTO of VB.net, Microsoft has implemented the basic structure of Com DLL. The only thing we have to do is to realize our own business requirements, and the generated DLL needs to rely on the running environment as shown in the figure.
Insert picture description here
 The Office Runtime environment is not automatically installed in Office 2010. You need to manually install the environment for the first deployment, and you don't need to install the plug-in later. The first installation also needs to install the .net Framworker environment.
 The Excel plug-in packaged in VB.net obtains an Excel instance when its dll is loaded. The instance is packaged as shown in the figure: Create a standard module in the project and declare the Excel instance:
Insert picture description here

Insert picture description here
 When the DLL is loaded into the Excel process, the method of obtaining the Excel instance is shown in the figure: any
Insert picture description here
 method can be used to obtain the running Excel instance.
 The above is the basic architecture of the Office plug-in. On this architecture, it is easy to implement function extensions. If you are interested in joining groups: 794568082 to communicate with each other.

Guess you like

Origin blog.csdn.net/qq_25686631/article/details/112007447