关于delphi中调用word(已编好的word)文档(27分)

  • 主题发起人 主题发起人 chenliyan163
  • 开始时间 开始时间
C

chenliyan163

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在delphi中调用word 文档
 
给一个例子你自己好好研究,呵呵

1、打开delphi,建立一个工程.
2、在form1上加一个按钮
3、代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure InsertLines(LineNum : Integer);
procedure CreateMailMergeDataFile;
procedure FillRow(Doc : Variant
Row : Integer;
Text1,Text2,Text3,Text4 : String);
private
{ Private declarations }

public
wrdApp, wrdDoc: Variant;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses ComObj;//注意,在这儿要uses comobj才能调用olecreateobject来调用word

Const wdAlignParagraphLeft = 0;
Const wdAlignParagraphCenter = 1;
Const wdAlignParagraphRight = 2;
Const wdAlignParagraphJustify = 3;
Const wdAdjustNone = 0;
Const wdGray25 = 16;
Const wdGoToLine = 3;
Const wdGoToLast = -1;
Const wdSendToNewDocument = 0;

{$R *.DFM}

procedure TForm1.InsertLines(LineNum : Integer);
var
iCount : Integer;
begin
for iCount := 1 to LineNum do
wrdApp.Selection.TypeParagraph;
end;

procedure TForm1.FillRow(Doc : Variant
Row : Integer;
Text1,Text2,Text3,Text4 : String);

begin
Doc.Tables.Item(1).Cell(Row,1).Range.InsertAfter(Text1);
Doc.Tables.Item(1).Cell(Row,2).Range.InsertAfter(Text2);
Doc.Tables.Item(1).Cell(Row,3).Range.InsertAfter(Text3);
Doc.Tables.Item(1).Cell(Row,4).Range.InsertAfter(Text4);
end;

procedure TForm1.CreateMailMergeDataFile;
var
wrdDataDoc : Variant;
iCount : Integer;
begin
// Create a data source at C:/DataDoc.doc containing the field data
wrdDoc.MailMerge.CreateDataSource('C:/DataDoc.doc',,,'FirstName, LastName,' +
' Address, CityStateZip');
// Open the file to insert data
wrdDataDoc := wrdApp.Documents.Open('C:/DataDoc.doc');
for iCount := 1 to 2 do
wrdDataDoc.Tables.Item(1).Rows.Add;
// Fill in the data
FillRow(wrdDataDoc, 2, 'Steve', 'DeBroux',
'4567 Main Street', 'Buffalo, NY 98052');
FillRow(wrdDataDoc, 3, 'Jan', 'Miksovsky',
'1234 5th Street', 'Charlotte, NC 98765');
FillRow(wrdDataDoc, 4, 'Brian', 'Valentine',
'12348 78th Street Apt. 214', 'Lubbock, TX 25874');
// Save and close the file
wrdDataDoc.Save;
wrdDataDoc.Close(False);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
StrToAdd : String;
wrdSelection, wrdMailMerge, wrdMergeFields : Variant;
begin
// Create an instance of Word and make it visible
wrdApp := CreateOleObject('Word.Application');
wrdApp.Visible := True;
// Create a new document
wrdDoc := wrdApp.Documents.Add();
wrdDoc.Select;

wrdSelection := wrdApp.Selection;
wrdMailMerge := wrdDoc.MailMerge;

// Create MailMerge data file
CreateMailMergeDataFile;


// Create a string and insert it into the document
StrToAdd := 'State University' + Chr(13) +
'Electrical Engineering Department';
wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
wrdSelection.TypeText(StrToAdd);

InsertLines(4);

// Insert Merge Data
wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
wrdMergeFields := wrdMailMerge.Fields;

wrdMergeFields.Add(wrdSelection.Range,'FirstName');
wrdSelection.TypeText(' ');
wrdMergeFields.Add(wrdSelection.Range,'LastName');
wrdSelection.TypeParagraph;
wrdMergeFields.Add(wrdSelection.Range,'Address');
wrdSelection.TypeParagraph;
wrdMergeFields.Add(wrdSelection.Range,'CityStateZip');

InsertLines(2);

// Right justify the line and insert a date field with
// the current date
wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphRight;
wrdSelection.InsertDateTime('dddd, MMMM dd, yyyy',False);

InsertLines(2);

// Justify the rest of the document
wrdSelection.ParagraphFormat.Alignment := wdAlignParagraphJustify;

wrdSelection.TypeText('Dear ');
wrdMergeFields.Add(wrdSelection.Range,'FirstName');

