question

BCK18 avatar image
0 Votes"
BCK18 asked Ezreal95-7594 edited

Read Microsoft word document - VB.NET

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,

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AddWebSolution-0525 avatar image
0 Votes"
AddWebSolution-0525 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ezreal95-7594 avatar image
0 Votes"
Ezreal95-7594 answered Ezreal95-7594 edited

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.