Read Microsoft word document - VB.NET

B_C_K 1 Reputation point
2021-06-10T05:47:37.957+00:00

I am developing an application that needs to read Microsoft word document. Document consists of numbered paragraphs like 1,2,3,4.....etc. I want to read paragraph by paragraph and load into rtf box or similar. Is there an API or someone can explain how to do it is highly appreciated.

Many Thanks,

Developer technologies | VB
0 comments No comments

3 answers

Sort by: Most helpful
  1. Ezreal95 1 Reputation point
    2022-01-24T03:49:42.357+00:00

    Try Free Spire.Doc. Install the DLL files in your project, through NuGet, and you can read a paragraph using the code snippet like below.

    Imports System.Windows.Forms
    Imports Spire.Doc
    Imports Spire.Doc.Documents
    
    Namespace ExtractParagraphToRichTextbox
        Public partial Class Form1
      Inherits Form
            Public  Sub New()
                InitializeComponent()
            End Sub
    
            Private  Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
                'Load word file
                Document doc  =  New Document()
                doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.docx")
                'Get the specific paragraph
                Dim paragraph As Paragraph =  doc.Sections(0).Paragraphs(0) 
                'Get text
                Me.richTextBox1.Text = paragraph.Text
            End Sub
        End Class
    End Namespace
    

    Was this answer helpful?

    0 comments No comments

  2. AddWebSolution 171 Reputation points
    2021-06-10T12:16:00.467+00:00

    If you want to read a word document and display doc content in richtextbox
    have a look on this references given below:-

    Open-ms-word-doc-in-richtextbox
    63192

    Imports Word = Microsoft.Office.Interop.Word
    
    Dim sFileName As String = "fullfilename"
    'open Word app
    Dim wdApp As Word.Application = New Word.Application()
    'open document
    Dim wdDoc As Word.Document = wdApp.Documents.Open(sFileName)
    'read paragraphs using Linq
    Dim lines As String() = wdDoc.Paragraphs.Cast(Of Word.Paragraph) _
            .Select(Function(x) x.Range.Text.Trim()).ToArray()
    'add lines
    RichTextBox1.Lines = lines
    'close the document
    wdDoc.Close(SaveChanges:=False)
    wdApp.Quit()
    

    Was this answer helpful?

    0 comments No comments

  3. Castorix31 91,976 Reputation points
    2021-06-10T07:36:44.173+00:00

    You can use Word Interop

    A test, displaying the text in the Console (replace it by a RichTextBox or other to display it) =>

    (Add reference to Microsoft.Office.Interop.Word)

                Dim oWordApplication As Word.Application = New Word.Application()
                Dim sDocFilePath As Object = "e:\\test.doc"
                Dim oWordDocument As Microsoft.Office.Interop.Word.Document = oWordApplication.Documents.Open(sDocFilePath)
                For Each oWordPar As Microsoft.Office.Interop.Word.Paragraph In oWordDocument.Paragraphs
                    Dim oWordRange As Microsoft.Office.Interop.Word.Range = oWordPar.Range
                    Dim sText As String = oWordRange.Text
                    Dim sList As String = oWordRange.ListFormat.ListString
                    'Dim nListLevelNumber As Integer = oWordRange.ListFormat.ListLevelNumber
                    If (sList <> "") Then
                        Console.WriteLine("List : {0} - Text : {1}", sList, sText)
                    Else
                        Console.WriteLine("Text : {0}", sText)
                    End If
                Next
                oWordDocument.Close()
                oWordApplication.Quit()
                Marshal.ReleaseComObject(oWordApplication)
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.