View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Kevin Kevin is offline
external usenet poster
 
Posts: 504
Default retrieve text from table in .doc file

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(wdHeaderFooterFirstPag e).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