从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 := '';

posted @ 2015-05-14 11:20  颜明发  阅读(744)  评论(0编辑  收藏  举报