Results 1 to 8 of 8

Thread: how open word input to textbox (vb.net)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    how open word input to textbox (vb.net)

    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.

  2. #2
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: how open word input to textbox (vb.net)

    #Region "Spell Check"

    Public Sub SpellCheck(ByRef txtTextBox As System.Windows.Forms.TextBox)
    Try
    If txtTextBox.Text.Length > 0 Then

    'Modified by Burt Gardner
    'Replace the following before sending to Microsoft Word, because
    'we dont know where the text came from.

    'replace Carriage Return
    txtTextBox.Text = txtTextBox.Text.Replace(Chr(13).ToString, vbCrLf.ToString)

    '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.

    'replace Carriage Return
    txtTextBox.Text = txtTextBox.Text.Replace(Chr(13).ToString, vbCrLf.ToString)

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

    ' Close the Document, not saving changes.
    doc.Close(SaveChanges:=False)

    ' Close the Word server.
    word_server.Quit()
    End If

    Catch ex As Exception
    'Error occurred notify user
    System.Windows.Forms.MessageBox.Show(ex.ToString)

    End Try

    End Sub

    #End Region
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Re: how open word input to textbox (vb.net)

    how can i open any word files?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Re: how open word input to textbox (vb.net)

    i mean like open dialog can select any word files

  5. #5
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: how open word input to textbox (vb.net)

    Please clarify :

    1. Do you want to know how to use open dialog to select a word file ?

    2. Do you want to open the word file with the word server ?
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Re: how open word input to textbox (vb.net)

    how to use open dialog to select a word file ?

  7. #7
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: how open word input to textbox (vb.net)

    Heres how to use opendialog to select a word file.
    Attached Files Attached Files
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    19

    Re: how open word input to textbox (vb.net)

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width