locked
how To read Text from Word Document RRS feed

  • Question

  • this code  work correctly

    
    Dim word As New Microsoft.Office.Interop.Word.Application()
    
     Dim miss As Object = System.Reflection.Missing.Value
    
     Dim path As Object = "C:\x2.docx"
    
     Dim [readOnly] As Object = True
    
     Dim docs As Microsoft.Office.Interop.Word.Document = word.Documents.Open(path, miss, [readOnly], miss, miss, miss, _
    
    miss, miss, miss, miss, miss, miss, _
    
    miss, miss, miss, miss)
    
     Dim totaltext As String = ""
    
     For i As Integer = 0 To docs.Paragraphs.Count - 1
    
     totaltext += " " & vbCr & vbLf & " " + docs.Paragraphs(i + 1).Range.Text.ToString()
    
     Next
    
    RichTextBox1.Text = totaltext
    
    docs.Close()
    
    word.Quit()
    

    but I want  call this code from openFileDialog

    like this

      Dim myStream As object = Nothing
            Dim openFileDialog1 As New OpenFileDialog()
    
            openFileDialog1.InitialDirectory = "c:\"
            openFileDialog1.Filter = "docx files (*.docx)|*.docx|All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True
    
            If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Try
                    myStream = openFileDialog1.OpenFile()
                    If (myStream IsNot Nothing) Then
    
    
                        Dim word As New Microsoft.Office.Interop.Word.Application()
                        Dim miss As Object = System.Reflection.Missing.Value
                        'myStream = openFileDialog1.FileName
                        '' Dim path As Object = "C:\x2.docx"
                        Dim [readOnly] As Object = True
                        Dim docs As Microsoft.Office.Interop.Word.Document = word.Documents.Open(myStream, miss, [readOnly], miss, miss, miss, _
                            miss, miss, miss, miss, miss, miss, _
                            miss, miss, miss, miss)
                        Dim totaltext As String = ""
                        For i As Integer = 0 To docs.Paragraphs.Count - 1
                            totaltext += " " & vbCr & vbLf & " " + docs.Paragraphs(i + 1).Range.Text.ToString()
                        Next
                        RichTextBox1.Text = totaltext
                        docs.Close()
                        word.Quit()
    
                    End If
                Catch Ex As Exception
                    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
                Finally
                    ' Check this again, since we need to make sure we didn't throw an exception on open.
                    If (myStream IsNot Nothing) Then
                        myStream.Close()
                    End If
                End Try
            End If
    this message well appear when I call word doc


    Saturday, December 19, 2015 8:06 AM

Answers

  • Shared  Sub Main()
     ' Open a doc file.
     Dim application As Application =  New Application()
     Dim document As Document =  application.Documents.Open("C:\\word.doc")
     
     ' Loop through all words in the document.
     Dim count As Integer =  document.Words.Count
     Dim i As Integer
     For  i = 1 To  count Step  i + 1
         ' Write the word.
         Dim text As String =  document.Words(i).Text
         Console.WriteLine("Word {0} = {1}", i, text)
     Next
     ' Close word.
     application.Quit()
     End Sub
    • Marked as answer by KhaLeeL ZoURoB Tuesday, December 22, 2015 6:30 AM
    Tuesday, December 22, 2015 6:30 AM

All replies

  • but I want  call this code from openFileDialog like this

    Get rid of the Try/Catch.   Then you will see the error at the line at which it occurs, and when execution stops at that line you will be able to examine the type of the variables involved. Then you can adjust the declarations for those variables so that the types match.

    You have added a stream to the original code.  It is not required because you are not reading the file to get the text.  You are creating a WORD document object and accessing the properties of that object in order to get the text. The Open method of the Documents class takes a path, not a stream, as the first argument.  See:
    https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.open.aspx

    Saturday, December 19, 2015 11:23 AM
  • Shared  Sub Main()
     ' Open a doc file.
     Dim application As Application =  New Application()
     Dim document As Document =  application.Documents.Open("C:\\word.doc")
     
     ' Loop through all words in the document.
     Dim count As Integer =  document.Words.Count
     Dim i As Integer
     For  i = 1 To  count Step  i + 1
         ' Write the word.
         Dim text As String =  document.Words(i).Text
         Console.WriteLine("Word {0} = {1}", i, text)
     Next
     ' Close word.
     application.Quit()
     End Sub
    • Marked as answer by KhaLeeL ZoURoB Tuesday, December 22, 2015 6:30 AM
    Tuesday, December 22, 2015 6:30 AM