wrdSelection.TypeText(',');
InsertLines(2);

// Create a string and insert it into the document
StrToAdd := 'Thank you for your recent request for next ' +
'semester''s class schedule for the Electrical ' +
'Engineering Department. Enclosed with this ' +
'letter is a booklet containing all the classes ' +
'offered next semester at State University. ' +
'Several new classes will be offered in the ' +
'Electrical Engineering Department next semester. ' +
'These classes are listed below.';
wrdSelection.TypeText(StrToAdd);

InsertLines(2);

// Insert a new table with 9 rows and 4 columns
wrdDoc.Tables.Add(wrdSelection.Range,9,4);
wrdDoc.Tables.Item(1).Columns.Item(1).SetWidth(51,wdAdjustNone);
wrdDoc.Tables.Item(1).Columns.Item(2).SetWidth(170,wdAdjustNone);
wrdDoc.Tables.Item(1).Columns.Item(3).SetWidth(100,wdAdjustNone);
wrdDoc.Tables.Item(1).Columns.Item(4).SetWidth(111,wdAdjustNone);
// Set the shading on the first row to light gray

wrdDoc.Tables.Item(1).Rows.Item(1).Cells
.Shading.BackgroundPatternColorIndex := wdGray25;
// BOLD the first row
wrdDoc.Tables.Item(1).Rows.Item(1).Range.Bold := True;
// Center the text in Cell (1,1)
wrdDoc.Tables.Item(1).Cell(1,1).Range.Paragraphs.Alignment :=
wdAlignParagraphCenter;

// Fill each row of the table with data
FillRow(wrdDoc, 1, 'Class Number', 'Class Name', 'Class Time',
'Instructor');
FillRow(wrdDoc, 2, 'EE220', 'Introduction to Electronics II',
'1:00-2:00 M,W,F', 'Dr. Jensen');
FillRow(wrdDoc, 3, 'EE230', 'Electromagnetic Field Theory I',
'10:00-11:30 T,T', 'Dr. Crump');
FillRow(wrdDoc, 4, 'EE300', 'Feedback Control Systems',
'9:00-10:00 M,W,F', 'Dr. Murdy');
FillRow(wrdDoc, 5, 'EE325', 'Advanced Digital Design',
'9:00-10:30 T,T', 'Dr. Alley');
FillRow(wrdDoc, 6, 'EE350', 'Advanced Communication Systems',
'9:00-10:30 T,T', 'Dr. Taylor');
FillRow(wrdDoc, 7, 'EE400', 'Advanced Microwave Theory',
'1:00-2:30 T,T', 'Dr. Lee');
FillRow(wrdDoc, 8, 'EE450', 'Plasma Theory',
'1:00-2:00 M,W,F', 'Dr. Davis');
FillRow(wrdDoc, 9, 'EE500', 'Principles of VLSI Design',
'3:00-4:00 M,W,F', 'Dr. Ellison');

// Go to the end of the document

wrdApp.Selection.GoTo(wdGotoLine,wdGoToLast);
InsertLines(2);

// Create a string and insert it into the document
StrToAdd := 'For additional information regarding the ' +
'Department of Electrical Engineering, ' +
'you can visit our website at ';
wrdSelection.TypeText(StrToAdd);
// Insert a hyperlink to the web page
wrdSelection.Hyperlinks.Add(wrdSelection.Range,'http://www.ee.stateu.tld');
// Create a string and insert it into the document
StrToAdd := '. Thank you for your interest in the classes ' +
'offered in the Department of Electrical ' +
'Engineering. If you have any other questions, ' +
'please feel free to give us a call at ' +
'555-1212.' + Chr(13) + Chr(13) +
'Sincerely,' + Chr(13) + Chr(13) +
'Kathryn M. Hinsch' + Chr(13) +
'Department of Electrical Engineering' + Chr(13);
wrdSelection.TypeText(StrToAdd);

// Perform mail merge
wrdMailMerge.Destination := wdSendToNewDocument;
wrdMailMerge.Execute(False);

// Close the original form document
wrdDoc.Saved := True;
wrdDoc.Close(False);

// Notify the user we are done.
ShowMessage('Mail Merge Complete.');

// Clean up temp file
DeleteFile('C:/DataDoc.doc');

end;

end.
 
大哥,怎么点了没反应啊,
 
TO:心灵琴音
很热心!
 
上面那个例子我是从msdn上找到的,你不能运行吗?

