We need to be able to traverse all the macros in the file and then we execute a list of pre-defined macros using OLE. In v3 we used the XLSComponent.VBA.Count property to loop through any VBA sections in the file. The XLSComponent.VBA.Modues[inx].Source.Count property to loop through the methods in the file and see if any of the macros we auto exectute is present.
That code worked fine in v3, but in v4 the "XLSComponent.VBA.Count" always returns 0, even if macros are present in the file.
I created an example project, but see no way to attach it to the post. Below is the code for the project, just create a new project, save the form calling it frmMacroExample, add a tbutton & tmemo and replace the code with the code below.
Did the the functionality change in v4, should we be using different code to access the Macros? This is vital and our clients are waiting for this functionality, so a quick response will be appreciated.
Thanks,
Ben
Code: Select all
unit frmMacroExample;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
XLSReadWriteII4, BIFFRecsII4, CellFormats4, XLSFonts4, SheetData4, MergedCells4, CellAreas4, MoveCopy4;
procedure TForm1.Button1Click(Sender: TObject);
var
XLS : TXLSReadWriteII4;
iCount, iMacroLines : Integer;
FileName : string;
begin
Memo1.Clear;
Filename := ExtractFilePath(Application.ExeName) + 'MacroWorkbook.xls';
if (not FileExists(Filename)) then
Exit;
XLS := TXLSReadWriteII4.Create(nil);
try
XLS.Filename := FileName;
XLS.Read;
For iCount := 0 to (XLS.VBA.Count - 1) do
For iMacroLines := 0 To (XLS.VBA.Modules[iCount].Source.Count - 1) do
Memo1.Lines.Add(XLS.VBA.Modules[iCount].Source.Strings[iMacroLines]);
finally
XLS.Free
end;
end;
end.