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版的创建高权限进程

Delphi winapi: loadstring - 从资源中载入字符串

Delphi searchrec.attr and fadirectory

idhttp使用时内存猛增,如何解决

Delphi中使用可修改的常量

Delphi如何简单取得后缀名

Delphi使用idhttp.get('') 造成假死(堵塞),请问线程idhttp怎么才能做到不出错?

Delphi xe [dcc32 fatal error] f2039 could not create output file 问题的解决

Delphi datetimepicker控件日期格式

Delphi 是否是闰年- isleapyear、isinleapyear

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



打赏

取消

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

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

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

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

评论

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