最近工作上需要用C++ builder操作复合结构的TABLE。由于之前的代码居然是用“MoveDown”和“TypeText”来完成的。这种方式在内容多的时候,定位就不准了。所以自然想到了定位“Cell”。普通表格还好说,但对于复合表格就不好办了。试了好些次,没找到行列的规律,都在不停的抛出异常。无奈只好上网发帖求助。得到了C++ builder版块大神-----妖哥的帮忙,在这里要感谢一下他。怕自己忘记了,写下这篇文章,看图就可以知道行列规则了。
代码:
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "uMainWnd.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- // ---------------------------------------------------------------------------
- Variant vWordApp, vTable, vCell;
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- try
- {
- vWordApp = Variant::CreateObject("Word.Application");
- }
- catch(...)
- {
- MessageBox(Handle, "启动Word出错!",
- Application->Title.c_str(), MB_OK | MB_ICONERROR);
- return;
- }
- vWordApp.OlePropertySet("Visible", true);
- String strDocFile = ExtractFilePath(ParamStr(0)) + "2.doc";
- vWordApp.OlePropertyGet("Documents").
- OleFunction("Open", WideString(strDocFile));
- vTable = vWordApp.OlePropertyGet("ActiveDocument").
- OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);
- }
- // ---------------------------------------------------------------------------
- void __fastcall TForm1::btnWriteClick(TObject *Sender)
- {
- int nRow = edtRow->Text.ToIntDef(1);
- int nCol = edtCol->Text.ToIntDef(1);
- vCell = vTable.OleFunction("Cell", nRow, nCol);
- String strText = String().sprintf(TEXT("第%d行第%d列"), nRow, nCol);
- try
- {
- vCell.OlePropertySet("Range", WideString(strText));
- }
- catch (...)
- {}
- }
- //-----------------------