通过Delphi Excecute Excel vba代码 [英] Excecute Excel vba code through Delphi

查看:634
本文介绍了通过Delphi Excecute Excel vba代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何通过Delphi 7程序执行Excel vba代码?
我在互联网上发现了一些文章,没有描述清楚。
其中一个是使用OLE:
文章< a>



如果您能给我答案的示例代码,我将不胜感激。
主要的问题是我不能低估代码,我需要一些帮助。
该变量没有被声明,我不会没有它会工作。
提前感谢

 单位宏; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,窗体,
对话框,ActnList,StdCtrls, MSObjCtrls,ComObj,ActiveX;

type
TForm1 = class(TForm)
ActionList1:TActionList;
btnMacro:TMSBitBtn;
acMacro:TAction;
procedure acMacroExecute(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

var
Form1:TForm1;

执行

{$ R * .dfm}

程序TForm1.acMacroExecute(发件人:TObject);
var
Excel:OleVariant;
begin
Excel:= CreateOleObject('Excel.Application');
Excel.Workbooks.Open('C:\Documents and Settings\iordan.borisov\Desktop\data.xls');
Excel.Run('PERSONAL.XLSB!MyMacro'); //<< --- ---此处出错。
Excel.Quit;

end;
初始化
CoInitialize(nil);
finalization
CoUninitialize;

结束。

此代码引发运行时异常:



< hr>

调试器异常通知



Project1.exe引发异常类EOleException,消息为'OLE错误800A03EC'。进程停止。



第34行

解决方案

这很简单:

 使用
ComObj;
....
var
Excel:OleVariant;
....
Excel:= CreateOleObject('Excel.Application');
Excel.Workbooks.Open('myworkbook.xls');
Excel.Run('MyMacro');
Excel.Quit;

确保不要忘记调用 CoInitializeEx 为了启动COM。


Hi my question is how to execute Excel vba code through Delphi 7 program? I found some articles in internet, which weren't described clearly. One of them was with using OLE : Article

I will be grateful if you can give me sample code with the answer. Tha main problem is that I can't understant the code and I need some help. The variable are not declared and I do not no will it work. Thanks in advance

    unit Macro;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ActnList, StdCtrls, Buttons, MSObjCtrls, ComObj, ActiveX;

    type
      TForm1 = class(TForm)
        ActionList1 : TActionList;
        btnMacro : TMSBitBtn;
        acMacro : TAction;
        procedure acMacroExecute(Sender : TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1             : TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.acMacroExecute(Sender : TObject);
    var
      Excel             : OleVariant;
    begin
      Excel := CreateOleObject('Excel.Application');
      Excel.Workbooks.Open('C:\Documents and Settings\iordan.borisov\Desktop\data.xls');
      Excel.Run('PERSONAL.XLSB!MyMacro'); // <<--- Error here.
      Excel.Quit;

    end;
    initialization
      CoInitialize(nil);
    finalization
      CoUninitialize;

    end.

This code throws a run time exception :


Debugger Exception Notification

Project Project1.exe raised exception class EOleException with message 'OLE error 800A03EC'. Process stopped. Use Step or Run to continue.

on line 34

解决方案

It's pretty much as simple as this:

uses
  ComObj;
....
var
  Excel: OleVariant;
....
Excel := CreateOleObject('Excel.Application');
Excel.Workbooks.Open('myworkbook.xls');
Excel.Run('MyMacro');
Excel.Quit;

Make sure you don't forget to call CoInitializeEx in order to initalize COM.

这篇关于通过Delphi Excecute Excel vba代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