Haru Rosyadi Haru Rosyadi - 1 year ago 110
Vb.net Question

How to get text from word document into a string using vb?

can anyone teach me how to get text from Microsoft Word using vb code?

Answer Source

Here's a tested example of a simple function to return the text from a word document given its filepath, hope this helps:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim docfilepath As String = "C:\Users\sooho\Desktop\test.docx"
    Dim doctext As String = getDocText(docfilepath)
    If doctext IsNot Nothing Then MsgBox(doctext)
End Sub
Private Function getDocText(ByVal filepath As String) As String
    If File.Exists(filepath) AndAlso Path.GetExtension(filepath).ToUpper.Equals(".DOCX") Then
        Dim app As Application = New Application
        Dim doc As Document = app.Documents.Open(filepath)
        Dim doctxt As String = doc.Content.Text
        Return doctxt
        Return Nothing
    End If
End Function

EDIT: forgot to mention, you will need to add Microsoft.Office.Interop.Word framework as a reference in your project's settings, this is required to interact with any word document unless you're using a third-party library. Also, not shown, "Imports Microsoft.Office.Interop.Word" and "Imports System.IO" are needed at the beginning of your code for this example to compile.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download