在VC中呼叫WORD(顯示,修改,存檔,執行宏) (轉)

worldblog發表於2007-12-10
在VC中呼叫WORD(顯示,修改,存檔,執行宏) (轉)[@more@]

(1)使用AppWizard建立一個新的MFC AppWizard(EXE)工程,命名為"" 
(2)選擇單文件檢視(SDI)結構,在第3步中需要選中Container,以提供容器支援,並且選中active document container 其它都為預設

(3)在View選單中,選ClassWizard,選Automation選項卡,選Add Class,選擇From a TypeLibrary, 在Office目錄中選中 97/2000 型別庫Word8.olb或Word9.olb,選中application,document,_document。單擊ok

(4)給COfficeCntrItem新增一方法。GetIDispatch()

其原始碼如下:

 ASSERT_VALID(this);  
 ASSERT(m_lp != 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->GetBound(&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)) 
 /*(!pItem->CreateFromFile (filename,clsid)) */
 AfxThrowMemoryException();
 ASSERT_VALID(pItem);
 
 pItem->Activate (OLEIVERB_SHOW,this);
 
 

 ASSERT_VALID(pItem);
   m_pion = 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 ();
 /*  ("Macro3");*/
  mydoc.Activate ();
 BOOL password=mydoc.GetHassword ();
 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章