|
|
|
Delphi - Microsoft Excel COM (2)
First of all we need to instantiate a COM class and start Excel. Below is a snippet showing how to create a COM object by calling the CoExcelApplication.Create method:
var
Excel: _Application;
AppWasRunning: boolean; { has Excel finished }
lcid: integer; { User ID }
Unknown: IUnknown;
Result: HResult;
WBk : _Workbook; { A Work Book }
WS : _Worksheet; { A Work Sheet }
aWorkSheetName : Name; { A Work Sheet Name }
Filename: OleVariant;
|
..
begin
lcid := LOCALE_USER_DEFAULT;
//----{~~}
Result := GetActiveObject(CLASS_ExcelApplication, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) or (AppWasRunning) then
Excel := CoExcelApplication.Create
else begin
OleCheck(Result); { Make sure the object has been linked }
OleCheck(Unknown.QueryInterface(_Application, Excel));
AppWasRunning := True; { States that excel is now running }
end;
Excel.Visible[lcid] := true;
//Open Workbook below
try
Filename := 'C:\MyExcelFile.xls';
WBk := Excel.Workbooks.Open(Filename, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, LCID);
WS := WBk.Worksheets.Get_Item(1) as _Worksheet;
WS.Activate(LCID);
except
Excel.quit;
MessageDlg('The file could not be opened',mtWarning,[mbOk],0);
Cancel_Process := true;
end;
end;
|
|
|
Featured |
Other popular Delphi resources. Why not try them for yourself?
|
|
Delphi Knowledge Base. Best practices, walkthroughs, tutorials, component advice and more. Solutions and professional designs for Delphi 4, Delphi 5, Delphi 6 and Delphi, 7. Word COM, Excel COM, Outlook COM including code examples for Word OLE, Excel OLE and Outlook OLE. Automating Outlook to create MailItem and send attachments in Delphi. ActiveX, Com. ADO Data Architecture, TMS component pack walk through and code examples. Using Developer Express and example walkthroughs. Date & time conversion routines, video playback and frame manipulation. COM articles, papers, tips, techniques and hints. How to create Word reports and excel spreadsheets using Delphi code and OLE automation. Understanding Delphi encryption and using ciphers, with source code examples of Blowfish, Twofish, and Rijndael. Securing application data and confidential information using open source encryption components. Free encryption dll component. Accessing and managing MS Excel sheets with Delphi. How to retrieve, display and edit Microsoft Excel spreadsheets with Delphi automation. Accessing and managing MS Word documents with Delphi. How to retrieve, display and edit Microsoft Word documents using Delphi automation. Read and write Excel spreadsheets using Delphi code. How to read and write registry entries using Delphi code. Source code examples using Delphi 5, Delphi 6 and Delphi 7. Working with VCL Developer Express components. How to apply grid styles and themes to Delphi applications. Using image columns and shading to improve the grid and treeview appearances. Including examples from ExpressQuantumPack, for Delphi, ExpressPack for Delphi. Developer guides for Developer Express components. All our articles are written by our senior Delphi professionals to help you get the best from your legacy Delphi applications |
|