1,在project\references 中加入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當中
另見:http://jingyan.baidu.com/article/11c17a2c73196ef446e39d13.html