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
How to create data in Excel and copying it to Word from MS Office 97 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
18-Oct-03
Category
OLE
Language
Delphi 4.x
Views
211
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Igor Siticov

How to transfer data from Excel to Word?

Answer:

Below is demonstration how to perform this task. 

Add these global variables: 

1   XLApp: Variant;
2   WordApp: Variant;
3   
4   Add these constants: 
5   
6   xlWBATWorksheet = -4167;
7   wdDoNotSaveChanges = 0;
8   
9   //For creating data in Excel we shall start it first: 
10  
11  //Starting Excel application:
12  XLApp := CreateOleObject('Excel.Application');
13  // Making it visible:
14  XLApp.Visible := True;
15  // Adding workbook:
16  XLApp.Workbooks.Add[XLWBatWorksheet];
17  // Specifying name of worksheet:
18  XLApp.Workbooks[1].Worksheets[1].Name := 'Delphi Data';
19  
20  //Now inserting data to Excel: 
21  
22  procedure TForm1.InsertData2Excel;
23  var
24    Sheet: Variant;
25    i: Integer;
26  begin
27    Sheet := XLApp.Workbooks[1].Worksheets['Delphi Data'];
28    for i := 1 to 10 do
29      Sheet.Cells[i, 1] := i;
30  end;


And copying data from Excel to Word. 
This process consists of two phrases: 

Data should be copied from Excel into Windows clipboard. 
Data should be pasted from Windows clipboard into the Word. 

For successful completion both Excel and Word should be running. 

Copying data from Excel into Windows clipboard: 

31  procedure TForm1.CopyData;
32  var
33    Sheets: Variant;
34  begin
35    SetFocus;
36  
37    Sheets := XLApp.Sheets;
38    // Selecting our worksheet:
39    Sheets.Item['Delphi Data'].Activate;
40    // Selecting our cells:
41    Sheets.Item['Delphi Data'].Range['A1:A10'].Select;
42    // Copying selected cells into clipboard:
43    Sheets.Item['Delphi Data'].UsedRange.Copy;
44    // Inserting copied data into Word
45    InserData2Word;
46  end;
47  
48  procedure TForm1.InsertData2Word;
49  var
50    Range: Variant;
51    i: Integer;
52  begin
53    // Starting Word:
54    WordApp := CreateOleObject('Word.Application');
55    // Making it visible:
56    WordApp.Visible := True;
57    // Adding new document:
58    WordApp.Documents.Add;
59    // Inserting description text into new document:
60    Range := WordApp.Documents.Item(1).Range;
61    Range.Text := 'This is a column from a spreadsheet: ';
62    for i := 1 to 3 do
63      WordApp.Documents.Item(1).Paragraphs.Add;
64    // Inserting data from clipboard
65    Range :=   
66  WordApp.Documents.Item(1).Range(WordApp.Documents.Item(1).Paragraphs.Item(3).Range.S
67  tart);
68    Range.Paste;
69    for i := 1 to 3 do
70      WordApp.Documents.Item(1).Paragraphs.Add;
71  end;


Don't forget to close Excel and Word by your program termination: 

72  procedure TForm1.FormDestroy(Sender: TObject);
73  begin
74    if not VarIsEmpty(XLApp) then
75    begin
76      XLApp.DisplayAlerts := False; // Discard unsaved files....
77      XLApp.Quit;
78    end;
79  
80    if not VarIsEmpty(WordApp) then
81    begin
82      WordApp.Documents.Item(1).Close(wdDoNotSaveChanges);
83      WordApp.Quit;
84    end;
85  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