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.
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.
'replace Line Feed
txtTextBox.Text = txtTextBox.Text.Replace(Chr(10).ToString, vbLf.ToString)
'replace Tabs - Assume 5 spaces for each tab
txtTextBox.Text = txtTextBox.Text.Replace(Chr(9).ToString, " ".ToString)
' Make a Word server object.
Dim word_server As New Word.Application
' Hide the server.
word_server.Visible = False
' Make a Word Document.
Dim doc As Word.Document = _
word_server.Documents.Add()
Dim rng As Word.Range
' Make a Range to represent the Document.
rng = doc.Range()
' Copy the text into the Document.
rng.Text = txtTextBox.Text
' Activate the Document and call its CheckSpelling method.
doc.Activate()
doc.CheckSpelling()
' Copy the results back into the TextBox, trimming off trailing CR and LF characters.
Dim chars() As Char = {CType(vbCr, Char), _
CType(vbLf, Char)}
txtTextBox.Text = doc.Range().Text.Trim(chars)
'Modified by Burt Gardner
'Replace the following text after coming from Microsoft Word, because
'we dont want Word's special characters.
Imports System.Runtime.InteropServices
Imports Word
Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
Dim word As New Word.Application
Dim doc As Word.Document
'filter which files to open
Me.OpenFileDialog1.Filter = "Word Docs (*.doc)|*.doc|All files|*.*"
Me.OpenFileDialog1.FileName.ToString.Trim
doc = word.Documents.Open(Me.OpenFileDialog1.FileName.ToString.Trim)
doc.Activate()
End Sub
(it's need double click then can open, after popup one message. How to solve this problem without popup one message)