how open word input to textbox (vb.net)

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?)

  • 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.
 
Are you thinking about pure text or formatted text?
 
Im not entirely sure what you are asking :-S
 
sorry to interrupt u guys!!!!

Is there a way to read/write word documents in VB.net char by char???

Any help would be highly appreciated!!! thanks!!!
 
The pure text is available by using Document object Select method, after that you access it from Text property of Selection object.

To get the rich text (formatted text) to a local RichTextBox you have to either
- use the SaveAs method to save the document to a rtf file, this file can be loaded by the RichTextBox,
- other method is to select all text and put it into clipboard with the Copy method, followed by the Paste method of RichTextBox.

This was an overview of how to access and transfer these two text formats from Word to own application. The object and properties reference for automation is available here: http://msdn.microsoft.com/library/en-us/vbawd11/html/WordVBAWelcome_HV01135786.asp
 
Thanks JohnH!!!!

can u direct me to an article or a tutorial!!! i am a newbie!!!!

i am coding an encryptor in vb.net that supports copy/paste text encryption, encrypts *.txt files, *.doc,*.pdf!!!!

curreltly i am struck with the last two *.doc and *.pdf!!!!

any good articles on this???
 
hey Amit Sachdev, except for the reference in previous post where you can check up on all the objects and properties to use (many examples there too, that they write in VBA language should not be a problem to understand), you need to know how to connect to the Word automation tools, this MS article show the two methods (it's the same for all MS Office products): http://support.microsoft.com/kb/304661/ Use the Early Binding method is my advice.
 
*select word files*

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)
 
Back
Top