源代码如下:

//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. GDB笔记

    GDB是在Linux命令行下对C/C++的程序进行调试常用的一个命令,现将平时记录在本子上的笔记整理如下: 一.断点 断点类型有breakpoints, watchpoints, catchpoint ...

  2. 不用Invoke就等用 Control.CheckForIllegalCrossThreadCalls = false;

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Android Studio安装、配置

    Google在2013年I/O大会上发布了Android Studio,AndroidStudio是一个基于IntelliJ IDEA的Android开发工具.这个IDE要比eclipse智能很多,具 ...

  4. linux 免交互状态下修改用户密码

    当利用某些工具对linux用户进行远程密码更改时,输入[ passwd 用户名 ] 后需要输入两次密码, 但是如果你利用的某些工具无法与linux进行交互的情况下,就没办法变更用户密码了,这个时候可以 ...

  5. player/stage 学习---安装

    环境 ubuntu 14.04 一,工具安装 sudo apt-get install git cmake g++ fltk1.1-dev libjpeg8-dev libpng12-dev libg ...

  6. UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树

    Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/ ...

  7. 在Linux中创建静态库和动态库

    我们通常把一些公用函数制作成函数库,供其它程序使用. 函数库分为静态库和动态库两种. 静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库. 动态库在程序编译时并不会被连接到目标代码中 ...

  8. Android Material Design-TabLayout的使用

    TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...

  9. 异常:Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.sf.log4jdbc.Properties

    参考文章: 使用Log4jdbc-log4j2监听MyBatis中运行的SQL和Connection 使用 log4jdbc格式化输出SQL,maven配置如下: <dependency> ...

  10. Hibernate与Jpa的关系,终于弄懂

    我知道Jpa是一种规范,而Hibernate是它的一种实现.除了Hibernate,还有EclipseLink(曾经的toplink),OpenJPA等可供选择,所以使用Jpa的一个好处是,可以更换实 ...