Home » excel » vb.net – .NET read word/excel documents without Microsoft.Office.Interop

vb.net – .NET read word/excel documents without Microsoft.Office.Interop

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to open a doc file under VB.NET, I found a very simple way to do it using word:

For example:

    Dim doc As Word.Document
    Dim wordApp As New Word.Application

    Dim allText As String
    Try
        doc = wordApp.Documents.Open("C:\marti.doc")
        allText = doc.Range.Text()
        doc.Close()
        RichTextBox1.Text = allText
    Catch
        'error            
    End Try

(More detailed info: http://support.microsoft.com/kb/316383)

This could work, but it need to open Microsoft Word window to handle it. I need to use it without Word installed. So I need a Library which can open doc/excel files.

Do you know a good Library that can do this?

I found this library:
http://bytescout.com/download/trial/documentsdk.html

Have you tried this?

How to&Answers:

The recommended way of interacting with Office files is through the Office OpenXML library. You can get it here.

If you are using Microsoft Office 2003, you can load and save documents in Open XML if you have installed the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats. You can download the compatibility pack and find more info here.

This is an example method to gather the content of the document:

Private Shared Function GetWordDocContent(strDoc As String) As String
    Dim stream As Stream = File.Open(strDoc, FileMode.Open)
    Dim wordprocessingDocument__1 As WordprocessingDocument = WordprocessingDocument.Open(stream, True)
    Dim body As Body = wordprocessingDocument__1.MainDocumentPart.Document.Body
    Dim content As String = body.InnerText
    wordprocessingDocument__1.Close()
    Return content    
End Function

Another example here.

Answer:

for handling docx file you can try NPOI or DocX

for the old word format (.doc) I’ve tried Syncfusion DocIO components, but i prefer to use word automation with NetOffice, much better than the interop