300分求Delphi7中调用Word XP的代码!(300分)

  • 主题发起人 主题发起人 DelCai
  • 开始时间 开始时间
D

DelCai

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟近日做了一个人事管理数据库,遇到了一些难题,希望大虾帮忙!
具体要求是把一个人员基本情况的表格输出到Word文档中,并且能够修改保存。
我建了一个Word模版,并且画好了表格,填入了一些固定的文字,剩下的我就不知道怎么办了。
请大虾指点一二,并能给出源码!测试通过后即奉上!肯请帮忙,小弟这里先谢了。
 
unit cnsword;

interface

const
{WdColorIndex}
wdAuto = 0;
wdBlack = 1;
wdBlue = 2;
wdTurquoise = 3;
wdBrightGreen = 4;
wdPink = 5;
wdRed = 6;
wdYellow = 7;
wdWhite = 8;
wdDarkBlue = 9;
wdTeal = 10;
wdGreen = 11;
wdViolet = 12;
wdDarkRed = 13;
wdDarkYellow = 14;
wdGray50 = 15;
wdGray25 = 16;
wdByAuthor = -1;
wdNoHighlight = 0;
{WdOrientation}
wdOrientPortrait = 0;
wdOrientLandscape = 1;
{WdPaperSize}
wdPaper10x14 = 0;
wdPaper11x17 = 1;
wdPaperLetter = 2;
wdPaperLetterSmall = 3;
wdPaperLegal = 4;
wdPaperExecutive = 5;
wdPaperA3 = 6;
wdPaperA4 = 7;
wdPaperA4Small = 8;
wdPaperA5 = 9;
wdPaperB4 = 10;
wdPaperB5 = 11;
wdPaperCSheet = 12;
wdPaperDSheet = 13;
wdPaperESheet = 14;
wdPaperFanfoldLegalGerman = 15;
wdPaperFanfoldStdGerman = 16;
wdPaperFanfoldUS = 17;
wdPaperFolio = 18;
wdPaperLedger = 19;
wdPaperNote = 20;
wdPaperQuarto = 21;
wdPaperStatement = 22;
wdPaperTabloid = 23;
wdPaperEnvelope9 = 24;
wdPaperEnvelope10 = 25;
wdPaperEnvelope11 = 26;
wdPaperEnvelope12 = 27;
wdPaperEnvelope14 = 28;
wdPaperEnvelopeB4 = 29;
wdPaperEnvelopeB5 = 30;
wdPaperEnvelopeB6 = 31;
wdPaperEnvelopeC3 = 32;
wdPaperEnvelopeC4 = 33;
wdPaperEnvelopeC5 = 34;
wdPaperEnvelopeC6 = 35;
wdPaperEnvelopeC65 = 36;
wdPaperEnvelopeDL = 37;
wdPaperEnvelopeItaly = 38;
wdPaperEnvelopeMonarch = 39;
wdPaperEnvelopePersonal = 40;
wdPaperCustom = 41;
implementation

end.

 
和调用WORD2000的程序没有什么区别的。
 
你真幸运,刚好写完一个控制 Word 插入表格的代码,给你共享出来:

procedure TForm1.MakeWordFile(const UserID : string);
var
Bookmark: TBookmark;
RangeW: Word97.Range;
v1: Variant;
ov1: OleVariant;
Row1: Word97.Row;
sQANDD : string;//问题与难点
sMemo : string;//备注
sSender:string;//提交人
sPOSTDATE :string;//提交日期
fName : OleVariant;
sUserName : string;//
sfName : string;
i , j : integer;
begin
// insert title
WordDocument1.Range.Text := '文档标题';
//格式必须在最后设置
WordDocument1.PageSetup.Orientation := wdOrientLandscape;//横向
WordDocument1.Range.Font.Size := 14;
WordDocument1.Range.Font.Bold := 0;

WordDocument1.Tables.Add(WordDocument1.Words.Last,Table1.RecordCount+1,5,EmptyParam,EmptyParam);
WordDocument1.Tables.Item(1).Cell(1,1).Range.Text := '格1';
WordDocument1.Tables.Item(1).Cell(1,2).Range.Text := '格2';
WordDocument1.Tables.Item(1).Cell(1,3).Range.Text := '格3';
WordDocument1.Tables.Item(1).Cell(1,4).Range.Text := '格4';
WordDocument1.Paragraphs.Last.Range.Text := ' ';//必须填这句,否则两个表格就粘合在一起了
WordDocument1.Tables.Item(1).Cell(1,5).Range.Text := '格5';
Table1.First;
for i := 1 to Table1.RecordCount do
begin
WordDocument1.Tables.Item(1).Cell(1+i,1).Range.Text := Table1.FieldByName('ITEM').AsString;
WordDocument1.Tables.Item(1).Cell(1+i,2).Range.Text := Table1.FieldByName('Description').AsString;
WordDocument1.Tables.Item(1).Cell(1+i,3).Range.Text := Table1.FieldByName('Result').AsString;
WordDocument1.Tables.Item(1).Cell(1+i,4).Range.Text := Table1.FieldByName('MEASURE').AsString;
WordDocument1.Tables.Item(1).Cell(1+i,5).Range.Text := Table1.FieldByName('POSTDATE').AsString;
Table1.Next;
end;

WordDocument1.Tables.Add(WordDocument1.Words.Last,Table2.RecordCount +1,2,EmptyParam,EmptyParam);
WordDocument1.Tables.Item(2).Cell(1,1).Range.Text := '其它';
WordDocument1.Tables.Item(2).Cell(1,2).Range.Text := '备注';

