ITPub博客

首页 > 应用开发 > IT综合 > 在VC中调用WORD(显示,修改,存盘,运行宏) (转)

在VC中调用WORD(显示,修改,存盘,运行宏) (转)

原创 IT综合 作者:worldblog 时间:2007-12-10 10:32:35 0 删除 编辑
在VC中调用WORD(显示,修改,存盘,运行宏) (转)[@more@]

(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.

自己辛苦了很久加上别人的经验总算做出来。:)


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-990867/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6241
  • 访问量
    2410988