环境;win7 64,vs2008 sp1,word2013
一.新建一单文档,复合文档选择容器,同时选中活动文档容器和支持复合文件
二.参考http://blog.csdn.net/dragoo1/article/details/41704245,使单文档打开的时候可以打开一个现有word。关键代码
- void CTest1203View::OnInitialUpdate()
- {
- CView::OnInitialUpdate();
- // TODO: 写入最终选择模式代码之后移除此代码
- m_pSelection = NULL; // 初始化选定内容
- EmbedAutomateWord("D:\\test.docx", 0);
- }
- void CTest1203View::EmbedAutomateWord(CString sDocPath, int isNewDocDocment)
- {
- //Change the cursor so that theuser knows that something exciting is going
- //on.
- BeginWaitCursor();
- CTest1203CntrItem*pItem = NULL;
- TRY{
- //Get thedocument that is associated with this view, and be sure that itis
- //valid.
- CTest1203Doc* pDoc =GetDocument();
- ASSERT_VALID(pDoc);
- //Create a newitem associated with this document, and be sure that it is
- //valid.
- pItem = new CTest1203CntrItem(pDoc);
- ASSERT_VALID(pItem);
- // Get the ClassID for the Word document.
- // This is usedin creation.
- CLSID clsid;
- if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))
- //Anyexception will do. You just need to break out of the
- //TRYstatement.
- AfxThrowMemoryException();
- if(isNewDocDocment==0) //如果是打开文档
- {
- if(!pItem->CreateFromFile(sDocPath, clsid))
- AfxThrowMemoryException();
- }
- else
- {
- // Create theWord embedded item.
- if(!pItem->CreateNewItem(clsid))
- //Anyexception will do. You just need to break out of the
- //TRYstatement.
- AfxThrowMemoryException();
- }
- //Make sure thatthe new CContainerItem is valid.
- ASSERT_VALID(pItem);
- // Start theserver to edit the item.
- pItem->DoVerb(OLEIVERB_SHOW,this);
- // As anarbitrary user interface design, this sets the
- // selection tothe last item inserted.
- m_pSelection =pItem; // Setselection to the last inserted item.
- pDoc->UpdateAllViews(NULL);
- //Query for thedispatch pointer for the embedded object. In
- //this case, thisis the Word document.
- //LPDISPATCH lpDisp;
- //lpDisp = pItem->GetIDispatch();
- //Add text to theembedded Word document.
- // CDocumentwdDoc;
- // CRangewdRange;
- //set CDocument0wdDoc to use lpDisp, the IDispatch* of the
- //actualdocument.
- // wdDoc.AttachDispatch(lpDisp);
- //Get a CRangeobject for the document.
- //wdRange =wdDoc.Range(COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR),
- // COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ) );
- //Fill the rangewith the string "Hello, World!"
- //wdRange.put_Text("Hello, World!" );
- //wdRang.
- }
- //Clean up if something went wrong.
- CATCH(CException, e){
- if(pItem !=NULL){
- ASSERT_VALID(pItem);
- pItem->Delete();
- }
- AfxMessageBox(IDP_FAILED_TO_CREATE);
- }
- END_CATCH
- //Set the cursor back to normal so theuser knows exciting stuff
- //is no longer happening.
- EndWaitCursor();
- }
三.添加对MSWORD.OLB的引用
1.在stdafx.h添加如下代码
- //copy from C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE15\MSO.DLL(win7_x64 computer)
- #import "lib\win7_x86\MSO.DLL" rename("RGB", "_RGB1") rename("DocumentProperties", "_DocumentProperties1")
- //copy from C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB(win7_x64 computer)
- #import "lib\win7_x86\VBE6EXT.OLB"
- //copy from word2013
- #import "lib\word\word2013\MSWORD.OLB" rename("ExitWindows", "_ExitWindows1"),rename("FindText", "_FindText1")
- public:
- Word::_ApplicationPtr g_lpApp;
- Word::_DocumentPtr m_lpWord;
四.添加一个菜单,并添加相应事件
1.CTest1203View.h:
afx_msg void OnTest();
2.CTest1203View.cpp:
......
ON_COMMAND(ID_TEST, &CTest1203View::OnTest)
......
3.CTest1203View.cpp:
- void CTest1203View::OnTest()
- {
- // TODO: 在此添加命令处理程序代码
- m_lpWord = m_pSelection->GetIDispatch();
- g_lpApp = m_lpWord->GetApplication();
- Word::SelectionPtr sel = g_lpApp->GetSelection();
- sel->PutText("这是测试");
- CString strPathName("D:\\test1.docx");
- int filetype = 0;
- CString sFileExt = "docx";
- if(sFileExt=="docx")
- filetype = 12;
- CComVariant FileName(strPathName);
- CComVariant FileFormat(filetype);
- CComVariant LockComments(false),Password(_T(""));
- CComVariant AddToRecentFiles(true),WritePassword(_T(""));
- CComVariant ReadOnlyRecommended(false),EmbedTrueTypeFonts(false);
- CComVariant SaveNativePictureFormat(false),SaveFormsData(false);
- CComVariant SaveAsAOCELetter(false);
- CComVariant Encoding(true);
- CComVariant InsertLineBreaks(false);
- CComVariant AllowSubstitutions(true);
- CComVariant LineEnding(false);
- CComVariant AddBiDiMarks(false);
- CComVariant CompatibilityMode(14);
- if(sFileExt=="docx")
- {
- CComVariant Encoding(false);
- m_lpWord->SaveAs2(&FileName,&FileFormat,&LockComments,&Password,
- &AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
- &EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,
- &SaveAsAOCELetter, &Encoding, &InsertLineBreaks, &AllowSubstitutions, &LineEnding, &AddBiDiMarks, &CompatibilityMode);
- }
- else
- {
- m_lpWord->SaveAs(&FileName,&FileFormat,&LockComments,&Password,
- &AddToRecentFiles,&WritePassword,&ReadOnlyRecommended,
- &EmbedTrueTypeFonts,&SaveNativePictureFormat,&SaveFormsData,
- &SaveAsAOCELetter);
- }
- }
4.CTest1203View.cpp:
- CTest1203View::~CTest1203View()
- {
- //释放容器
- if(m_pSelection!=NULL)
- {
- //m_pSelection->Release();
- m_pSelection->Delete();//该方法包含Release(),并有其他处理
- m_pSelection = NULL;
- }
- //释放word
- if (m_lpWord!=NULL)
- {
- try
- {
- m_lpWord->Release();
- }
- catch (...)
- {
- }
- m_lpWord = NULL;
- }
- }
本文主要是对MSWORD.OLB引入,这样可以很方便的使用msword的Word::_DocumentPtr类,更容易操作word,比如插入文字,表格修改,文档保存等,其实这就是vba在vc(或c++)的使用,当然下一篇将介绍在网页中利用activex控件打开word,以及利用IDispatch* 同时支持word/wps