从VBA到Delphi

 

我一般用OLE方式处理office系列的数据。

一、定义

WordApp: variant;
CurrDoc: variant;
Selection: variant;

二、初始化

WordApp.Visible := true;
CurrDoc := WordApp.documents.Open(AFullFileName, Revert := true, Visible := true);
Selection := CurrDoc.ActiveWindow.Selection;//好处在于不用怕打开关闭其它word文档而出错

三、释放处理

if not VarIsEmpty(CurrDoc) then
begin
try
CurrDoc.save;
CurrDoc.close;
except

end;

end;
CurrDoc := Unassigned;
if not VarIsEmpty(WordApp) then
begin
try
WordApp.quit;
except

end;

end;
WordApp := Unassigned;
Selection := Unassigned;

以上是基本处理。

五、VBA到delphi的处理方法

首先请先下载我的另一博文的Office Vba手册,方便参考。

下面仅举几个例子如何从vba代码中转为delphi代码。

这一部分为VBA代码

//Debug.Print Selection.Information(wdEndOfRangeColumnNumber) '17结束列
//Debug.Print Selection.Information(wdEndOfRangeRowNumber) '14结束行
//Debug.Print Selection.Information(wdStartOfRangeColumnNumber) '16开始列
//Debug.Print Selection.Information(wdStartOfRangeRowNumber) '13开始行
Delphi代码:

bCol := Selection.Information[16];//其中Selection是有定义的,看前面才可以有和VBa类似
eCol := Selection.Information[17];//information这是selection的属性。VBA中是用()但是在delphi是用[]
bRow := Selection.Information[13];//调用属性有参数时delphi是[],VBA是()函数或事件都是()
eRow := Selection.Information[14];

VBA转成delphi很多部分可以抄,但是有细微的差别。这个要注意。

六、我的应用中要读取当前光标的是在表格那个位置,但是word的表格数据读取,很麻烦。

我用了这个来处理

Selection.paragraphs.item(1).range.text := '';

转载于:https://www.cnblogs.com/CatDo/p/4502855.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值