如何在程序中生成一定格式的word文档?(100分)

  • 主题发起人 主题发起人 鱼片干子
  • 开始时间 开始时间

鱼片干子

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用word对象模型,
1。在word中插入一行,该怎么做?
2。如果还有一些比较复杂的格式,那我应该怎么控呢?
 
Demos/Activex/Oleauto/SrvComp/Word
Demos/Activex/Oleauto/Word8
有你要的例子
 
然后使用varaulnt变量调用(vba for word的帮助word里有)
 
delphi自带例子,但不全,我也想要这方面的资料,不知谁有?
谁要是有全面、详细的资料(最好是中文),我给他200分
 
1.在word中插入一行:
WordDocument.Range.InsertAfter('Delphi大师 ' + #13);
2.比较复杂的格式:把DBImage中的图形copy后再Paste到Word里
DBImgFishImg.CopyToClipboard;
WordDocument.Sentences.Last.Paste;

 
附加功能 将问题提前
 
如已解决,请早日结束!
 
多人接受答案了。
 


1.WORD OLE 服务器的连接:

app_Word:TwordApplication;



app_Word := TwordApplication.Create(self);

app_Word.Connect;

app_Word.Visible := true;

2. WORD 文档的连结:

doc_Word:TWordDocument;



doc_Word:= TWordDocument.Create(self);

app_Word.Documents.Add(EmptyParam,EmptyParam);

doc_Word.Connect;

3.WORD 字体的设置:

app_Word.Selection.Font.Name :='黑体';

app_Word.Selection.Font.Size := 16;

4.WORD 对齐方式的设置:

app_Word.Selection. ParagraphFormat. Alignment := wdAlignParagraphCenter;

5.WORD 中输出文字:

app_Word.Selection.TypeText(FPageHeader.Caption);

6.WORD 中的换行:

app_Word.Selection. TypeParagraph;

7.WORD 中的表格的建立:

tbl : Table;



tbl := app_Word. ActiveDocument. Tables.Add(app_Word.Selection.Range,RowCount,ColCount);

8.WORD 中光标的定位:

app_Word.Selection.GoTo_(Var What,Which,Count,Name: OleVariant);



What 取值: (wdGoToBookmark,wdGoToSection,wdGoToPage,wdGoToTable,wdGoToLine,wdGoToFootnote,wdGoToEndnote,wdGoToComment,wdGoToField,wdGoToGraphic,wdGoToObject,wdGoToEquation,wdGoToHeading,wdGoToPercent,wdGoToSpellingError,wdGoToGrammaticalError,wdGoToProofreadingError)

Which 取值: (wdGoToFirst,wdGoToLast,,wdGoToNext,wdGoToRelative,wdGoToPrevious,wdGoToAbsolute)

Count 取值:(1,2,3….)

Name 取值:string;一般可以为’’;

9.WORD 中光标的移动:

app_Word.Selection.MoveRight(var Unit,Count,Extend: OleVariant);



Unit 取值: (wdCharacter,wdWord,wdSentence,wdParagraph,wdLine,wdStory,wdScreen,wdSection,wdColumn,wdRow,wdWindow,wdCell,wdCharacterFormatting,wdParagraphFormatting,wdTable,wdItem)

Count 取值:(1,2,3…)

Extend 取值:(wdMove,wdExtend)



编程实例:

unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls,ComObj, Grids, OleServer, Word97;



type

TForm1 = class(TForm)

Button1: TButton;

grd_record: TStringGrid;

app_Word: TWordApplication;

doc_Word: TWordDocument;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.Button1Click(Sender: TObject);

var

tbl : Table;

i,j:integer;

un_Var,ex_Var,cnt_Var:OleVariant;

begin

// 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小



app_Word.Connect;

app_Word.Visible := True;

app_Word.Documents.Add(EmptyParam,EmptyParam);

doc_Word.Connect;

app_word.ActiveWindow.View.Type_:= wdNormalView;

app_Word.Selection.Font.Name :='黑体';

app_Word.Selection.Font.Size := 16;

app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;

app_Word.Selection.TypeText('常州市重点外商投资企业在建项目跟踪');

app_Word.Selection.TypeParagraph;

app_Word.Selection.TypeParagraph;

app_Word.Selection.Font.Name := '宋体';

app_Word.Selection.Font.Size :=12;

app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;

app_Word.Selection.TypeText('日期:2000年10月20日');

app_Word.Selection.TypeParagraph;

app_Word.Selection.TypeParagraph;

app_Word.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;

tbl := app_Word.ActiveDocument.Tables.Add(app_Word.Selection.Range,grd_Record.RowCount,grd_Record.ColCount);

un_Var:=wdCharacter;

cnt_Var:=1;

ex_Var:=wdMove;

for i := 0 to grd_Record.RowCount-1 do // Iterate

begin

for j := 0 to grd_Record.ColCount-1 do // Iterate

begin

app_Word.Selection.TypeText(grd_Record.Cells[j,i]);

app_Word.Selection.MoveRight(un_Var,cnt_Var,ex_Var);

end; // foror

app_Word.Selection.MoveRight(un_Var,cnt_Var,ex_Var);

end; // foror

app_Word.Selection.TypeText('制表人:');

app_Word.Selection.TypeParagraph;

self.Close;

end;



end.


 
后退
顶部