delphi ole word
2016-03-07 10:10
411 查看
源代码如下:
//Word打印(声明部分)
wDoc,wApp:Variant;
function PrnWordBegin(tempDoc,docName:String):boolean;
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
function PrnWordInsert(lineText:String;bNewLine:boolean=true):boolean;overload;
function PrnWordInsert(var imgInsert:TImage;sBookMark:String=''):boolean;overload;
function PrnWordInsert(var chartInsert:TChart;sBookMark:String=''):boolean;overload;
function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=''):boolean;
procedure PrnWordSave;
procedure PrnWordEnd;
//Word打印(实现部分)
{
功能:基于模板文件tempDoc新建目标文件docName并打开文件
}
function PrnWordBegin(tempDoc,docName:String):boolean;
begin
result:=false;
//复制模版
if tempDoc<>'' then
if not shFileCopy(tempDoc,docName) then exit;
//连接Word
try
wApp:=CreateOleObject('Word.Application');
except
guiInfo('请先安装 Microsoft Word 。');
exit;
end;
try
//打开
if tempDoc='' then
begin
//创建新文档
wDoc:=wApp.Document.Add;
wDoc.SaveAs(docName);
end else begin
//打开模版
wDoc:=wApp.Documents.Open(docName);
end;
except
guiInfo('打开模版失败,请检查模版是否正确。');
wApp.Quit;
exit;
end;
wApp.Visible:=true;
result:=true;
end;
{
功能:使用newText替换docText内容
bSimpleReplace:true时仅做简单的替换,false时对新文本进行换行处理
}
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
var i:Integer;
begin
if bSimpleReplace then
begin
//简单处理,直接执行替换操作
try
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text :=newText;
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
exit;
end;
//自动分行
reWord.Lines.Clear;
reWord.Lines.Add(newText);
try
//定位到要替换的位置的后面
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := False;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute;
wApp.Selection.MoveRight(wdCharacter,1);
//开始逐行插入
for i:=0 to reWord.Lines.Count-1 Do
begin
//插入当前行
wApp.Selection.InsertAfter(reWord.Lines[i]);
//除最后一行外,自动加入新行
if i<reWord.Lines.Count-1 then
wApp.Selection.InsertAfter(#13);
end;
//删除替换位标
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
end;
数据导入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;
//设置表格
wApp := CreateOleobject('word.application');
wApp.visible :=true;
wDoc := wApp.Documents.Open
wTable:=wDoc.Tables.Add(wApp.Selection.Range,16,7);
wApp.Selection.Columns.SetWidth(15,True);
wApp.Selection.MoveRight;
wApp.Selection.Columns.SetWidth(200,True);
wApp.Selection.MoveRight;
//Word打印(声明部分)
wDoc,wApp:Variant;
function PrnWordBegin(tempDoc,docName:String):boolean;
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
function PrnWordInsert(lineText:String;bNewLine:boolean=true):boolean;overload;
function PrnWordInsert(var imgInsert:TImage;sBookMark:String=''):boolean;overload;
function PrnWordInsert(var chartInsert:TChart;sBookMark:String=''):boolean;overload;
function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=''):boolean;
procedure PrnWordSave;
procedure PrnWordEnd;
//Word打印(实现部分)
{
功能:基于模板文件tempDoc新建目标文件docName并打开文件
}
function PrnWordBegin(tempDoc,docName:String):boolean;
begin
result:=false;
//复制模版
if tempDoc<>'' then
if not shFileCopy(tempDoc,docName) then exit;
//连接Word
try
wApp:=CreateOleObject('Word.Application');
except
guiInfo('请先安装 Microsoft Word 。');
exit;
end;
try
//打开
if tempDoc='' then
begin
//创建新文档
wDoc:=wApp.Document.Add;
wDoc.SaveAs(docName);
end else begin
//打开模版
wDoc:=wApp.Documents.Open(docName);
end;
except
guiInfo('打开模版失败,请检查模版是否正确。');
wApp.Quit;
exit;
end;
wApp.Visible:=true;
result:=true;
end;
{
功能:使用newText替换docText内容
bSimpleReplace:true时仅做简单的替换,false时对新文本进行换行处理
}
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
var i:Integer;
begin
if bSimpleReplace then
begin
//简单处理,直接执行替换操作
try
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text :=newText;
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
exit;
end;
//自动分行
reWord.Lines.Clear;
reWord.Lines.Add(newText);
try
//定位到要替换的位置的后面
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := False;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute;
wApp.Selection.MoveRight(wdCharacter,1);
//开始逐行插入
for i:=0 to reWord.Lines.Count-1 Do
begin
//插入当前行
wApp.Selection.InsertAfter(reWord.Lines[i]);
//除最后一行外,自动加入新行
if i<reWord.Lines.Count-1 then
wApp.Selection.InsertAfter(#13);
end;
//删除替换位标
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
end;
数据导入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;
//设置表格
wApp := CreateOleobject('word.application');
wApp.visible :=true;
wDoc := wApp.Documents.Open
wTable:=wDoc.Tables.Add(wApp.Selection.Range,16,7);
wApp.Selection.Columns.SetWidth(15,True);
wApp.Selection.MoveRight;
wApp.Selection.Columns.SetWidth(200,True);
wApp.Selection.MoveRight;
相关文章推荐
- Delphi Excel FastReport
- Delphi word编辑
- Delphi 操作word 表格
- delphi 设置表格样式。
- delphi通过OLE对word进行单元格合并操作
- Delphi 6常用日期与时间函数
- Delphi常用日期函数
- delphi中Message消息的使用方法
- 现在, Delphi 的多线程已经非常易用了!
- Delphi实现截屏存盘的方法
- Why we need interfaces in Delphi
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)
- 深入Delphi -- Windows 消息机制
- Delphi -- 创建 桌面、发送到...、快速启动栏、开始菜单、程序菜单、右键菜 单
- 关于Delphi中的字符串的浅析(瓢虫大作,里面有内存错误的举例)
- String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)
- 外国的Delphi网站
- Delphi word
- delphi 数据导出到word
- Delphi制作DLL