BradleyOng83
Member
- Joined
- Mar 2, 2006
- Messages
- 19
- Programming Experience
- Beginner
First, I create a new project within Visual Studio .NET and add a reference to the Microsoft Word type library. I am using Microsoft Word 2003, so the type library is Microsoft Word 11.0 Object Library. Once the reference is added, I can use the Word objects in the code.
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String
Dim word As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
Try
doc = word.Documents.Open("c:\test.doc")
doc.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
End Class (this is use for open word only so how to put inside textbox?)
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Public Class Form1 Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String
Dim word As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
Try
doc = word.Documents.Open("c:\test.doc")
doc.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
End Class (this is use for open word only so how to put inside textbox?)
- The System.Runtime.InteropServices namespace is imported to work with COM and .NET interoperability. It contains the COMException class.
- The .NET wrapper for the Word objects is contained in the Microsoft.Office.Interop.Word namespace (the reference previously added to the project).
- The Application class within the Word namespace is used to access the Word application.
- The Document class within the Word namespace allows you to work with Word documents.
- The Open method contained within the Documents property of the application allows an existing document to be loaded. It contains a Close method as well.
- The Activate method of the Document class opens the document within a new instance of Word.
- The code accessing the Word document is contained within a Try/Catch block. It catches COM errors via the COMException class.
- The MessageBox.Show method replaces the Office VBA MsgBox function.