大致的都寫好了,就差讓表格居中了,注意是讓表格居中,不是讓文字居中!
部分代碼貼在下面,有會的朋友請不吝賜教!謝謝!可有償!
可聯系 手機 微信 13522968273
void CWordtestDlg::OnNewDoc()
{
COleVariant vTrue((short)TRUE);
COleVariant vFalse((short)FALSE);
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//////////////////////////////////////////////////////////////////////////
//開始一個新的Microsoft Word實體
_Application oWordApp;
if (!oWordApp.CreateDispatch("Word.Application", NULL))
{
AfxMessageBox("CreateDispatch failed.", MB_OK | MB_SETFOREGROUND);
return;
}
oWordApp.SetVisible(TRUE);
//////////////////////////////////////////////////////////////////////////
//創建一個新的word檔案
Documents oDocs = oWordApp.GetDocuments();
_Document oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);
//把文本添加到word檔案
Selection oSel = oWordApp.GetSelection();
_Font f=oSel.GetFont();
Paragraph format = oSel.GetParagraphFormat();//設定格式
format.SetAlignment(1);//居中
format.SetLineSpacingRule(4);
format.SetLineSpacing(13);
f.SetSize(12);
f.SetBold(1);
oSel.TypeParagraph();
oSel.TypeText("西歐8國15天行程");
oSel.TypeParagraph();
f.SetSize(9);
f.SetBold(0);
oSel.TypeParagraph();
oSel.WholeStory();
// 設定字體——老的
/* _Font font(oSel.GetFont());
//font.SetColor(RGB(255,0,0));
font.SetColor(RGB(0,0,0));
font.SetSize(14.f);
font.SetBold(1);
oSel.SetFont(font);*/
///表格1
oSel.EndKey(COleVariant((short)wdStory), COleVariant((short)wdMove));
Range Rng = oSel.GetRange();
Tables Tbls = oDoc.GetTables();
Table Tb0 = Tbls.Add(Rng, 16, 5, vOpt, vOpt);
Borders bords0 = Tb0.GetBorders();
bords0.SetOutsideLineStyle(wdLineStyleSingle);//設定邊框屬性
bords0.SetOutsideColorIndex(wdBlack);//邊框顏色
bords0.SetOutsideLineWidth(wdLineWidth150pt);//邊框寬度
bords0.SetInsideLineStyle(wdLineStyleSingle);//內框屬性
bords0.SetInsideColorIndex(wdBlack);//內框顏色
f.SetBold(1);
Columns c=oSel.GetColumns();
c.SetWidth(40);
oSel.TypeText(_T("第X天"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
f.SetBold(1);
c.SetWidth(50);
oSel.TypeText(_T("日期"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
f.SetBold(1);
c.SetWidth(260);
oSel.TypeText(_T("行程安排"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
f.SetBold(1);
c.SetWidth(40);
oSel.TypeText(_T("住宿"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
f.SetBold(1);
//c.SetWidth(30);
c.SetWidth(100);
oSel.TypeText(_T("用餐"));
oSel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
uj5u.com熱心網友回復:
手動操作,錄制宏,按宏代碼修改uj5u.com熱心網友回復:
在Word中開始記錄宏,手動完成所需功能,結束記錄宏,按Alt+F11鍵,查看剛才記錄的宏對應的VBA代碼。uj5u.com熱心網友回復:
2樓大神,得到了VBA代碼Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
還是不曉得怎么把這個加到程式里去
uj5u.com熱心網友回復:
要居中的表格.Rows.Alignment = 1;//wdAlignRowCenter
uj5u.com熱心網友回復:
輸入
Tb0.Rows.Alignment = 1;//wdAlignRowCenter
VC還是報錯:
error C2039: 'Rows' : is not a member of 'Table'
怎么解決?謝謝!
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熱心網友回復:
懷疑你少生成了相應包裝類。
僅供參考://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熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/89640.html
標籤:基礎類
上一篇:RSA非對稱加密,OpenSSL庫的使用問題,是每次都生成一個新的公、私鑰對呢?還是生成一次,以后一直用它?
下一篇:這題有沒有什么效率高點的方法?
