• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1522
  • Last Modified:

opening Excel within Delphi form

Hi experts,
I need to connect Excel through Delphi and need to open it within the delphi for and shd be able to add/format worksheets.

I have tried both way.. through variant variables and olecontainer.
through variant variables:
procedure TForm1.Button1Click(Sender: TObject);
var
 WorkBook: variant;
 ExcelObject: Variant;
begin
 ExcelObject:= CreateOleObject('Excel.Application');
 WorkBook := ExcelObject.Workbooks.Add;
 WorkBook.WorkSheets[1].Name := 'Change Control';
 WorkBook.WorkSheets[2].Name := 'NRI IMSI Series';
 WorkBook.WorkSheets[3].Name := 'IMSI Series';
// ExcelObject.parent := Panel1;
 ExcelObject.Visible := True;
end;

This works perfactly fine, i can format the sheets, add sheets and everything that i need to do ..
The only thing, i am not able to do is ... to display excel within my form instead of creating new excel application.

Ole Container :
procedure TForm1.Button1Click(Sender: TObject);
var
lcid : Integer;
begin
  oc.CreateObject('Excel.Sheet', True);
  ExcelApplication1.Connect;
  lcid := GetUserDefaultLCID;
  ExcelApplication1.Visible[lcid]:=true;
  ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(TOleEnum(xlWBATWorksheet), lcid));
  ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1] as _Workbook);
  ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
  ExcelWorksheet1.Activate;
end;

this works the way i wanted i.e showing the excelsheet within the form through olecontainer.
But now i am not able to add/format the worksheets..

Can anyone suggest a solution using any of the method...
i need it urgently..

Thanks in advance..
0
SarikaTyagi
Asked:
SarikaTyagi
  • 3
  • 3
1 Solution
 
MerijnBSr. Software EngineerCommented:
Can you get the window handle of the excel instance you created?
0
 
SarikaTyagiAuthor Commented:
No , It says method windowhandle is not supported.
0
 
MerijnBSr. Software EngineerCommented:
see this working example:
function TForm2.GetWindowHandle(ExcelObject: Variant): hwnd;
var s: string;
begin
 // temp change caption so we can find it
 s := 'excel find me';
 ExcelObject.Caption := s;
 
 result := FindWindow(nil, pchar(s));
 
 // change the caption back to normal
 ExcelObject.Caption := '';
end;
 
 
procedure TForm2.EmbedInPanel(WindowHandle: hwnd);
var Style: integer;
begin
 // remove the caption
 Style := GetWindowLong(WindowHandle, GWL_STYLE);
 Style := Style and (not (WS_CAPTION));
 SetWindowLong(WindowHandle, GWL_STYLE, Style);
 
 // move to the panel
 if Windows.SetParent(WindowHandle, Panel1.Handle) = 0 then
  style := GetLastError();
 
 // set size correctly
 SetWindowPos(WindowHandle, 0, 0, 0, Width, Height, SWP_NOZORDER or SWP_SHOWWINDOW);
 ShowWindow(WindowHandle, SW_MAXIMIZE);
end;
 
procedure TForm2.Button1Click(Sender: TObject);
var
 WorkBook: variant;
 ExcelObject: Variant;
 WindowHandle: hwnd;
begin
 ExcelObject:= CreateOleObject('Excel.Application');
 
 WindowHandle := GetWindowHandle(ExcelObject);
 if WindowHandle <> 0 then
  EmbedInPanel(WindowHandle);
 
 WorkBook := ExcelObject.Workbooks.Add;
 WorkBook.WorkSheets[1].Name := 'Change Control';
 WorkBook.WorkSheets[2].Name := 'NRI IMSI Series';
 WorkBook.WorkSheets[3].Name := 'IMSI Series';
// ExcelObject.parent := Panel1;
 ExcelObject.Visible := True;
end;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SarikaTyagiAuthor Commented:
Thanks MerijnB !!!
Its working for me.
But its taking much time to get open. Can we optimize it.
Also how can i quit the excel instance ?
0
 
MerijnBSr. Software EngineerCommented:
> But its taking much time to get open. Can we optimize it.

Nope, you are just waiting for Excel to open up. I know a faster way, but that's gonna cost you money

> Also how can i quit the excel instance ?

Did you try ExcelObject.Quit; ?
0
 
SarikaTyagiAuthor Commented:
Thanks MerijnB!! my project is ready .. thanks for ur help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now