retrieve text from table in .doc file

K

Kevin

I've developed code open an existing .doc file and then retrieve text from a
table in the header and text from a table in the body of the document. The
..doc file has a 2 x 3 table in the header with text in cell (1, 1). The body
of the document includes a 5 x 2 table with text in cell(1, 1) and cell(2,
2). The code pulls the text from the table in the body ok. But it fails to
retrieve the text in the header.

My code looks like,


Sub ListFileContents()
Dim strSourceFolder As String, strSubFolder As String
Dim strFilename As String, strPathAndFilename As String
Dim WordApp As Object, WordDoc As Object
Dim STR1 As String
Dim EndPos1 As Long
Dim strText1 As String, strText2 As String

On Error Resume Next

strSourceFolder = "C:\testFolder\"
strSubFolder = "testSubfolder\"
strFilename = "test.doc"

strPathAndFilename = strSourceFolder & strSubFolder & strFilename

Set WordApp = CreateObject("Word.Application")

Set WordDoc = WordApp.Documents.Open(strPathAndFilename)

WordApp.Visible = False

STR1 = WordDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range _
.Tables(1).Rows(1).Cells(1).Range.Text

Worksheets("Sheet1").Cells(1, 1).Value = STR1

With WordDoc.Tables(1)
STR1 = .Cell(1, 1).Range.Text
EndPos1 = Len(STR1) - 2
Worksheets("Sheet1").Cells(2, 1).Value = Mid$(STR1, 1, EndPos1)

STR1 = .Cell(2, 2).Range.Text
EndPos1 = Len(STR1) - 2
Worksheets("Sheet1").Cells(3, 1).Value = Mid$(STR1, 1, EndPos1)
End With

WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub


Can anyone tell me what's wrong with this code?

Thanks much.

-Kevin
 
K

Kevin

The problem was an unselected reference. I'd been working on the code at
home and then took it to work. The reference for home use is Word 9.0 Object
Library. At work it's Word 10.0 Object Library.

No problem after selecting the proper reference.

Sorry for bogarting the bandwidth.


-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top