如何使用自动化来修改 Office 菜单
2010-07-23 16:07
417 查看
请按照步骤 1 至 12 中基本的文章,以创建使用 IDispatch 接口和成员函数的示例项目定义 Excel 类型库下面的 Microsoft 知识步骤操作:
178749 (http://support.microsoft.com/kb/178749/EN-US/ ) 如何创建自动化项目使用 MFC 和类型库
重复步骤 8 和 9,10 个以上的文章,以将类型库添加到项目中的 Microsoft Office。Microsoft Office 97 的类型库是 Mso97.dll 在文件中。Microsoft Office 2000 的类型库是 Mso9.dll。默认位置是 Files/Microsoft Office/mso.dll。Microsoft Office 2002 的类型库是 Mso.dll。默认位置是 Files/Common Files/Microsoft Shared/Office10。
注意: 选择的所有该类型库的组件。您将生成,并将 Mso.h 和 Mso.cpp 文件添加到您的项目。
当您执行操作您将获得很多重复在 COleDispatchDriver 中包装类。这些会导致从 Microsoft Excel 2000 或 2002年中的 IDispatch 名称和 Microsoft Office 2000 或 Microsoft Office XP 的复制。例如对于两者都有一个名为 _Application IDispatch。
若要解决这些重复,在本练习中,使用 VC + + 所提供"的命名空间"工具。Excel.h 头文件的最顶部,在插入行:
[/code]
在该同一文件的最底部,添加只包含一个行右大括号和分号 (特别是"} ;"不带引号)
恰好在编译器指令"#endif"后的新行上的该 Excel.cpp 文件的开头添加以下行:
[/code]
添加以下行以 # 包括 AutoProjectDlg.cpp 程序文件的顶部的语句:
[/code]
将下面的代码添加到 CAutoProjectDlg::OnRun() 事件处理程序 AutoProjectDlg.cpp 文件中:
178749 (http://support.microsoft.com/kb/178749/EN-US/ ) 如何创建自动化项目使用 MFC 和类型库
重复步骤 8 和 9,10 个以上的文章,以将类型库添加到项目中的 Microsoft Office。Microsoft Office 97 的类型库是 Mso97.dll 在文件中。Microsoft Office 2000 的类型库是 Mso9.dll。默认位置是 Files/Microsoft Office/mso.dll。Microsoft Office 2002 的类型库是 Mso.dll。默认位置是 Files/Common Files/Microsoft Shared/Office10。
注意: 选择的所有该类型库的组件。您将生成,并将 Mso.h 和 Mso.cpp 文件添加到您的项目。
当您执行操作您将获得很多重复在 COleDispatchDriver 中包装类。这些会导致从 Microsoft Excel 2000 或 2002年中的 IDispatch 名称和 Microsoft Office 2000 或 Microsoft Office XP 的复制。例如对于两者都有一个名为 _Application IDispatch。
若要解决这些重复,在本练习中,使用 VC + + 所提供"的命名空间"工具。Excel.h 头文件的最顶部,在插入行:
[code] namespace XL { // that's an opening brace.
[/code]
在该同一文件的最底部,添加只包含一个行右大括号和分号 (特别是"} ;"不带引号)
恰好在编译器指令"#endif"后的新行上的该 Excel.cpp 文件的开头添加以下行:
[code] using namespace XL;
[/code]
添加以下行以 # 包括 AutoProjectDlg.cpp 程序文件的顶部的语句:
[code] #include "excel9.h" // for Excel 2002, include excel.h #include "mso.h"
[/code]
将下面的代码添加到 CAutoProjectDlg::OnRun() 事件处理程序 AutoProjectDlg.cpp 文件中:
// Common OLE-variants. Easy variants to use for calling arguments. COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); HRESULT hr; DISPID dispID = 0; LPDISPATCH lpDisp = 0; VARIANT vResult; // A Struct with a pdispVal member long cCount = 0; char buf[2048]; // General purpose message buffers OLECHAR *strCBs = L"CommandBars"; // Object declarations. XL::_Application app; // The XL prefix specifies the namerange. XL::Workbooks oWorkbooks; XL::_Workbook oBook; XL::Worksheets oWorksheets; XL::_Worksheet oSheet; XL::Shapes oShapes; XL::Shape oShape; // More objects will be declared throughout the program. // Create and show instance of Excel. if(!app.CreateDispatch("Excel.Application")) { AfxMessageBox("Failed to create Excel.Application"); return; } // Excel visibility makes Office Menu Bar visible. app.SetVisible(TRUE); oWorkbooks = app.GetWorkbooks(); oBook = oWorkbooks.Add(covOptional); oWorksheets = oBook.GetWorksheets(); oSheet = oWorksheets.GetItem(COleVariant((short)3)); oSheet.Activate(); // Make the sheet selection work // Find &strCBs, i.e. L"CommandBars" and put it in dispID. hr = app.m_lpDispatch->GetIDsOfNames(IID_NULL, &strCBs, 1, LOCALE_SYSTEM_DEFAULT, &dispID); if(FAILED(hr)) { sprintf(buf,"Failed to GetIDsOfNames() :(... Error = %08lx", (long)hr); AfxMessageBox(buf,MB_SETFOREGROUND); } // Get a dispatch pointer to CommandBars! Use that of running // application's (Excel) existing menu configuration. // "vResult" is a VARIANT. It's declared above. app.InvokeHelper(dispID, DISPATCH_METHOD | DISPATCH_PROPERTYGET, VT_VARIANT, (void*)&vResult, NULL); sprintf(buf, "CommandBars dispID = %ld /n" "CommandBars IDispatch pointer is %08lx", (long) dispID, (long)vResult.pdispVal); AfxMessageBox(buf, MB_SETFOREGROUND); CommandBars cbs(vResult.pdispVal); // Pre Office XP _CommandBars cbs(vResult.pdispVal); // Construct the CommandBars // object and attach the // IDispatch pointer to it. cCount = cbs.GetCount(); // 114 for Excel, Word has more!!?? // MSOffice reconfigures for each // user-application. sprintf(buf, "Count of CommandBars is %d", cCount); AfxMessageBox(buf, MB_SETFOREGROUND); vResult.pdispVal = cbs.GetActiveMenuBar(); // Returns a LPDISPATCH // pointer of the CommandBar object that // represents the active menu bar in the // container application; that is, MS Office's // Excel Menu Bar Configuration. sprintf( buf, "dispatch pointer to the ActiveMenuBar is %08lx", (long)vResult.pdispVal); AfxMessageBox(buf, MB_SETFOREGROUND); CommandBar oBar(vResult.pdispVal); // Construct a CommandBar object // & attach the LPDispatch // of the active menu bar. CString cBarName = oBar.GetName(); // "Worksheet Menu Bar" sprintf(buf, "Name of the menu bar is %s", (LPCTSTR)cBarName); AfxMessageBox(buf, MB_SETFOREGROUND); CString cBarNameLocal = oBar.GetNameLocal(); // "Worksheet Menu Bar" sprintf(buf, "Local language's name of the menu bar is %s", (LPCTSTR)cBarNameLocal); AfxMessageBox(buf, MB_SETFOREGROUND); long iMenuBarType = oBar.GetType(); // 1 sprintf(buf, "Type of Menu Bar is %d,", (long)iMenuBarType); AfxMessageBox(buf, MB_SETFOREGROUND); vResult.pdispVal = oBar.GetControls(); // CommandBarControls // IDispatch pointer // Construct a CommandBarControls object, and attach the IDispatch // pointer for CommandBarControls to that oBarcontrols object. CommandBarControls oBarcontrols(vResult.pdispVal); // Construct a CommandBarControl for the 6th item in the // ComandBarControls collection, // and attach a IDispatch pointer to it. CommandBarControl cbCtl=oBarcontrols.GetItem(COleVariant((short)6) ); CString ccCaption = cbCtl.GetCaption(); // "&Tools" long iiType = cbCtl.GetType(); // = 10 long iiIndex = cbCtl.GetIndex(); // 10 long iiId = cbCtl.GetId(); // 30007 CString ccTag = cbCtl.GetTag(); // blank sprintf(buf, "Caption of Control # 6 is %s/n" "'Type' property of Control # 6 is %d/n" "'Index' property of Control # 6 is %d/n" "'Id' property of Control # 6 is %d/n", (LPCTSTR)ccCaption, iiType, iiIndex, iiId); if("" == ccTag) strcat(buf, "Control #6 has noTag property"); else sprintf(buf + strlen(buf), "Tag of Control #6 is %s", (LPCTSTR)ccTag); AfxMessageBox(buf, MB_SETFOREGROUND); // Get a pointer for CommandBarPopup object with the ID of 30005. vResult.pdispVal = oBar.FindControl( COleVariant((short)10), // msoControlPopup type COleVariant((long)30005), covOptional, covOptional, covOptional); sprintf(buf, "IDispatch pointer of the msoControlPopup is %08lx", (long)vResult.pdispVal); AfxMessageBox(buf,MB_SETFOREGROUND); // Construct a Popup Control object and // Attach the IDispatch pointer of CommandBarPopup // to that new control object. CommandBarControl cbPop(vResult.pdispVal); ccCaption = cbPop.GetCaption(); iiType = cbPop.GetType(); iiIndex = cbPop.GetIndex(); iiId = cbPop.GetId(); sprintf(buf, "Caption of ControlPopup is %s/n" "'Type' property of ControlPopup is %d/n" "'Index' property of ControlPopup is %d/n" "'Id' property of ControlPopup is %d/n", (LPCTSTR)ccCaption, iiType, iiIndex, iiId); AfxMessageBox(buf,MB_SETFOREGROUND); sprintf(buf, "Watch the %s menu pad disappear.", (LPCTSTR)ccCaption); AfxMessageBox(buf, MB_SETFOREGROUND); cbPop.Delete(covTrue); oShapes = oSheet.GetShapes(); // Shapes collection, // is empty at first. AfxMessageBox("Adding a SmileyFace to be used in testing"); // It's a msoShapesSmileyFace AutoShape oShape = oShapes.AddShape( 17, 10.0, 10.0, 40.0, 40.0); CommandBar iBar; // Use the count of commandbars in the CommandBars object. for (int i = 1; i <= cCount; i++) { iBar = cbs.GetItem(COleVariant((short)i)); // To see the names of all the command bars, // uncomment the next 2 lines: // sprintf(buf, "Name of CommandBar is %s", iBar.GetName()); // AfxMessageBox(buf, MB_SETFOREGROUND); if( "Shapes" == iBar.GetName()) { iBar.SetEnabled(TRUE); long lType = iBar.GetType(); sprintf(buf, "For CommandBars(%d), the 'Shapes' shortcut, " "Type property is %d", i, lType); AfxMessageBox(buf, MB_SETFOREGROUND); break; } } // End of For loop. // Construct and attach IDispatch pointer to // CommandBarControls object. CommandBarControls oCBCs = iBar.GetControls(); CommandBarControl oCBC = oCBCs.Add( COleVariant((short)1), // msoControlButton const // VARIANT& Type, covOptional, //const VARIANT& Id, covOptional, //const VARIANT& // Parameter, to pass with OnAction covOptional, //const VARIANT& // Before, // Location on popup before item # covTrue //const VARIANT& Temporary, // (delete when app quits). ); oCBC.SetCaption("Run Macro #1"); oCBC.SetVisible(TRUE); oCBC.SetEnabled(TRUE); // oCBC.SetOnAction("Macro1"); // You'd uncomment the line above and substitute // the correct name of the desired Excel macro. sprintf(buf, "You've just added a CommandBarButtonControl " "to the shortcut menu for SmileyFace./n" "Right-click on a handle of the SmileyFace to see the new " "line at the bottom of the context menu./n/n" " - it says 'Run Macro #1'./n/n" "Save the worksheet and close Excel when " "you're through examining the change./n/n" "Then, reload Excel and open the worksheet " "from the MRU list. /n" "The new CommandBarButtonControl was " "temporary, so it's gone."); AfxMessageBox(buf, MB_SETFOREGROUND); return;
相关文章推荐
- 如何使用 MFC 嵌入并自动化 Microsoft Excel 工作表
- 如何修改Android应用程序能够使用的默认最大内存值
- ubuntu14.04,如何使用鼠标右键菜单在shell中打开选择项目?
- 如何修改使用phonegap创建的android程序的名称
- 如何使用dnSpy反编译unity代码并修改
- 如何批量部署Office 2013 (六)——使用MDT部署Office
- 使用Unity+HTC VIVE做VR时如何创建用户体验较强的菜单
- MySQl使用-------如何修改root密码&&解决本地无法登录问题
- 如何修改Android应用程序能够使用的默认最大内存值
- 如何修改DotNetNuke Admin级别的菜单
- PL/SQL Developer 使用错误的tnsnames.ora,如何修改?
- django:DateTimeField如何自动设置为当前时间并且能被修改 ——django日期时间字段的使用
- 如何修改本地代码,并更新到github,及其他使用技巧
- dede织梦后台如何修改?如何增加删除菜单?(
- 如何使用LINQ to SQL插入、修改、删除数据
- 如何使用PowerShell批量删除Office 365的用户
- 如何修改鼠标右键的弹出菜单项
- C# WinForm开发系列之如何使用panel控件制作左侧导航菜单
- ABAP–如何在’REUSE_ALV_GRID_DISPLAY’使用自定义F4帮助,返回多个字段以及计算修改其他字段
- 如何编辑修改Windows NT/2000/XP启动菜单“BOOT.INI”文件