var Template,NewTemplate,ItemIndex:OleVariant;
coli,rowi:integer;
procedure setfont;
begin
WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
if ChkBoxUnderline.checked then WordFont.Underline := 2;
if ChkBoxBold.checked then WordFont.Bold := 1;
if ChkBoxItalic.Checked then WordFont.Italic := 1;
if ChkBoxEmboss.Checked then WordFont.Emboss := 1;
if ChkBoxEngrave.checked then WordFont.Engrave := 1;
if ChkBoxShadow.checked then WordFont.shadow := 1;
if ChkBoxDoublestrike.checked then WordFont.DoubleStrikeThrough := 1;
if ChkBoxStrike.checked then WordFont.StrikeThrough := 1;
WordFont.Size := StrToInt(Size.text);
if Fonttype.Itemindex >= 0 then
WordFont.Name := FontType.Items[FontType.Itemindex];
end;
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
{Create new document}
Template := EmptyParam;
NewTemplate := False;
if ChkBoxNewDoc.Checked then
begin
WordApplication.Documents.Add(Template, NewTemplate);
{Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
end;
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
{Insert data}
DBImgFishImg.CopyToClipboard;
WordDocument.Sentences.Last.Paste;
WordDocument.Range.InsertAfter('Common Name: ' + Table.Fields.Fields[2].AsString + #13);
SetFont;
WordDocument.Range.InsertAfter('Species Name:' + Table.Fields.Fields[3].AsString + #13);
WordDocument.Range.InsertAfter('Length: ' + Table.Fields.Fields[4].AsString + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
with WordDocument do
begin
tables.Add(WordDocument.Words.Last,self.StringGrid1.RowCount,self.StringGrid1.colCount);
for coli:=0 to self.StringGrid1.colCount-1 do
for rowi:=0 to self.StringGrid1.RowCount-1 do
tables.Item(1).Cell(rowi+1,coli+1).Range.Set_Text(self.StringGrid1.Cells[coli,rowi]);
end;
//WordDocument.Range.PageSetup.
WordDocument.Range.PageSetup.Set_BottomMargin(100);
WordDocument.Range.InsertAfter('asbrdqwuier');
BtnCloseWord.Enabled := True;
BtnPrint.Enabled := True;
BtnPreview.Enabled := True;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
运行一下就看到效果了