Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Automation (that old chestnut) :O) 1

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi

I've been looking at svanels thread in FAQ and come up with this lean version. All I need to do is poke a string in to one cell and fire off an Excel macro but I can't get the last line to compile, comes up with "'[' excpected but ':=' found". Can someone put me right? Also, can anyone tell me how to fire off a macro in XL?

procedure TForm1.Button2Click(Sender: TObject);
var ws : _Worksheet;
begin
ExcelApp.connect;
ExcelSheet.ConnectTo(ExcelApp.Worksheets.Item['TEST'] as _Worksheet);

ws := ExcelApp.ActiveSheet as _Worksheet;
ws.Range['A3', 'A3'].Value := edValue.text;

end;

Btw, I have an ExcelApplication and ExcelWorksheet on my form. I added the 'ws' bit after as I thought I was doing something wrong with the Worksheet component because of the same error.

Thanks in advance
Lou.
 
I've got the poking data bit working but still having trouble firing off a macro. I'm have the following little proc. Any help gratefully received.

procedure TForm1.Button2Click(Sender: TObject);
var LCID : integer;
begin
LCID := GetUserDefaultLCID;
ExcelApp.connect;
ExcelSheet.ConnectTo(ExcelApp.Worksheets.Item['TEST'] as _Worksheet);

ExcelSheet.cells.item[2, 3].value := edPID.text;

ExcelApp.ExecuteExcel4Macro('MyMacro');
ExcelApp.ExecuteExcel4Macro('MyMacro', LCID);
end;
 
Woohoo, got it working, obvious really, needed the 'Run' command! Working example:

procedure TForm1.Button2Click(Sender: TObject);
var ws : _Worksheet;
begin
ExcelApp.connect;
ExcelSheet.ConnectTo(ExcelApp.Worksheets.Item['TEST'] as _Worksheet);
ExcelSheet.Activate;

ExcelSheet.cells.item[2, 3].value := edValue.text;

ExcelApp.Run('MyMacro');
ExcelApp.Disconnect;
end;
 
Just to make it clearer with the components I used.

From the Servers tab, I added 2 components:

TExcelApplication named it ExcelApp
TExcelWorksheet named it ExcelSheet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top