源代码如下:

//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. java基础知识回顾之java集合类-Properties集合

    /** java.lang.Object   |--java.util.Dictionary<K,V>      |--java.util.Hashtable<Object,Obje ...

  2. DJANGO中,用QJUERY的AJAX的json返回中文乱码的解决办法

    和网上其它用JAVA或是PHP的实现不太一样, DJANGO中的解决办法如下: 后端样例: def render_to_json_response(context, **response_kwargs ...

  3. DJANGO变动库的一次真实手动经历

    在变更库时,由于对字段规划和约束性没考虑完全,需要手工操作数据库,以便可以重复执行. 有以下三点要注意. 1,先迎合错误输出,增删对应的表或字段. 2,必要时,修改migrations文件,以去除唯一 ...

  4. iptables 代理设置

    代理: 开启转发:echo 1 > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ...

  5. Project Euler 93:Arithmetic expressions 算术表达式

    Arithmetic expressions By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and mak ...

  6. JavaWeb项目开发案例精粹-第3章在线考试系统-005action层

    1. <?xml version="1.0" encoding="UTF-8" ?><!-- XML声明 --> <!DOCTYP ...

  7. 核心思想:百度网盘怎么盈利(互联网的高速更新决定了:亏钱你还有点机会,放弃连门都进不了),战略预备队 good

    百度做网盘很大程度就是为了防止别人依靠网盘做大和积累点技术储备.腾讯邮箱怎么赚钱?腾讯影音怎么赚钱?互联网的高速更新决定了,一些你看不起眼的软件很可能就会席卷整个市场,所以互联网大佬宁愿一些项目亏钱也 ...

  8. swift:类型转换(is用作判断检测、as用作类型向下转换)

    类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式.   类型转换在Swift中使用is 和 as操作符实现.这两个操作符提供了一种简单达意的方式去检查值的类型或者转换 ...

  9. 怎么在eclipse里调试WebDriver的源代码

    当你看完WebDriver的工作原理这篇博客以后,是不是也跃跃欲试想印证文章里的理论是不是正确,想自己也看下webdriver的源代码,并且调试下,通过代码来更深入的了解WebDriver的工作原理. ...

  10. Zookeeper核心机制

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) Zookeeper是Hado ...