環(huán)境:vb.net 2005,Word2003
需求:A、用vb.net打開(kāi)word,從自定義的模板生成WORD文檔。
B、把相應(yīng)字段替換成用戶指定的數(shù)據(jù)。
C、文檔關(guān)閉前,自動(dòng)讀取戶修改后固定位置的文本到vb.net中。
D、不管用戶是否選擇保存Word文檔,都自動(dòng)取得需要的文本。
E、用戶可以通過(guò)vb.net關(guān)閉Word,也可以手工關(guān)閉Word。
(1)選擇vb.net項(xiàng)目,右鍵屬性=》添加引用=》COM,添加Microsoft Word 11.0 Object Library。在程序中引用方法:Imports Microsoft.Office.Interop
(2)MyWord_Quit是關(guān)鍵,如果不在這里面釋放掉用Dim WithEvents定義的MyWord,當(dāng)用戶不在Vb.net中,而用手工直接關(guān)閉Word程序后,再次調(diào)用Word程序,就會(huì)產(chǎn)生“RPC 服務(wù)器不能用”錯(cuò)誤。
(3)程序代碼:
Imports Microsoft.Office.Interop
Public Class Form1
Dim WithEvents MyWord As Word.Application 'WithEvents '用于存放 Microsoft Word 引用的變量。
Dim MyDoc As Word.Document
Dim WordWasNotRunning As Boolean
Dim DocWasNotOpened As Boolean
Dim str1 As String
'-------------------------------------------------------------------------------------
'打開(kāi)Word程序,如果Word已經(jīng)運(yùn)行,就GetObject,否則就CreateObject
Private Sub Get_Word()
On Error GoTo Open_word '不帶第一個(gè)參數(shù)調(diào)用 Getobject 函數(shù)將
'返回對(duì)該應(yīng)用程序的實(shí)例的引用,如果該應(yīng)用程序不在運(yùn)行,則會(huì)產(chǎn)生錯(cuò)誤。
MyWord = GetObject(, "Word.Application")
GoTo Set_Visible
Open_word:
MyWord = CreateObject("Word.Application")
'如果發(fā)生錯(cuò)誤則要?jiǎng)?chuàng)建一個(gè)Word.Application對(duì)象
WordWasNotRunning = True '表明Word是由程序啟動(dòng)的,最后應(yīng)該關(guān)閉
Err.Clear()
Set_Visible:
MyWord.Visible = True
MyWord.Activate()
End Sub
'-------------------------------------------------------------------------------------
'從模板生成Word文檔
Public Sub NewDocWithModel(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
Dim isVisible As Boolean = False
Dim strName As String
strName = FileName
MyDoc = MyWord.Documents.Add(strName, missing, missing, isVisible)
DocWasNotOpened = True
MyDoc.Activate()
MyWord.Visible = True
End Sub
'-------------------------------------------------------------------------------------
Private Sub Get_Doc()
Dim MyTime As String
MyTime = Format(Now, "yyyy年M月d日h時(shí)m分")
'在此處對(duì)文件進(jìn)行操作。
MyDoc.Activate()
MyDoc.Select()
With MyWord.Selection.Find
.ClearFormatting()
'替換Word文檔模板中的“<姓名>和<性別>”為相應(yīng)值
'<姓名>是提前在模板是定義的,也可以用別的比較特別的字符代替
While .Execute("<姓名>") '被替換的內(nèi)容
MyWord.Selection.TypeText(Text:="張三") '替換后的內(nèi)容
End While
While .Execute("<性別>") '被替換的內(nèi)容
MyWord.Selection.TypeText(Text:="女") '替換后的內(nèi)容
End While
End With
End Sub
'-------------------------------------------------------------------------------------
'打開(kāi)Word,從模板生成Word文檔,替換文本
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Get_Word() '打開(kāi)Word程序
NewDocWithModel("D:/bai/vb.net/內(nèi)部明電批辦單.dot") '從模板生成文檔
Get_Doc() '替換相關(guān)文本
End Sub
'-------------------------------------------------------------------------------------
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MsgBox("WordWasNotrunning=" & WordWasNotRunning & ";DocWasNotOpened=" & DocWasNotOpened)
If WordWasNotRunning Then 'Word是vb.net打開(kāi)的,關(guān)閉它。
WordWasNotRunning = False
DocWasNotOpened = False
MyDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
MyWord.Quit()
Else 'Word以前就打開(kāi),只關(guān)閉vb.net打開(kāi)的Word文檔
If DocWasNotOpened = True Then
DocWasNotOpened = False
MyDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges) '(Word.WdSaveOptions.wdDoNotSaveChanges)
End If
End If
'釋放對(duì)象
MyDoc = Nothing
MyWord = Nothing
End Sub
'-------------------------------------------------------------------------------------
'顯示從Word中取得的數(shù)據(jù)
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = str1
End Sub
'-------------------------------------------------------------------------------------
'關(guān)閉前保存數(shù)據(jù)到變量
Private Sub MyWord_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles MyWord.DocumentBeforeClose
'保存Word文檔中,表格1的第3行第2列的文本,以后Vb.net調(diào)用
str1 = Doc.Tables(1).Rows(3).Cells(2).Range.Text
End Sub
'-------------------------------------------------------------------------------------
'Word退出后,執(zhí)行的操作
'如果用戶手工關(guān)閉Word,那么必須釋放MyDoc和MyWd,否則再次打開(kāi)Word就會(huì)出錯(cuò)
Private Sub MyWord_Quit() Handles MyWord.Quit
MyDoc = Nothing
MyWord = Nothing
WordWasNotRunning = False
DocWasNotOpened = False
End Sub
End Class