源代码如下:

//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 ole word的更多相关文章

  1. Delphi读取Word

    Delphi读取Word现在关于往Word中写入数据的方法比较多,现在专门开个贴子,希望大家把自己读取Word内容的心得体会说一下,包括读取word文档中,有几个段落,如何读取第几个段落,读取有拼音的 ...

  2. delphi控制 word的几种方法--转

    对几种方法的难易程度的判别 a.通过Delphi的控件TOleContainer 将Word嵌入 这是最简单的Ole嵌入,能够直接将Word文档调用,只需要使用ToleContainer.Run就可以 ...

  3. Delphi 统计Word文档中的字数

    急待解决的问题就是如何用delphi实现word中的统计字数 另外想多了解一些关于操作word的相关内容 比如用ole动态创建的和TWordApplication的偏重点在哪里,有什么不同等等…… 用 ...

  4. Delphi 运行Word VBA 宏 删除软回车

    Sub 整理网页()'整理网页:删除软回车.删除空白段.使段落文字两端对齐Selection.WholeStory        Selection.Find.ClearFormatting    S ...

  5. delphi控制word 标题 字符和位置

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. 用Delphi进行word开发

    使用以CreateOleObjects方式调用Word 实际上还是Ole,但是这种方式能够真正做到完全控制Word文件,能够使用Word的所有属性,包括自己编写的VBA宏代码.------------ ...

  7. Delphi对Word的基本操作

    一.Delphi程序启动Word 采用CreateOleObjects的方法来启动Word,调用VBA代码,具体实现过程为: 首先使用GetActiveOleObject('Word.Applicat ...

  8. Delphi - OLE类实现TTS方式语音朗读

    Delphi调用OLE类实现TTS方式语音朗读 直接看代码: unit uMain; interface uses Windows, Messages, SysUtils, Variants, Cla ...

  9. delphi 712 Word 2

    //导出Wordprocedure TFrm_Computing.ExportWord(aFileName: string; aFileType: string);var wordApp, WordD ...

随机推荐

  1. strut2的原理

    Struts2 在项目中用到的核心是拦截器interceptor,OGNL(Object Graph navigation Language)对象图导航语言(用来操作ValueStack里面的数据), ...

  2. 使用 Storyboard Segue 实作 UIViewController 的切换

    http://blog.csdn.net/mazhen1986/article/details/7791430 Storyboard 是在 iOS 5 SDK 中才出现的新名词,它其实就是原本的 Xi ...

  3. JSP include标签和include指令

    test1.jsp <% int a = 5; out.println(a); %> test2.jsp <jsp:include page="/test1.jsp&quo ...

  4. Jmeter 快速入门教程(三-1) --添加响应断言(即loadrunner中所指的检查点)

    [版权所有: whoistester.com & jmeter.cf] 上一节课,我们创建了一个测试场景,并进行了少量vuser的负载测试. 有时候我们执行了测试,但是发现并不是所有事务都执行 ...

  5. aircrack-ng 字典破解WPA / WPA2

    1. 安装 首先安装两个扩展sudo apt-get install build-essentialsudo apt-get install libssl-dev 然后到http://download ...

  6. 为什么重写equals方法还要重写hashcode方法?

    我们都知道Java语言是完全面向对象的,在java中,所有的对象都是继承于Object类.Ojbect类中有两个方法equals.hashCode,这两个方法都是用来比较两个对象是否相等的. 在未重写 ...

  7. 苹果操作系统Mac OS X

    OS X 是先进的操作系统.基于坚如磐石的 UNIX 基础,设计简单直观,让处处创新的 Mac 安全易用,高度兼容,出类拔萃. UNIX 之威力,Mac 之简单OS X 既简单易用且功能强大.所有的一 ...

  8. seafile安装日志(非教程)

    需要的软件: python 2.7.x(从 Seafile 5.1 开始,python 版本最低要求为2.7) python-setuptools python-imaging python-mysq ...

  9. PowerDesigner概念模型的Notation设置

    原文:PowerDesigner概念模型的Notation设置 在进行数据库设计模型时,分为概念模型设计和物理模型设计两种,概念模型主要是反映真是 世界中的业务关系,也就是我们常用的实体关系图.物理模 ...

  10. List应用举例

    1.集合的嵌套遍历 学生类: package listexercise; /** * Created by gao on 15-12-9. */ public class Student { priv ...