全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1774
推到 Plurk!
推到 Facebook!

BCB在Word中插入表格,先前資料遭清除

缺席
jyunyen
一般會員


發表:2
回覆:1
積分:0
註冊:2010-04-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-11-08 17:51:38 IP:60.250.xxx.xxx 訂閱
 目前使用BCB來寫入資料到Word中,目前可以寫入文字與插入表格!
但再插入表格後,會將先前資料全部清除,不知道有何方式可以改善此問題呢?
附上程式碼:


void __fastcall TForm1::Button13Click(TObject *Sender)
{
Variant word_app;
Variant word_docs;
Variant word_range;
Variant word_tables;
Variant word_table;
Variant active_doc;
Variant the_cell;
Variant cell_range;
int cell_width ;
int cell_long ;
AnsiString temStr,FilePath;
FilePath = ExtractFilePath(Application->ExeName);
word_app=Variant::CreateObject("Word.application");
word_docs=word_app.OlePropertyGet("documents");
temStr = FilePath "123456.doc";
word_docs.OleProcedure("Open",temStr.c_str());
//word_docs.OleProcedure("Add");
word_app.OlePropertySet("Visible",true);
active_doc=word_app.OlePropertyGet("ActiveDocument");
word_range=active_doc.OleFunction("Range");
word_range.OleFunction("InsertAfter", "123" );
word_range.OleFunction("InsertParagraphAfter");
// 移到文件結尾
word_app.OlePropertyGet("Selection").OleProcedure("EndKey", 6); //wdStory
// 換行指令
word_app.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
word_tables=word_range.OlePropertyGet("Tables");
cell_width=5;
cell_long=1;
word_table=word_tables.OleFunction("Add",word_range,cell_long 1,cell_width,1);
//畫表格
Procedure InsertAfter("InsertAfter");
for(int i=0;i
the_cell=word_table.OleFunction("cell",1,i 1);
cell_range=the_cell.OlePropertyGet("range");
InsertAfter.ClearArgs();
cell_range.Exec(InsertAfter<<"title" IntToStr(i));
}
for(int i=0;i
for(int j=0;j
the_cell=word_table.OleFunction("cell",j 2,i 1);
cell_range=the_cell.OlePropertyGet("range");
InsertAfter.ClearArgs();
cell_range.Exec(InsertAfter <<"Cell");
}
}
}

編輯記錄
jyunyen 重新編輯於 2010-11-08 02:52:36, 註解 無‧
jyunyen 重新編輯於 2010-11-08 02:53:10, 註解 無‧
jyunyen 重新編輯於 2010-11-08 02:56:22, 註解 無‧
jyunyen 重新編輯於 2010-11-08 02:56:55, 註解 無‧
jyunyen
一般會員


發表:2
回覆:1
積分:0
註冊:2010-04-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-11-09 16:01:48 IP:60.250.xxx.xxx 訂閱
找到方法了~跟大家分享一下

Variant wordApp;
Variant wordDocuments;
Variant word_range;
Variant wordTables;
Variant wordSelection;
Variant Range;
AnsiString temStr,FilePath;
if (wordApp.IsEmpty())
wordApp=Variant::CreateObject("Word.Application");
else
wordApp=GetActiveOleObject("Word.Application");
if (wordApp.IsEmpty()){
ShowMessage("Unable to find Word application.");
return;
}
wordApp.OlePropertySet("Visible",true);
wordDocuments = wordApp.OlePropertyGet("Documents");
FilePath = ExtractFilePath(Application->ExeName);
temStr = FilePath "xxxx.doc";
wordDocuments.OleProcedure("Open",temStr.c_str());
wordDocuments=wordApp.OlePropertyGet("ActiveDocument");
word_range=wordDocuments.OleFunction("Range");
temStr = "ABCDEFGHIJKLMNOP";
word_range.OleFunction("InsertAfter", temStr.c_str());
word_range.OleFunction("InsertParagraphAfter");
// 移到文件結尾
wordApp.OlePropertyGet("Selection").OleProcedure("EndKey", 6); //wdStory
// 換行指令
wordApp.OlePropertyGet("Selection").OleProcedure("TypeParagraph");
wordTables = wordDocuments.OlePropertyGet("Tables");
wordSelection = wordApp.OlePropertyGet("Selection");
Range = wordSelection.OlePropertyGet("Range");
// inserts the table
wordTables.OleProcedure("Add", Range, (Variant) 3,(Variant) 5,1);

系統時間:2024-04-24 16:55:51
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!