[十万火急]200分求Excel嵌入单文档视图后菜单隐藏的解决方案
大山里的松 2005-10-09 01:31:31 下面两个帖子分别有100分 回复哪一个都可 谢谢了 十万火急阿
http://community.csdn.net/Expert/topic/4308/4308100.xml?temp=.9545099
http://community.csdn.net/Expert/topic/4314/4314226.xml?temp=.5092584
我想把Excel嵌入一个单文档的视图里面,然后通过自动化的方法控制excel做一些工作,如打印等等
但是我成功的嵌入后,发现我的单文档程序的菜单被Excel菜单取代了,后来我在msdn上面找了一篇文章
HOWTO: Use Automation to Modify the Office Menu 试图隐藏菜单,但是运行后虽然菜单恢复了,但是总是
马上崩溃掉
还望各位高手多多指教阿
我的步骤是:
新建一个单文档程序
在第三步种选择
Container和Active document container
完成
ClassWzd =>Automation => Add class from a type lib
选择 C:\Program Files\Microsoft Office\Office10\EXCEL.EXE (我的是office xp ,2000的好像选择excel9.olb)
把所有的类都添加进来
同样的把
C:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll 中的所有类也添加进来
在Embed_ExcelView.cpp加入
#include "Excel.h"
#include "mso.h"
在 excel.h 顶端加入 "namespace XL{"
尾部加入 "}" 避免有些类重复定义
在 Excel.cpp 中 头部的#endif后面加上
using namespace XL;
在CEmbed_ExcelView::OnInsertObject 改为如下代码:
void CEmbed_ExcelView::OnInsertObject()
{
BeginWaitCursor();
CEmbed_ExcelCntrItem* pItem = NULL;
TRY
{
// Create new item connected to this document.
CEmbed_ExcelDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pItem = new CEmbed_ExcelCntrItem(pDoc);
ASSERT_VALID(pItem);
// Initialize the item from the dialog data.
/* if (!dlg.CreateItem(pItem))
AfxThrowMemoryException(); // any exception will do
ASSERT_VALID(pItem);*/
CLSID clsid; //
//if(FAILED(::CLSIDFromProgID(L"word.document",&clsid)))
if(FAILED(::CLSIDFromProgID(L"excel.sheet",&clsid)))
AfxThrowMemoryException();
if(!pItem->CreateNewItem(clsid))
//if(!pItem->CreateFromFile ("c:\\testlist.xlt",clsid))
AfxThrowMemoryException();
ASSERT_VALID(pItem);
pItem->Activate (OLEIVERB_SHOW,this);
ASSERT_VALID(pItem);
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
// As an arbitrary user interface design, this sets the selection
// to the last item inserted.
// TODO: reimplement selection as appropriate for your application
m_pSelection = pItem; // set selection to last inserted item
pDoc->UpdateAllViews(NULL);
}
CATCH(CException, e)
{
if (pItem != NULL)
{
ASSERT_VALID(pItem);
pItem->Delete();
}
AfxMessageBox(IDP_FAILED_TO_CREATE);
}
END_CATCH
hidetoolbar(FALSE);//自己写的隐藏工具栏和菜单的代码
EndWaitCursor();
}
添加函数 CEmbed_ExcelView::hidetoolbar(BOOL hide)
用来隐藏Toolbar 和菜单
int CEmbed_ExcelView::hidetoolbar(BOOL hide)
{
TRY{
LPDISPATCH lpDisp;
lpDisp = m_pSelection->GetIDispatch(true);
//Documents docs;
XL::_Application app;
VARIANT vResult;
DISPID dispID = 0;
HRESULT hr;
char buf[2048]; // General purpose message buffers
OLECHAR *strCBs = L"CommandBars";
//xxxx _Workbook mydoc;
//xxxx
//xxxx mydoc.AttachDispatch (lpDisp,TRUE);
//xxxx mydoc.Activate ();
//xxxx app=mydoc.GetApplication ();
app.AttachDispatch(lpDisp,true);
// Find &strCBs, i.e. L"CommandBars" and put it in dispID.
//app.m_lpDispatch=lpDisp;
//m_pSelection->Activate(OLEIVERB_SHOW,this);
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);
_CommandBars mybars;
CommandBar mybar;
vResult.pdispVal= mybars.GetActiveMenuBar();
CommandBar oBar(vResult.pdispVal);
oBar.SetEnabled(false);
// H 用来隐藏工具栏的 现在注释了
//H mybars.AttachDispatch (mydoc.GetCommandBars (),TRUE);
//H
//H mybar.AttachDispatch (mybars.GetItem (COleVariant(/*(short)1)*/"Standard")),TRUE);
//H mybar.SetVisible (hide);
//H mybar.AttachDispatch (mybars.GetItem (COleVariant(/*(short)2*/"formatting")),TRUE);
//H mybar.SetVisible (hide);
CFrameWnd * pwnd=(CFrameWnd *)AfxGetMainWnd();
pwnd->GetActiveFrame()->UpdateWindow ();
}
CATCH(CException, e)
{
TCHAR errormsg[255];
e->GetErrorMessage (errormsg,255,NULL);
}
END_CATCH
return 0;
}