,一个关于用delphi读取word的小疑点,之.
高手进,一个关于用delphi读取word的小问题,在线等之.....
如题:
用Delphi操作word,新建word文档1,把已知word模板里的内容读取到新建的Word文档1中.请高手出招,在线急等之............
------解决方案--------------------
不要急着新建文档1,先打开Word模版,然后另存为文档1
------解决方案--------------------
启动Word时用如下代码:
begin
try
Wordapplication.Connect;
except
MessageDlg( 'Word may not be installed ', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation ';
end;
让Word打开一个指定的文件,需要先放置OpenDialog,然后调用WordApplication.Documents.Open:
var
ItemIndex :OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
begin
if not dlgOpen.Execute then
Exit;
{Open document}
FileName := dlgOpen.FileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := ' ';
PasswordTemplate := ' ';
Revert := True;
WritePasswordDocument := ' ';
WritePasswordTemplate := ' ';
Format := wdOpenFormatDocument;
WordApplication.Documents.Open( FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );
{Assign WordDocument component}
ItemIndex := 1;
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
end;
------解决方案--------------------
导出DBGrid为Word表格。
procedure TForm1.Button1Click(Sender: TObject);
var
WordApp,WordDoc,WordTable:OleVariant;
i,j:integer;
begin
WordApp:=CreateOleObject( 'Word.Application ');
WordApp.Visible:=True;
WordDoc:=WordApp.Documents.Add;
WordTable:=WordDoc.Tables.Add(WordApp.Selection.Range,DBGrid1.DataSource.DataSet.RecordCount+1,DBGrid1.Columns.Count);
for i:=1 to DBGrid1.Columns.Count do
WordTable.Cell(1,i).Range.InsertAfter(DBGrid1.Columns[i-1].Title.Caption);
i:=2;
with DBGrid1.DataSource.DataSet do
while not eof do
begin
for j:=1 to DBGrid1.Columns.Count do
WordTable.Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
Next;
Inc(i);
end;
end;
启动word时用如下代码:
begin
try
wordapplication.connect;
except
messagedlg( 'word may not be installed ', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;
wordapplication.caption := 'delphi automation ';
end;
关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end;
让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:
var
itemindex :olevariant;
如题:
用Delphi操作word,新建word文档1,把已知word模板里的内容读取到新建的Word文档1中.请高手出招,在线急等之............
------解决方案--------------------
不要急着新建文档1,先打开Word模版,然后另存为文档1
------解决方案--------------------
启动Word时用如下代码:
begin
try
Wordapplication.Connect;
except
MessageDlg( 'Word may not be installed ', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation ';
end;
让Word打开一个指定的文件,需要先放置OpenDialog,然后调用WordApplication.Documents.Open:
var
ItemIndex :OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
begin
if not dlgOpen.Execute then
Exit;
{Open document}
FileName := dlgOpen.FileName;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := ' ';
PasswordTemplate := ' ';
Revert := True;
WritePasswordDocument := ' ';
WritePasswordTemplate := ' ';
Format := wdOpenFormatDocument;
WordApplication.Documents.Open( FileName, ConfirmConversions,
ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate,
Revert, WritePasswordDocument, WritePasswordTemplate, Format );
{Assign WordDocument component}
ItemIndex := 1;
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
end;
------解决方案--------------------
导出DBGrid为Word表格。
procedure TForm1.Button1Click(Sender: TObject);
var
WordApp,WordDoc,WordTable:OleVariant;
i,j:integer;
begin
WordApp:=CreateOleObject( 'Word.Application ');
WordApp.Visible:=True;
WordDoc:=WordApp.Documents.Add;
WordTable:=WordDoc.Tables.Add(WordApp.Selection.Range,DBGrid1.DataSource.DataSet.RecordCount+1,DBGrid1.Columns.Count);
for i:=1 to DBGrid1.Columns.Count do
WordTable.Cell(1,i).Range.InsertAfter(DBGrid1.Columns[i-1].Title.Caption);
i:=2;
with DBGrid1.DataSource.DataSet do
while not eof do
begin
for j:=1 to DBGrid1.Columns.Count do
WordTable.Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);
Next;
Inc(i);
end;
end;
启动word时用如下代码:
begin
try
wordapplication.connect;
except
messagedlg( 'word may not be installed ', mterror, [mbok], 0);
abort;
end;
wordapplication.visible := true;
wordapplication.caption := 'delphi automation ';
end;
关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容:
var
savechanges, originalformat, routedocument: olevariant;
begin
savechanges := wddonotsavechanges;
originalformat := unassigned;
routedocument := unassigned;
try
wordapplication.quit(savechanges, originalformat, routedocument);
wordapplication.disconnect;
except
on e: exception do
begin
showmessage(e.message);
wordapplication.disconnect;
end;
end;
end;
让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open:
var
itemindex :olevariant;