15,980
社区成员




《Visual C++开发实战1200例》这本书不错,你可以看下http://www.verycd.com/topics/2914781/
《Visual C++开发实战1200例》这本书不错,你可以看下http://www.verycd.com/topics/2914781/
void COffCtlView::EmbedAppWord(CString aFileName)
{
if(!m_wdApp.CreateDispatch(_T("Word.Application")))
{
AfxMessageBox(_T("Cannot start Excel and get Application object."));
return;
}
else
{
m_wdApp.put_ScreenUpdating(TRUE);
m_wdApp.put_Visible(TRUE);
m_childWnd = ::FindWindow(_T("Opusapp"), m_wdApp.get_Caption());
::SetParent(m_childWnd,this->m_hWnd);
RECT rect;
GetWindowRect(&rect);
int Width= rect.right - rect.left;
int Height= rect.bottom - rect.top;
m_wdDocs = m_wdApp.get_Documents();
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
vtFalse((short)FALSE);
m_wdDoc = m_wdDocs.Open(COleVariant(aFileName),vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,
vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional);
LONG style = ::GetWindowLong(m_childWnd,GWL_STYLE);
style&=~WS_CAPTION &~WS_MINIMIZEBOX &~WS_MAXIMIZEBOX &~WS_THICKFRAME &~WS_SYSMENU |FWS_ADDTOTITLE;
::SetWindowLong(m_childWnd,GWL_STYLE,style);
m_RibbonWnd = GetRibbonWnd(m_childWnd);
ShowRibbonBar(m_bHideToolbar);
m_wdApp.put_ScreenUpdating(TRUE);
/* g_lpWndDefProc = (WNDPROC)::SetWindowLong(m_RibbonWnd, GWL_WNDPROC, (LONG)SubWindowProc);*/
InvalidateAllChildWindows(this->m_hWnd);
}
}
2010-03-03
Tag:word 自动化 VC MFC
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://air-head.blogbus.com/logs/59826305.html
概述
本文只是对vc中使用word的初步介绍,为涉及编辑等问题
主要参看http://support.microsoft.com/?scid=kb;zh-cn;316207&spid=2989&sid=15
增加自己创建时遇到问题的解决
1.用向导创建MFC应用程序.
2.选则多文档,在复合文档类型中选择container(容器)
3.添加关于word的引用,如果使用的是Com的方式,那么不需要做3.4步骤.
4.注意
默认导入时的引用文件为
//#import "C:\\Program Files\\Microsoft Office\\Office12\\MSWORD.OLB" no_namespace
可能无法正确使用,所以需做如下调整
#import "C:\\Program Files\\Microsoft Office\\Office12\\MSWORD.OLB" auto_search auto_rename no_auto_exclude rename("ExitWindows", "WordExitWindows")
using namespace Word;
5.修改CntrItem.h文件
class CTestWord2CntrItem : public COleDocObjectItem,默认继承自COleClientItem
在CntrItem.cpp中做相应调整
解决word在view中的显示问题.
6.为CTestWord2CntrItem添加共有成员函数
public:
LPDISPATCH GetIDispatch();
实现如下:
LPDISPATCH CTestWord2CntrItem::GetIDispatch()
{
//The this and m_lpObject pointers must be valid for this function
//to work correctly. The m_lpObject is the IUnknown pointer to
// this object.
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
LPUNKNOWN lpUnk = m_lpObject;
//The embedded application must be running in order for the rest
//of the function to work.
Run();
//QI for the IOleLink interface of m_lpObject.
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink,
(LPVOID FAR*)&lpOleLink) == NOERROR)
{
ASSERT(lpOleLink != NULL);
lpUnk = NULL;
//Retrieve the IUnknown interface to the linked application.
if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
{
TRACE0("Warning: Link is not connected!\n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
}
//QI for the IDispatch interface of the linked application.
LPDISPATCH lpDispatch = NULL;
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)
!=NOERROR)
{
TRACE0("Warning: does not support IDispatch!\n");
return NULL;
}
//After you verify that it is valid, return the IDispatch
//interface to the caller.
ASSERT(lpDispatch != NULL);
return lpDispatch;
}
7.修改TestWord2View.cpp文件
void CTestWord2View::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: remove this code when final selection model code is written
m_pSelection = NULL; // initialize selection
EmbedAutomateWord();
}
8.为TestWord2View.cpp增加,共有函数
public:
void EmbedAutomateWord();
实现如下:
void CTestWord2View::EmbedAutomateWord()
{
//Change the cursor so that the user knows that something exciting is going
//on.
BeginWaitCursor();
CTestWord2CntrItem* pItem = NULL;
TRY
{
//Get the document that is associated with this view, and be sure that it is
//valid.
CTestWord2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//Create a new item associated with this document, and be sure that it is
//valid.
pItem = new CTestWord2CntrItem(pDoc);
ASSERT_VALID(pItem);
// Get the Class ID for the Word document.
// This is used in creation.
CLSID clsid;
if(FAILED(::CLSIDFromProgID(L"Word.document",&clsid)))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
// Create the Word embedded item.
if(!pItem->CreateNewItem(clsid))
//Any exception will do. You just need to break out of the
//TRY statement.
AfxThrowMemoryException();
//Make sure that the new CContainerItem is valid.
ASSERT_VALID(pItem);
// Start the server to edit the item.
pItem->DoVerb(OLEIVERB_SHOW, this);
// As an arbitrary user interface design, this sets the
// selection to the last item inserted.
m_pSelection = pItem; // Set selection to the last inserted item.
pDoc->UpdateAllViews(NULL);
//Query for the dispatch pointer for the embedded object. In
//this case, this is the Word document.
LPDISPATCH lpDisp;
lpDisp = pItem->GetIDispatch();
//Add text to the embedded Word document.
CDocument0 wdDoc;
CRange wdRange;
//set CDocument0 wdDoc to use lpDisp, the IDispatch* of the
//actual document.
wdDoc.AttachDispatch(lpDisp);
//Get a CRange object for the document.
//wdRange = wdDoc.Range(COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ),
// COleVariant( (long)DISP_E_PARAMNOTFOUND, VT_ERROR ) );
//Fill the range with 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 the user knows exciting stuff
//is no longer happening.
EndWaitCursor();
}
9.编译运行.