vb6中word编程总结

来源:互联网 发布:python excel 修改 编辑:程序博客网 时间:2024/05/16 19:53
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1,在projecteferences 中加入microsoft word 9.0 object library

2, 启动word    Dim wApp As word.Application    Set wApp = New word.Application    wApp.Visible = True   关闭word    wApp.Quit    Set wApp = Nothing

3, 打开文件    Set wDoc = Documents.Add  (新建)    ActiveDocument.SaveAs Text1.Text (保存)    Set wDoc = Documents.Open(FileName:=Text1.Text) (打开指定文件)

以上的Documents 和 ActiveDocument 均是word object 中的已实例化了的对象,即不用set obj=new obj即可以使用的对象.  就像vb中的app、debug、err等对象,文件打开之后,获取光标所在位置mySelection即可给文件添加各种数据(文本,图像,表格等等,)

4,插入文本

    Dim mySelection As word.Selection    Set mySelection = Documents.Application.Selection '注意上面的这两行代码,只要有这两行代码,就可以使用所有的word中的宏操作。以下的代码就是从宏中拷过来的。    With mySelection        .InsertAfter Text1.Text & vbCrLf        .Font.Name = "楷体_GB2312"        .Font.Size = 16        .ParagraphFormat.Alignment = 1    End With'这里有必要提到宏(macro)在word编程的重要性,几乎所有的word操作,只要你能够通过word可以实现,就可以编程实现

5,插入图像    Documents.Application.Selection.InlineShapes.AddPicture text1.text

6,插入表格因为excel中处理表格的能力要比word的处理能力要强,所以可以在excel中生成了表格之后再复制到word当中

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>