We help IT Professionals succeed at work.

opening Excel within Delphi form

SarikaTyagi
SarikaTyagi asked
on
Medium Priority
2,068 Views
Last Modified: 2012-05-06
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..
Comment
Watch Question

MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
Can you get the window handle of the excel instance you created?

Author

Commented:
No , It says method windowhandle is not supported.
Sr. Software Engineer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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 ?
MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
> 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; ?

Author

Commented:
Thanks MerijnB!! my project is ready .. thanks for ur help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.