(1)使用AppWizard创建一个新的MFC AppWizard(EXE)工程,命名为"office" 
(2)选择单文档视图(SDI)结构,在第3步中需要选中Container,以提供容器支持,并且选中active document container 其它都为默认

(3)在View菜单中,选ClassWizard,选Automation选项卡,选Add Class,选择From a TypeLibrary, 在Office目录中选中Microsoft Word 97/2000 类型库Word8.olb或Word9.olb,选中application,document,_document。单击ok

(4)给COfficeCntrItem添加一方法。GetIDispatch()

其源码如下:

  ASSERT_VALID(this);  
 ASSERT(m_lpObject != NULL);   
 LPUNKNOWN lpUnk = m_lpObject;   
 Run();   
 LPOLELINK lpOleLink = NULL;  
 if (m_lpObject->QueryInterface(IID_IOleLink, (LPVOID FAR*)&lpOleLink) == NOERROR)   
 {   
  ASSERT(lpOleLink != NULL);   
  lpUnk = NULL;       
  if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)    
  {    
   TRACE0("Warning: Link is not connected!\n");    
   lpOleLink->Release();    
   return NULL;    
  }   
  ASSERT(lpUnk != NULL);   
 }    
 LPDISPATCH lpDispatch = NULL;  
 if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)!=NOERROR)   
 {
    TRACE0("Warning: does not support IDispatch!\n");   
  return NULL;
  
 }
 ASSERT(lpDispatch != NULL);  
 return lpDispatch;
(5)。在officeView.h添加#include "msword8.h"

(6)。修改void COfficeView::OnInsertObject(),源码如下:

 BeginWaitCursor();

 COfficeCntrItem* pItem = NULL;
 TRY
 {
  // Create new item connected to this document.
  COfficeDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  pItem = new COfficeCntrItem(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)))
   AfxThrowMemoryException();
  
  if(!pItem->CreateNewItem(clsid)) 
  /*if(!pItem->CreateFromFile (filename,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

 EndWaitCursor();

(7)重载ID—FILE—SAVE,

void COfficeView::OnFileSave()
{
 // TODO: Add your command handler code here
  TRY{
     LPDISPATCH lpDisp;
  
  lpDisp = m_pSelection->GetIDispatch();

    Documents docs;
  
  _Application app;
  
  _Document mydoc;
  Documents my;

  mydoc.AttachDispatch (lpDisp,TRUE);
  app=mydoc.GetApplication ();
    /*   app.Run ("Macro3");*/
     mydoc.Activate ();
  BOOL password=mydoc.GetHasPassword ();
  mydoc.SetPassword ("love");
  password=mydoc.GetHasPassword ();  
  COleVariant vFalse((short)FALSE);
  mydoc.SaveAs (COleVariant("c:\\love.doc"),vFalse,vFalse, COleVariant(""),vFalse,
   COleVariant(""),vFalse,vFalse,vFalse,vFalse,vFalse);
  
 }
 CATCH(CException, e)
 {
        }
 END_CATCH
}

bulid,click insert object,and edit ,and save.maybe run macro.

Posted on 2007-01-13 16:52 艾凡赫 阅读(1608) 评论(0)  编辑 收藏 引用 所属分类: MFC

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理