DELPHI控制WORD添加多行文字


本文整理自网络,侵删。

 
procedure TForm1.Button1Click(Sender: TObject);  
var  
  TB,Template,  
  NewTemplate,  
  Visible,  
  DocumentType,  
  ItemIndex  :OleVariant;  
begin  
  template:=emptyparam;  
  //使用模板的名称,emptyparam的目的是使word服务器采用默认的参数值  
  newtemplate:=false;  
  //新建文档的类型,True表示为模板,False表示为文档  
  visible:=true;  
  //打捞的窗口是否可见  
  documenttype:=emptyparam;  
  //文档类型,默认为空白文档 ,所以赋值为emptyparam  
  itemindex:=1;  
  try  
    wordApp.Connect();  
  except  
    MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!', mtError, [mbOK], 0);  
    Abort;  
  end;  
  wordApp.Visible := true;  
//  wordApp.ActiveDocument.PageSetup.Orientation := 1;  
  wordApp.Documents.Add(Template, NewTemplate,documenttype,visible);  
  doc.ConnectTo(wordApp.Documents.Item(ItemIndex));  // 赋值worddocument组件  
  //添加多行。#13代表回车换行  
  doc.Range.InsertAfter('test string no 1');  
  doc.Range.InsertAfter(#13+'2');  
  doc.Range.InsertAfter(#13+'3');  
  doc.Range.InsertAfter(#13+'4');  
  doc.Range.InsertAfter(#13+'5');  
  doc.Range.InsertAfter(#13);  
  //在文字后加入一个表格  
  doc.Range.Tables.Add(doc.Words.Last,3,4,EmptyParam,EmptyParam);  
  doc.Range.InsertAfter(#13);   加一个空行  
  TB := doc.Range.Tables.Add(doc.Words.Last,1,10,EmptyParam,EmptyParam);  
  //增加行 ,以下两种方法都可以。  
//  tb.Range.rows.add(emptyparam);    这行代码也可以用来加行  
  doc.Range.Tables.Item(2).Range.Rows.Add(EmptyParam);  
  //增加列  
  doc.Range.Tables.Item(2).Range.Columns.Add(EmptyParam);  
  //合并   
  doc.Range.Tables.Item(2).Cell(1,1).Merge(doc.Range.Tables.Item(2).Cell(2,1));  
  doc.Range.Tables.Item(2).Cell(1,2).Merge(doc.Range.Tables.Item(2).Cell(2,3));  
  wordApp.Disconnect;  
end;  

相关阅读 >>

Delphi 用hook实现dll注入详解

Delphi取得文件图标并在tlistview中显示

Delphi线程池

Delphi tlist性能注意事项

Delphi tstream 详细介绍

Delphi android 保持唤醒状态

Delphi判断字符串是否是数字、字母、大小写字母

Delphi用多媒体库 bass.dll 播放 mp3 - 打开、播放、暂停、停止

Delphi中提取网址链接分路径

Delphi 获取鼠标当前位置的相对坐标

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...