VB.net:
Public Class WordClass
Public Function wirteWord(ByVal str As String, ByVal title As String) As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
'取得Word文件保存路径
Dim filename As Object = "C:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
'创建一个名为WordApp的组件对象
WordApp = New Word.ApplicationClass
'创建一个名为WordDoc的文档对象
WordDoc = WordApp.Documents.Add()
'在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text = str
'保存
WordDoc.SaveAs(filename)
'关闭WordDoc文档对象
WordDoc.Close()
'关闭WordApp组件对象
WordApp.Quit()
Return True
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return False
End Try
End Function
Public Function readWord(ByVal title As String) As String
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
Dim comment As String = WordDoc.Range.Text
WordDoc.Close()
WordApp.Quit()
Return comment
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return Nothing
End Try
End Function
Public Function printWord(ByVal title As String) As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
'WordApp.ActivePrinter = WordDoc.Range.Text
'WordDoc.Close()
'WordApp.Quit()
Return True
Catch ex As Exception
'If Not WordDoc Is Nothing Then
'WordDoc.Close()
'End If
'If Not WordApp Is Nothing Then
'WordApp.Quit()
'End If
'Return Nothing
End Try
Return False
End Function
Public Function printSetWord(ByVal title As String)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
WordApp.PrintPreview = True
'WordDoc.Close()
'WordApp.Quit()
Catch ex As Exception
'If Not WordDoc Is Nothing Then
'WordDoc.Close()
'End If
'If Not WordApp Is Nothing Then
'WordApp.Quit()
'End If
'Return Nothing
End Try
End Function
Public Function viewWord(ByVal title As String)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
WordApp.PrintPreview = True
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return Nothing
End Try
End Function
End Class