在網上找到一個c#程式,可以呼叫word里面的畫圖工具,想用vc++實作一次,不會用VC++去初始化commandbars物件,求大神們幫忙~~~~~~~~~~~~~~
uj5u.com熱心網友回復:
vc中import匯入對應的office com組件,然后會自動生成包裝類,你就可以呼叫了uj5u.com熱心網友回復:
匯入以后,沒有commandbars物件啊,只有在有的類里面有get_commandbars方法,回傳的只是LPDISPATCHuj5u.com熱心網友回復:
自己頂一下~大神們,我用的office10uj5u.com熱心網友回復:
_CommandBarsPtr pCmdbars = pApp->CommandBars;??
uj5u.com熱心網友回復:
_Application類里通過 LPDISPATCH GetCommandBars();獲取,內容固定的圖片可以通過插入圖片方式,散點圖等可以用machart去畫,真正用word里畫圖不怎么常見uj5u.com熱心網友回復:
_application,里面的getcommandbars方法,不能獲得commandbars物件,我想用commandbars里面的方法~~
uj5u.com熱心網友回復:
提示_commandbarsptr未宣告識別符號uj5u.com熱心網友回復:
現在能編譯通過,但呼叫里面的方法會報錯uj5u.com熱心網友回復:
明白了,實質上你提問的不對,你應該問哪兒得到commandbars這個包裝類及介面,從microsoft office xx.0 object library這個里得到。
uj5u.com熱心網友回復:
CCommandBars bar(word.m_pApplication.GetCommandBars());
int nCount = bar.get_Count();
CString str;
str.Format("%d",nCount);
MessageBox(str);
結果176個,至于單獨某個怎么使用,可以看介面
uj5u.com熱心網友回復:
CCommandBars bar(word.m_pApplication.GetCommandBars());
int nCount = bar.get_Count();
CString str;
str.Format("%d",nCount);
MessageBox(str);
結果176個,至于單獨某個怎么使用,可以看介面
uj5u.com熱心網友回復:
頂一下,看見的幫我分析下uj5u.com熱心網友回復:
自己頂下,raw_executemso的回傳值一直都是E_OUTOFMEMORYuj5u.com熱心網友回復:
CCommandBars bar(word.m_pApplication.GetCommandBars());int nCount = bar.get_Count();
CString str;
str.Format("%d",nCount);
MessageBox(str);
哪里有CCommandBars ?
uj5u.com熱心網友回復:
僅供參考://1.在VC中新建一控制臺程式,選支持MFC(當然,你也可以不選擇支持MFC的,不過會很麻煩)
//2.按CTRL+W調出MFC ClassWizard,Add Class->From a type library,選擇你的word的型別庫
// (例如我的是word2003,安裝在e盤,我的路徑是"e:\edittools\microsoft office\office11\msword.olb"),
// 選擇完畢后,在彈出的視窗中選擇要讓classwizard生成的包裝類,在本例中要用到
// _Application,
// Documents,
// _Document,
// Range
// 這四個類,選中他們后按OK
//3.進入你的main函式所在的cpp檔案,加入頭檔案參考
// #include "msword.h" //參考剛才classwizard生成的idispatch包裝類
//4.加入代碼
// console_word.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
printf(_T("Fatal Error: MFC initialization failed!\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持庫失敗!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString txt;
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("c:\\new\\測驗.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
txt=aRange.GetText();
AfxMessageBox(txt);//這里GetText得到的就是word檔案的純文本了,你可以將其寫到txt檔案中
printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的換行不是\r\n而是\r,所以需要輸出重定向到文本檔案看結果。
aRange.ReleaseDispatch();
doc.Close(vOpt,vOpt,vOpt);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
wordApp.Quit(vOpt,vOpt,vOpt);
wordApp.ReleaseDispatch();
CoUninitialize();
}
return nRetCode;
}
uj5u.com熱心網友回復:
在Word2003中開始記錄宏,手動完成所需功能,結束記錄宏,按Alt+F11鍵,查看剛才記錄的宏對應的VBA代碼。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/95357.html
上一篇:用管道讀取資料很慢是怎么回事?
