C++ builder操作复合结构的TABLE

最近工作上需要用C++ builder操作复合结构的TABLE。由于之前的代码居然是用“MoveDown”和“TypeText”来完成的。这种方式在内容多的时候,定位就不准了。所以自然想到了定位“Cell”。普通表格还好说,但对于复合表格就不好办了。试了好些次,没找到行列的规律,都在不停的抛出异常。无奈只好上网发帖求助。得到了C++ builder版块大神-----妖哥的帮忙,在这里要感谢一下他。怕自己忘记了,写下这篇文章,看图就可以知道行列规则了。

代码:

 

  1. //---------------------------------------------------------------------------  
  2.   
  3. #include <vcl.h>  
  4. #pragma hdrstop  
  5.   
  6. #include "uMainWnd.h"  
  7. //---------------------------------------------------------------------------  
  8. #pragma package(smart_init)  
  9. #pragma resource "*.dfm"  
  10. TForm1 *Form1;  
  11. //---------------------------------------------------------------------------  
  12. __fastcall TForm1::TForm1(TComponent* Owner)  
  13.     : TForm(Owner)  
  14. {  
  15. }  
  16.   
  17. // ---------------------------------------------------------------------------  
  18. Variant vWordApp, vTable, vCell;  
  19.   
  20. void __fastcall TForm1::Button1Click(TObject *Sender)  
  21. {  
  22.     try  
  23.     {  
  24.         vWordApp = Variant::CreateObject("Word.Application");  
  25.     }  
  26.     catch(...)  
  27.     {  
  28.         MessageBox(Handle, "启动Word出错!",  
  29.                 Application->Title.c_str(), MB_OK | MB_ICONERROR);  
  30.   
  31.         return;  
  32.     }  
  33.   
  34.     vWordApp.OlePropertySet("Visible"true);  
  35.   
  36.     String strDocFile = ExtractFilePath(ParamStr(0)) + "2.doc";  
  37.     vWordApp.OlePropertyGet("Documents").  
  38.             OleFunction("Open", WideString(strDocFile));  
  39.   
  40.     vTable = vWordApp.OlePropertyGet("ActiveDocument").  
  41.             OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);  
  42. }  
  43.   
  44. // ---------------------------------------------------------------------------  
  45. void __fastcall TForm1::btnWriteClick(TObject *Sender)  
  46. {  
  47.     int nRow = edtRow->Text.ToIntDef(1);  
  48.     int nCol = edtCol->Text.ToIntDef(1);  
  49.   
  50.     vCell = vTable.OleFunction("Cell", nRow, nCol);  
  51.     String strText = String().sprintf(TEXT("第%d行第%d列"), nRow, nCol);  
  52.   
  53.     try  
  54.     {  
  55.         vCell.OlePropertySet("Range", WideString(strText));  
  56.     }  
  57.     catch (...)  
  58.     {}  
  59. }  
  60. //-----------------------

转载于:https://www.cnblogs.com/iq50/p/3148670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值