Write the Word plug-in with VC6.0 (Office2K, XP, 03)
Author: hjphy
Recently, due to work needs, I learned how to compile the Office plug-in. After a lot of detours, I finally figured out the principle of writing plug-ins and did not dare to share them with you. The following uses word 2003 as an example to describe it briefly.
Step 1: Use the Wizard to generate a new project of atl com Appwizard.
Figure 1
In the first dialog box of the wizard, select dynamic link library (DLL) as the server type, and click Finish.
Figure 2
Then, select the menu insert-> new ATL object, select the simple object option corresponding to the corresponding objects on the right in the pop-up ATL object wizard dialog box, and click Next.
Figure 3
In the displayed dialog box, enter the corresponding name and click OK to insert the ATL object.
Figure 4
In this way, a simple Atl-based COM component project is successfully established.
Step 2: Use the Import Type Library to implement the _ idtextensibility2 interface. Right-click the newly added class in the classview and select the implement interface item from the shortcut menu.
Figure 5
In the displayed implementation interface dialog box, click Add typelib.
Figure 6
In the displayed browse type libraries dialog box, select the Microsoft add-in designer (1.0) subitem and click OK.
Figure 7
In the displayed interface List dialog box, select the _ idtextensibility2 interface and click OK to complete the import.
In this way, the system will automatically generate five empty interface functions for you, namely onconnection, ondisconnection, onaddinsupdate, onstartupcomplete, and onbeginshutdown.
Step 3: Through the above two steps, our plug-in framework has been formed, but how does office know to load our plug-in during startup? How can I load my own plug-ins to different office components, such as Word, Excel, and outlook? The answer is to add the corresponding key value to the Registry. Open the RGS file in the file view FileView> resource file and add the following code:
HKCU{Software{Microsoft{Office{Word{Addins{''TestAddin.SimAddin''{val FriendlyName = s ''WORD Custom Addin''val Description = s ''Word Custom Addin''val LoadBehavior = d ''00000003''val CommandLineSafe = d ''00000001''}}}}}}}
The above code consists of three points worth attention:
1. The sub-item under the Office indicates that the plug-in belongs to the component, such as Word, Excel, and outlook.
2. the subitem under addins should be written as the name of the COM component you added. Do not copy it according to the name of my project.
3. All values are enclosed in single quotes and must be enclosed in single quotes.
The framework of such an office plug-in is complete. You can add some test code in the onconnection function to check whether the code has been executed. If the code is successfully executed, you can continue. Otherwise, check whether the preceding steps are correct.
Step 4: import two office files, one is MSO. dll and the other is MSWord. olb. These two files can be found in the following locations (the specific location is related to the Office installation path ):
C: \ Program Files \ common files \ microsoft shared \ office11
C: \ Program Files \ Microsoft Office \ office11
Then add the following statement to stdafx. h:
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\mso.dll"\rename_namespace("Office") named_guids,exclude("Pages")using namespace Office;#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" rename_namespace("VBE6")using namespace VBE6;#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSWORD.OLB" rename("ExitWindows","ExitWindowsEx")#import "C:\\Program Files\\Microsoft Office\\OFFICE11\\MSWORD.OLB"\rename_namespace("Word"), raw_interfaces_only, named_guids ,exclude("Pages")using namespace Word;
Compile the above Code to check whether the code is successful. The reason for the introduction of these two files is mainly to introduce some variable types and prepare for the subsequent UI creation.
The last step is to write the code. Add the following code to onconnection:
CComPtr < Office::_CommandBars> spCmdBars;CComQIPtr <Word::_Application> spApp(Application);ATLASSERT(spApp);HRESULT hr = spApp->get_CommandBars(&spCmdBars);if(FAILED(hr))return hr;ATLASSERT(spCmdBars);CComVariant vName("MyAddin");CComPtr <Office::CommandBar> spNewCmdBar;CComVariant vPos(1);CComVariant vTemp(VARIANT_TRUE);CComVariant vEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);spNewCmdBar = spCmdBars->Add(vName, vPos, vEmpty, vTemp);CComPtr < Office::CommandBarControls> spBarControls;spBarControls = spNewCmdBar->GetControls();ATLASSERT(spBarControls);CComVariant vToolBarType(1);CComVariant vShow(VARIANT_TRUE);CComPtr < Office::CommandBarControl> spNewBar;spNewBar = spBarControls->Add(vToolBarType, vEmpty, vEmpty, vEmpty, vShow);ATLASSERT(spNewBar);CComQIPtr < Office::_CommandBarButton> spCmdButton(spNewBar);ATLASSERT(spCmdButton);HBITMAP hBmp =(HBITMAP)::LoadImage(_Module.GetResourceInstance(),MAKEINTRESOURCE(IDB_BITMAP),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);::OpenClipboard(NULL);::EmptyClipboard();::SetClipboardData(CF_BITMAP, (HANDLE)hBmp);::CloseClipboard();::DeleteObject(hBmp);spCmdButton->PutStyle(Office::msoButtonIconAndCaption);hr = spCmdButton->PasteFace();if (FAILED(hr))return hr;spCmdButton->PutVisible(VARIANT_TRUE);spCmdButton->PutCaption(OLESTR("myAddin"));spCmdButton->PutEnabled(VARIANT_TRUE);spCmdButton->PutTooltipText(OLESTR("test1"));spCmdButton->PutTag(OLESTR("test1"));spNewCmdBar->PutVisible(VARIANT_TRUE);m_spCmdButton = spCmdButton;
In this way, open Word again to see the interface effect shown in Figure 1.
Figure 9
However, there is no response when you click it. Finally, let's solve this problem.
1. Add IDispEventSimpleImpl inheritance to the COutlookAddin inheritance class. The Code is as follows:
class ATL_NO_VTABLE COutlookAddin :public CComObjectRootEx<CComSingleThreadModel>,……public IDispEventSimpleImpl<1,COutlookAddin,&__uuidof(Office::_CommandBarButtonEvents)>
2. Declare the callback parameter information of the _ ATL_SINK_INFO structure. Add the following statement to the OutlookAddin. h file:
// Button Event Response Information declaration extern _ ATL_FUNC_INFO OnClickButtonInfo;
Add the Definition Statement to the OutlookAddin. cpp file as follows:
// Button Event Response Information definition _ ATL_FUNC_INFO OnClickButtonInfo = {CC_STDCALL, VT_EMPTY, 2, {VT_DISPATCH, VT_BYREF | VT_BOOL }};
3. Add the Sink ing as follows:
EGIN_SINK_MAP(COutlookAddin)SINK_ENTRY_INFO(1, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/0x01, OnClickButton1, &OnClickButtonInfo)SINK_ENTRY_INFO(2, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/0x01, OnClickButton2, &OnClickButtonInfo)SINK_ENTRY_INFO(3, __uuidof(Office::_CommandBarButtonEvents),/*dispid*/0x01, OnClickMenu, &OnClickButtonInfo)END_SINK_MAP()
4. Add event functions. Add the Declaration in OutlookAddin. h:
void __stdcall OnClickButton1(IDispatch * /*Office::_CommandBarButton**/Ctrl,VARIANT_BOOL * CancelDefault);
Add the following implementation to OutlookAddin. cpp:
// Toolbar button 1 click the Event Response Function
Void _ stdcall CWordAddin: OnClickButton1 (IDispatch */* Office: _ CommandBarButton **/Ctrl, VARIANT_BOOL * CancelDefault) {MessageBox (NULL, "hello", "world ", MB_ OK );}
5. Finally, open or disconnect the interface. The method is as follows:
In the last part of the OnConnection interface function, add the following code to open the connection:
To sum up, it is not difficult to compile a simple office plug-in. As long as you follow the steps one by one, it will certainly succeed. If you have any questions during use, please join us.