first Automation

    //创建一个容器
    CEmbWordCntrItem * pItem = NULL;
    CEmbWordDoc * pDoc = GetDocument();
    pItem = new CEmbWordCntrItem(pDoc);
    CLSID clsid = {0};
    //创建Automation服务器
    CLSIDFromProgID(L"Word.Document", &clsid);
    pItem->CreateNewItem(clsid);
    //显示
    pItem->DoVerb(OLEIVERB_SHOW, this);
    CRect rcClient;
    GetClientRect(&rcClient);
    pItem->SetItemRects(&rcClient);

  m_pSelection = pItem;

 

 

菜单 View 添加Command OnText { //获取Word.Document的接口 _Document doc  m_pSelection->GetDispatch(); _Application app = doc.GetApplication(); Selection sel = app.GetSelection();  //输入文字  sel.TypeText("Hello World");

IDispatch * CEmbWordCntrItem::GetDispatch() { IDispatch * piDispatch = NULL; m_lpObject->QueryInterface(IID_IDispatch, (LPVOID*)&piDispatch);

} 导入MSWORD.OLB 类型库

//客户端模式 //创建服务器 //显示 _Application app; app.CreateDispatch("Word.Application"); app.SetVisible("TRUE"); Documents docs = app.GetDocuments(); COleVariant varTemplate(_T""); COleVariant newTemplate(0L); COleVariant varDocumentType(0L); COleVariant varVisiable;

_Document doc = docs.Add(&varTemplate,

&newTemplate, &varDocumentType, &varVisiable); Section sel = app.GetSelection(); sel.TypeText("Hello"); doc.Save();

/////// Atuomation服务器 EXE

AfxGetMainWnd()->ShowWindow(); GetActiveView()->SetWindowText DLL

IAutoExe autoexe; autoexe.CreateDispatch("AutoExe.Document"); autoexe.SetVisible(TRUE); autoexe.DetachDispatch();

添加dll 添加类 CCmdTarget父类 注册 使用

posted @ 2013-08-06 08:49  呵呵··  阅读(212)  评论(0编辑  收藏  举报