Table2.First;
for i := 1 to Table2.RecordCount do
begin
WordDocument1.Tables.Item(2).Cell(1+i,1).Range.Text := Table2.FieldByName ('QANDD').AsString;
WordDocument1.Tables.Item(2).Cell(1+i,2).Range.Text := Table2.FieldByName ('MEMO').AsString;
Table2.Next;
end;
//***********************设置标题文字格式*********************************
WordDocument1.Paragraphs.Item(1).Range.Select;
WordDocument1.Paragraphs.Item(1).Range.Font.Size := 30;
WordDocument1.Application.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
//****************************保存文件****************************************
sUserName := lstUser.Items[lstUser.ItemIndex];
ZipStr(sUserName, #10);
sfName := Trim(edFileDir.Text)+'/' + sUserName + ' ' + Trim(edDate.Text)+'.doc';
fName := sfname;
//保存
WordDocument1.SaveAs2000(fname);
WordDocument1.Close;//关闭
end;
 
我说的是如何运用word的模版来实现,因为表格比较复杂所以在delphi中画起来可能很麻烦,所以我想如何调用模版,并替换掉可变的字符串!
 
Delphi+Word解决方案参考
http://www.delphibbs.com/keylife/iblog_show.asp?xid=3680
看看吧,或许对你有帮助
 
“因为表格比较复杂所以在delphi中画起来可能很麻烦”
非也!用模板倒是更麻烦,并且不够灵活。全部用编程实现的难度跟使用模板差不多。
建议你还是先关注一下helloqiner及wangxian11的意见,了解一下基本知识。
 
感谢楼上的几位,小弟研究了几天还是一点头绪都没有。希望哪位大哥行行好,指点一下,贴个源码!!!小弟这里先谢了!!!
 
给你一点思路吧:
在表格中需要填写数据的地方标记出来,比如说要填写姓名的地方,那么在WORD摸板中写上<#name>,在程序中把 <#name>替换成真实的名字。
 
procedure TFrmMain.SpeedButton9Click(Sender: TObject);
Var
StrTemp : String;
ZS : Integer;
Nian, Yue, Ri : Word;

ItemIndex :OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;

FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, ReplaceWith, Replace: OleVariant;

SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
FileName := ExtractFilePath(ParamStr(0)) + 'DOC/考察材料.DOC';
try
// WordApplication1.Disconnect;
WordApplication1.Connect;
except
Application.MessageBox('连接WORD服务器失败,请确定您已经正确安装。','错误框',MB_OK+MB_ICONSTOP);
Abort;
end;

try
Frmjd := TFrmjd.Create(Self);

//显示进度
Frmjd.ProgressBar1.Max:=100;
Frmjd.Show;

WordApplication1.Visible := False;
WordApplication1.Caption := '干部考察材料';

//参数赋值
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;

//打开文档(摸板)
WordApplication1.Documents.Openold( FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );


{Assign WordDocument component}
ItemIndex := 1;
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));

{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;

//让Word替换标记字符串要使用WordDocument.Range.Find.Execute:姓名
FindText := '<#XM>';
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith := DM.ADOQryMainXM.Value;
Replace := wdReplaceAll;
WordDocument1.Range.Find.Executeold( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace );
Frmjd.ProgressBar1.Position := 10;//进度
Frmjd.Update;//进度窗口刷新

这是我以前的代码,没修改和注释就贴上来,希望你能看懂,呵呵
 
请问hongjg,是直接在模版上写出<#name>吗?参数FindText是需要替换的字符,而 ReplaceWith 是替换的字符,对吗?我试过了,为什么不行呢?
 
等得花儿都谢了,那位大哥帮帮忙!
 
感谢楼上的几位
 
直接在WORD模版上写出<#name>。
FindText := '<#XM>';//这个是模板中的标记。
MatchCase := False;//匹配大小写。
MatchWholeWord := True;//匹配整个词语,这个不管,添True就行
MatchWildcards := False;//不清楚,这个不管,添False就行
MatchSoundsLike := False;//不清楚,这个不管,添False就行
MatchAllWordForms := False;//不清楚,这个不管,添False就行
Forward := True;//不清楚,这个不管,添True就行
Wrap := wdFindContinue;//不清楚,这个不管,用wdFindContinue就行
Format := False;//不清楚,这个不管,添False就行
ReplaceWith := DM.ADOQryMainXM.Value;//数据库的内容啊。
Replace := wdReplaceAll;//全部替换。
你说的都对啊,怎么不行呢?出错信息?
 
var
WordTable,Itemindex,Row,col,range:OleVariant;
OFormat :OleVariant;
rowNum,iTemp:integer;
WordApp,WordDoc,wordShape:OleVariant;
Template,NewTemplate,Docutype,visible:OleVariant;
graphShape,chart,chartDs: OleVariant;
begin

try
WordApp:=CreateOleObject('Word.Application');
WordApp.Visible:=True;
except
Application.MessageBox('WORD³ÌÐòÆô¶¯Ê§°Ü!','²Ù×÷ÎÞ·¨½øÐÐ');
Abort;
exit;
end;

try
Template := '¹âÍøÂç²úƷά»¤Ô±¨Ä£°å.dot';
NewTemplate :=false;//
DocuType :=0;
Visible :=true;

wordDoc:=WordApp.Documents.Add(Template,NewTemplate,DocuType,Visible);
worddoc.Range(Itemindex,itemindex);
 
hongjg兄太感谢了,我找这样的例子已经找了两天,在csdn上没人理我,正在郁闷中,没想到来这边刚注册近来就找到了。谢谢!非常谢谢!
[:D][:D][:D][8D][8D]
 
这一贴我学了很多,谢谢!
 
好好学习,天天向上
 
后退
顶部