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

最近工作上需要用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 (...)
    {}
}
//---------------------------------------------------------------------------


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值