When software developers consider extending Microsoft Office, the most common method is to use VB. In fact, it is quite easy to use C ++ and ATL to expand the office. I will show you how to create a project and register the Office plug-in as a COM Object to the office. (This article uses outlook as the main program of the plug-in .) Project Establishment
Let's create a new ATL Project. First, add a simple COM object named plugin. Use the Class Wizard to add the _ idtextensibility2 interface to the plugin class. (Do not forget to select the isupporterrorinfo check box .) The class wizard adds the following methods to the plugin class: onconnection, ondisconnection, onaddinsupdate, onstartupcomplete, and onbeginshutdown. This article only deals with onconnection and ondisconnection methods. Then, create a type definition (typedef, which is only used to simplify the Code) in the plugin header file, as shown below: # Define appid 102 Class cplugin; Typedef idispeventimpl <appid, cplugin, & diid_applicationevents, & Libid_outlook, 9, 0> outlooksink; Now we need to add the outlooksink class to the inheritance list of the plugin class: Class atl_no_vtable cplugin: Public iplugin, Public idispatchimpl <_ idtextensibility2, &__ uuidof (_ idtextensibility2 ), & Libid_addindesignerobjects,/* wmajor = */1,/* wminor = */0>, Public outlooksink In the plugin. h file, introduce the Declaration for mso9.dll and msoutl9.olb, as shown below: # Import "C: \ Program Files \ common files \ designer \ msaddndr. dll "\ Raw_interfaces_only ,\ Raw_native_types, no_namespace, named_guids, auto_search # Import "C: \ Program Files \ Microsoft Office \ mso9.dll "\ Rename_namespace ("Office") named_guids # Import "C: \ Program Files \ Microsoft \ Office \ msoutl9.olb "\ Rename_namespace ("Outlook"), raw_interfaces_only ,\ Named_guids Using namespace office; Using namespace outlook; Note: You need to follow the file path in your system to correct the paths of the introduced files above. You must register the office extension as a COM component to the office. Visual c ++ automatically generates the. RGS file to control com registration. Add additional projects to the. RGS file to support Automatic Registration of office. Under the HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ office key, there are several child keys, each of which corresponds to each office application installed in the system. These keys contain the add-in key ). To register our COM Object in office, we need to add a new key to the plug-in key. There is also a corresponding tree under HKEY_CURRENT_USER to control the plug-in of a single user, while HKEY_LOCAL_MACHINE controls the plug-in of the entire machine. The following is the outlook plug-in registration code in the. RGS file: Hkcu { Noremove Software { Noremove Microsoft { Noremove Office { Noremove Outlook { Noremove addins { Forceremove outlookdemo. plugin.1 { Val friendlyname = S' outlook in' Val description = S' an outlook in' Val loadbehavior = D 3 Val commandlinesafe = D 0 }
} } } } } } This set of registration items registers plug-ins for specific users. To set the plug-ins to be available throughout the system, you only need to change the hkcu to HKLM. The loadbehavior value controls the actions of the plug-in after the main program runs. To enable the agent to run automatically after the main program starts running, set loadbehavior to 3. If you want to manually load the plug-in, set loadbehavior to 8. If you compile this project, you have an outlook plug-in that does not actually do anything, but it is indeed a working plug-in. We now add some features to this plug-in. First, add a new member variable: _ Applicationptr m_papp; Then set the onconnection method: Stdmethod (onconnection) (lpdispatch application, Ext_connectmode connectmode, Lpdispatch addininst, Safearray ** custom) { Hresult rslt = s_ OK; Try { M_papp = application; Outlooksynch: dispeventadvise (m_papp ); } Catch (_ com_error & E ){ Outputdebugstring (E. errormessage ()); Rslt = e_unexpected; } Return rslt; } Set the corresponding ondisconnection method as follows: Stdmethod (ondisconnection) (ext_disconnectmode removemode, safearray ** Custom) { Hresult rslt = s_ OK; Try { Outlooksynch: dispeventunadvise (m_papp ); } Catch (_ com_error & E ){ Outputdebugstring (E. errormessage ()); Rslt = e_unexpected; } Return rslt; }
Now we can receive several events, such as onnewmail events. The best way to get event information and their IDs is to open the OLE/COM viewer on the msvc tool menu ).
- Go to the File menu and select View typelib.
- Browse the msoutl9.olb file.
- Scroll down the dispinterface of applicationevents. Note that the ID of the onnewmail event is 0x0000f003 ..
- Add an onnewmail method to the cplugin class.
- Create a sink ing to associate the onnewmail method with the onnewmail event.
Begin_sink_map (cplugin) Sink_entry_ex (appid, Outlook: diid_applicationevents ,/ 0x0000f003, onnewmail) End_sink_map () Now we only need to complete the onnewmail event processing function. Void _ stdcall onnewmail (){ MessageBox (null, "new mail", null, mb_ OK ); } To remove these annoying pop-up dialog boxes (created by this project), follow these steps:
- Go to the tools | Options menu of outlook.
- Select the other tag.
- Click the advanced Options button.
- Click com plug-in.
- Select the outlook plugin item and click Remove.
As you can see, implementing the Office com plug-in is not difficult or even easy. It is most difficult to create a Com office plug-in to calculate the initial values of all parameters. However, once you find that you can get this information in the OLE/COM viewer, this will not be a problem. |