Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Delphi controls MS Office applications Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
11-Sep-02
Category
OLE
Language
Delphi 2.x
Views
109
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: William Gerbert

Delphi controls MS Office applications

Answer:

How can you remote control MS Office applications from your Delphi application? The 
Answer is to use a TOLEContainer.

It requires some interface knowledge to use the right object(s) and their 
properties. Some samples are added to Delphi demos, but all of them are targeted at 
MSWord. I have posted examples for Internet Explorer elsewhere and here is a sample 
for MSExcel:


1   // procedure is activated when OleOject activates user interface
2   // procedure copies TStringGrid content to an (OleObject) Excel sheet
3   
4   procedure TForm1.OleContainer1Activate(Sender: TObject);
5   var
6     ExcelSheet: Variant;
7     Count,
8       Curent: Variant;
9     i,
10      j: Integer;
11  begin
12    // first we read how many sheets are open in a specified Excel document
13    Count := OleContainer1.OleObject.Application.Sheets.Count;
14  
15    // then we read the number of a sheet to witch user wants to add StringGrid 
16  content
17    Curent := StrToInt(OKBottomDlg.Edit2.Text);
18  
19    if Curent <> 0 then
20    begin
21      if Curent <= Count then
22        // if the sheet with index Curent exist then copy content
23      begin
24        // first we activate the desiered sheet object
25        OleContainer1.OleObject.Application.Sheets[Count].Activate;
26        // pass the object to a variant variable
27        ExcelSheet := OleContainer1.OleObject.Application.ActiveSheet;
28  
29        // now we can do what ever we like with it
30        ExcelSheet.name := OKBottomDlg.Edit3.Text + IntToStr(Count);
31        for i := 0 to StringGrid1.RowCount do
32        begin
33          for j := 0 to StringGrid1.ColCount do
34          begin
35            ExcelSheet.Cells(i, j) := StringGrid1.Cells[j, i]
36          end
37        end;
38        // here we copy the content
39      end
40      else // else if the sheet we are trying to access doesn't exsist
41      begin
42        // we add new sheets untill the requested
43        // user's index is reached ( curent variable )
44        for i := Count + 1 to Curent do
45        begin
46          OleContainer1.OleObject.Application.Sheets.Add
47        end;
48        // again we do as above
49        OleContainer1.OleObject.Application.Sheets[Curent].Activate;
50        ExcelSheet := OleContainer1.OleObject.Application.ActiveSheet;
51        ExcelSheet.name := OKBottomDlg.Edit3.Text + IntToStr(Count);
52        for i := 0 to StringGrid1.RowCount do
53        begin
54          for j := 0 to StringGrid1.ColCount do
55          begin
56            ExcelSheet.Cells(i, j) := StringGrid1.Cells[j, i]
57          end
58        end;
59      end
60    end;
61  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC