Office自动化编程(1)

1:控制Word文档

 {显示word档案}

 var NewWord:Variant;

...

  NewWord:=  CreateOLEObject('Word.Application');

  {

   当CreateOLEObject函数创建一个Word的Com对象之后,Word已经完全被构造起来了

   内存中已经存在一个Word进程,创建了Application对象

  }

  NewWord.Visible := True;   // 显示Word程序

 

  NewWord.Documents.Add;  // 增加一个Word文档,Application.Documents嵌套类

 

  NewWord.Documents.Item(1).Range.Text:=  str;

  Document := NewWord.Documents.Item(1);

  Document.Paragraphs.Add;

 

  MyRange:= Document.Range(start:=0, end:=0);

 

2:向已存在的word表格中添加字符串

 

{ 以指定的模板文件创建新Word文档 }

  wordApp.Documents.AddOld(templateName, newTemplate);

  wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex];

 

  case ListBox1.ItemIndex of

   0:

     begin

       { 取文档中的第1张表 }

       { 文档的表格是以序号来标志的 }

       vTable := wordApp.ActiveDocument.Tables.Item(1);

       vTable.Cell(1, 2).Range.Text := '李鑫 北京供电公司';

vTable.Cell(1, 4).Range.Text := '昆明锐祺软件';

 

vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B 李鑫(收) 650051 (0871)3126628';

 

       vTable.Cell(3, 2).Range.Text := DateToStr(Now);

       vTable.Cell(3, 4).Range.Text := DateToStr(Now);

 

       vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。';

       vTable.Cell(7, 1).Range.Text := '不知道了';

       vTable.Cell(9, 2).Range.Text := '不知道';

     end;

   1:

     begin

       ItemIndex := 1;

     end;

  end;

 

 

说明:

Document对象,下面有:

 

Range对象

Section对象

Charactions对象

Words对象

 

最重要的是Range对象,要对Decument文档的字符串操作,必须返回操作Doecment内的Range对象

用Document.Range(start, end),和Document.Range属性,都可以返回这个对象

 

每操作一段文本,必须取得Range这个表示文本的对象,果然非常是“面向对象“

 

3:  添加图片

 

procedure TForm1.Button2Click(Sender: TObject);
var
  w: Variant;
begin
  W:= CreateOleObject('Word.Application');
  W.Visible := True;
  W.Documents.open('c:/范例文章.doc');
  // W.ActiveDocument.SaveAs('TestDoc.doc');
  w.Selection.Font.Bold:= false;
  W.Selection.TypeText('Here goes my picture:');
  W.Selection.TypeParagraph;    // 加入一个段落


  W.Selection.InlineShapes.AddPicture( 'c:/相片2.jpg', False, True );
  W.Selection.TypeParagraph;   // 加入一个段落

end;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值