参看Delphi\Demos\Activex\Oleauto\Word8的例子
解决方案 »
- Delphi是不是一种语言
- 用TTS5.1朗读问题
- 高分求ado控件的数据字典的实现(up有分,先给100分,若完美可用可以加到2000分)
- 关于更新 DBGrid 的问题,老提示 can't modify a read-only dataset
- 一个我想不通的问题,大家来讨论讨论
- 编程高手进来看一看!!!!
- 哈哈哈~~~明天开始休假了,今天放分,下午六点结贴~~~~
- 拜托了各位老大,我的rave报表里,为什么老是就一页?????????
- 两个不同的 odbc数据源的 集合插入,用sql语言怎么 实现,有没有一步能实现的?
- Web服务器应用程序浏览的问题
- 程序员考试是不是一年两次?
- 如何连续获取远程计算机屏幕?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, Word97, OleServer, StdCtrls;type
TForm1 = class(TForm)
App: TWordApplication;
Doc: TWordDocument;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
odDoc: TOpenDialog;
SpeedButton6: TSpeedButton;
Button1: TButton;
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton4Click(Sender: TObject);
procedure SpeedButton5Click(Sender: TObject);
procedure SpeedButton6Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
APP.Connect;
app.Visible := true;
end;procedure TForm1.SpeedButton2Click(Sender: TObject);
var Index,sFile,bFlag:OleVariant;
begin
if odDoc.Execute then
begin
sFile:=odDoc.FileName;
bFlag := False;
App.Documents.Open(sFile,bFlag,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
index :=1;
Doc.ConnectTo(App.Documents.Item(index));
end
else
begin
///Documents.Add DocumentType:=wdNewBlankDocument
// ShowVisualBasicEditor = True
///
app.Documents.Add(EmptyParam,EmptyParam);
end;
end;procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
Doc.PrintPreview;
end;procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
// Selection.TypeText Text:="uiyuiyuiyuiyuyuyui"
///这是我录的插入字符的宏
// 思想:宏转换在 vb转换要容易些
app.Selection.TypeText('dfjalfda;fdsajfla');end;procedure TForm1.SpeedButton5Click(Sender: TObject);
var
LinkToFile, SaveWithDocument:OleVariant;
begin
// Selection.InlineShapes.AddPicture FileName:="F:\新建文件夹 (2)\BLOW4.JPG", _
// LinkToFile:=False, SaveWithDocument:=True
odDoc.FileName :='F:\新建文件夹 (2)\BLOW4.JPG';
if odDoc.Execute then
begin
LinkToFile:=False; SaveWithDocument:=True;
app.Selection.InlineShapes.AddPicture(odDoc.FileName,LinkToFile,SaveWithDocument,EmptyParam); end;
{插入表格也类是 用range 可以控制插入的位置}
end;procedure TForm1.SpeedButton6Click(Sender: TObject);
var
append,FileName, Range, Item, Copies, Pages, PageType , ManualDuplexPrint
, Collate, Background, PrintToFile , PrintZoomColumn, from,to_ ,
PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight:OleVariant;begin
{'
' Macro3 Macro
' 宏在 2001-7-24 由 yj 录制
'
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="11", PageType:=wdPrintAllPages _
, ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile _
:=False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
}
FileName:=''; Range:='wdPrintRangeOfPages'; Item:='wdPrintDocumentContent';
Copies:=1; Pages:=2; PageType:='wdPrintAllPages';
ManualDuplexPrint:=False; Collate:=True; Background:=True; PrintToFile :=False; PrintZoomColumn:=0; PrintZoomRow:=0;
PrintZoomPaperWidth:=0;
PrintZoomPaperHeight:=0 ;
append:=false;
from:=2;
to_:=2;
app.PrintOut(Background,Append , Range, filename, from, to_, Item, Copies, Pages, PageType, PrintToFile, Collate, FileName, EmptyParam, ManualDuplexPrint)//, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight) {(Background,EmptyParam,EmptyParam,filename,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,pages,EmptyParam,EmptyParam);}
end;procedure TForm1.Button1Click(Sender: TObject);
var
FileName, FileFormat,LockComments, Password, AddToRecentFiles,
WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts,
SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter:OleVariant;
begin {
' Macro1 Macro
' 宏在 2001-10-26 由 yj 录制
'
ChangeFileOpenDirectory "\\Lxh\MY DOCUMENTS\"
ActiveDocument.SaveAs FileName:="C:\购销合同.doc", FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
}
FileName:='C:\购销合同1.doc'; FileFormat:= wdFormatDocument;
LockComments:=False; Password:='';
AddToRecentFiles:= True; WritePassword:=''; ReadOnlyRecommended:=False; EmbedTrueTypeFonts:= False;
SaveNativePictureFormat:=False; SaveFormsData:=False;
SaveAsAOCELetter:=False; app.ActiveDocument.SaveAs( FileName, FileFormat,LockComments, Password, AddToRecentFiles,
WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts,
SaveNativePictureFormat, SaveFormsData, SaveAsAOCELetter);end;end.
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;
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;
var
WordApp,WordDoc,WordParagraph,WordRange,WordTable:variant;
SltRec,SltCol,VisCol :integer;
ColIndex,RowIndex : Integer;
I,J : integer;
PrintAll : Boolean;
begin
try
if VarIsEmpty(WordApp) then
WordApp := CreateOleObject('word.Application'); WordDoc := WordApp.documents.add;
WordParagraph := WordApp.activedocument.paragraphs.add; WordRange := WordParagraph.range;
WordRange.Font.Size := 18;
WordRange.Font.Name := '宋体';
except
Application.MessageBox(pchar('Ms Word 无法启动,数据转入终止!'),pchar('注意'),mb_ok);
Abort;
end; if DBGrid1.SelectedRows.Count <> 0 then
SltRec:= DBGrid1.SelectedRows.Count
else
SltRec := Table1.RecordCount; SltCol := DBGrid1.Columns.Count; WordRange := WordApp.ActiveDocument.Content;
WordRange.Collapse(wdCollapseEnd);
WordTable:= WordApp.activedocument.tables.add(WordRange,SltRec + 1,SltCol); {打印表头}
for j := 0 to SltCol-1 do
begin
WordTable.Cell(1,J+1).Range.InsertAfter(dbgrid1.Columns[j].Title.Caption);
end; //打印数据
Table1.First;
RowIndex := 2;
ColIndex := 1;
while not Table1.Eof do
begin
if dbgrid1.SelectedRows.CurrentRowSelected then
begin
for j := 0 to dbgrid1.Columns.Count-1 do
begin
WordTable.Cell(RowIndex,ColIndex).Range.InsertAfter
(table1.Fieldbyname(dbgrid1.Columns[j].FieldName).asstring);
ColIndex := ColIndex + 1;
end;
RowIndex := RowIndex + 1;
ColIndex := 1;
end; Table1.Next;
end;{End of While...not...} if savedialog1.Execute then
WordDoc.saveas(savedialog1.FileName); WordApp.Visible := true;
end;