我自己写过这方面的例子,你看一下,如果可以运行则可

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure InsertLines(LineNum:Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
wordApp,wordDoc:Variant;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses ComObj;

Const wdAlignParagraphLeft=0;
Const wdAlignParagraphCenter=1;
Const wdAlignParagraphRight=2;
Const wdAlignParagraphJustify=3;
Const wdAdjustNone=0;
Const wdGray25=16;
Const wdGoTOLine=3;
Const wdGoToLast=-1;
Const wdSendToNewDocument=0;

{$R *.DFM}

//在文档中插入空行
procedure TForm1.InsertLines(LineNum:Integer);
var
iCount:Integer;
begin
for iCount:=1 to LineNum do
wordApp.Selection.TypeParagraph;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
StrAdd:String;
i:Integer;
wrdSelection:Variant;
begin
//创建一个word对象
wordApp:=CreateOleObject('Word.Application');
wordApp.Visible:=true;
//创建一个word文档
wordDoc:=wordApp.Documents.Add();
wordDoc.Select;

wrdSelection:=wordApp.selection;

//在文档中插入内容
StrAdd:='教务管理员职务说明书';
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
wrdSelection.font.bold:=true;
wrdSelection.font.size:=15;
wrdSelection.font.Underline:=1;
wrdSelection.TypeText(StrAdd);

wrdSelection.font.Underline:=0;
wrdSelection.font.bold:=false;
wrdSelection.font.size:=11;
InsertLines(1);

//在文档中插入一个4行4列的表格,并格式化
wordDoc.Tables.Add(wrdSelection.Range,4,4,2,0);
wordDoc.Tables.Item(1).Borders.Item(1).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(2).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(3).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(4).LineStyle:=7;
for i:=1 to 4 do
begin
wordDoc.Tables.Item(1).Cell(i,1).Range.Bold:=true;
wordDoc.Tables.Item(1).Cell(i,3).Range.Bold:=true;
wordDoc.Tables.Item(1).Rows.Item(i).Range.Paragraphs.Alignment:=wdAlignParagraphCenter;
end;
//在第一个表格中插入内容
wordDoc.Tables.Item(1).Cell(1,1).Range.text:='岗位名称';
worddoc.tables.item(1).cell(1,2).range.InlineShapes.AddPicture('E:/heartsong/0003.gif',False,True);
wordDoc.Tables.Item(1).Cell(2,1).Range.InsertAfter('岗位级别');
wordDoc.Tables.Item(1).Cell(3,1).Range.InsertAfter('隶属部门');
wordDoc.Tables.Item(1).Cell(4,1).Range.InsertAfter('直接上级');
wordDoc.Tables.Item(1).Cell(1,3).Range.InsertAfter('岗位编号');
wordDoc.Tables.Item(1).Cell(2,3).Range.InsertAfter('现任职者');
wordDoc.Tables.Item(1).Cell(3,3).Range.InsertAfter('分支机构');
wordDoc.Tables.Item(1).Cell(4,3).Range.InsertAfter('直接下级');

//插入一个5行一列的表格:工作概述
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(5).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;

wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('一、工作概述:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');

//插入一个5行一列的表格:主要工作职责
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(6).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;

wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('二、主要工作职责:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');

//插入一个5行一列的表格:工作关系:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(7).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;

wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('三、工作关系:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');

//插入一个5行一列的表格:工作权限:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(8).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;

wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('四、工作权限:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');

//插入一个5行一列的表格:任职资格与要求:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(9).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;

wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('五、任职资格与要求::');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'学历:'+'一些内容');
wrdSelection.TypeText(chr(10)+'工作经历:'+'一些内容');
wrdSelection.TypeText(chr(10)+'要求具备的胜任特质:'+'一些内容');

//插入编制人等内容
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

//编制人编制日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('编制人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('编制人'+' ');

wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('编制日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('编制日期');

InsertLines(1);

//审核人审核日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('审核人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('审核人'+' ');

wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('审核日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('审核日期');

InsertLines(1);

//批准人批准日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('批准人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('批准人'+' ');

wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('批准日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('批准日期');

InsertLines(1);

//操作员操作日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('操作员:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('操作员'+' ');

wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('操作日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('操作日期');

//保存文档
{此处代码略之}
end;
end.

 
谢谢心灵琴音哥哥,
可否留个QQ啊
 
我不常上qq:2651879
你可以msn上找我:zwhu_1980@hotmail.com,希望多多交流
 
这位大哥,我只有QQ
 
对了,我想问一下,当装入图像时,可不可以限制图像的大小啊
 
肯定是可以的,呵呵
 
那怎么做,哥哥,可以告诉我吗
 
我已经做好了
 
后退
顶部