现在的位置: 首页 > 综合 > 正文

Office自动化编程(1)

2013年10月22日 ⁄ 综合 ⁄ 共 1791字 ⁄ 字号 评论关闭

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;

 

抱歉!评论已关闭.