ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   retrieve text from table in .doc file (https://www.excelbanter.com/excel-programming/346306-retrieve-text-table-doc-file.html)

Kevin

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

Kevin

retrieve text from table in .doc file
 
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


"Kevin" wrote:

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



All times are GMT +1. The time now is 08:03 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com