Sona Shetty
Sona Shetty

Reputation: 1047

Parsing full content of Word document with Excel VBA

I want to write a generic method to extract everything from a word document to text file by looping through paragraph and shapes.

I am able to parse 90% of the document using the code below. However this code is not reading contents from few tables.

Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = False
Set objWordDoc = objWordApp.Documents.Open(strWordDocPath)

Set objFso = CreateObject("Scripting.FileSystemObject")
Set oFile = objFso.createTextFile(strTextFilePath)
Set colParagraphs = objWordDoc.Paragraphs


For Each objParagraph In colParagraphs
    lineText =Trim(objParagraph.Range.Text)
    If lineText <> "" Then
       oFile.Write lineText & vbCrLf
    end if
next

I am unable to extract few texts in a table from MS word document. The issue is only with a few texts and tables and I am able to read most contents from the document using my code.

Word document is present in the below link -

https://drive.google.com/file/d/0B1C7jj9dLG2aTXJNRGt6QTBVUUE/view?usp=sharing

The main issue is with parsing first table in the document. This document is generated by an application and I do not have any control on the formatting of the contents.

Can some one help me to read the complete contents from the attached document?

Upvotes: 0

Views: 11832

Answers (1)

Winterknell
Winterknell

Reputation: 585

Here's a lead for you:

Sub test()
    Dim tCel As Cell, cellText As String

    For Each tCel In ActiveDocument.Shapes(1).TextFrame.TextRange.Tables(1).Range.Cells
        cellText = Trim(tCel.Range.Text)
        Debug.Print cellText
    Next
End Sub

Upvotes: 1

Related Questions