Office自动化编程(1)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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这个表示文本的对象,果然非常是“面向对象“

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航