Importing from MS word table

Ammarhm

Beginner User
Local time
Yesterday, 17:03
Joined
Jul 3, 2008
Messages
80
Hi
I am trying to import text from different parts of several (1000) MS word documents. Most of the text is in Form Fields and is therefore easy to impost
However, one part of the text is inserted into a table in the word document, you can see the attached file to see how the table looks like. This table could be at page 5 or 20 of the document, but the header is always "Event". so the question is: how do you import the text from the row following the header event? I can open the connection to the word file, but exactly how to refer to the text in the table is tricky, only the header is the reference I can link to, but how?
The other question: If I want to attach/insert the whole MS word file to a cell in MS access, is this possible and how?
Best Regards
Ammarhm
 

Attachments

This seems to work:

Code:
Public Sub ImportEvents()

Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim tbl As Word.Table

Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("C:\temp\ms word file.doc")

For Each tbl In WordDoc.Tables
    If CellText(tbl.cell(1, 1)) = "Event" Then
        For r = 2 To tbl.Rows.Count
        
            Debug.Print "Row: "; r, "text: "; CellText(tbl.cell(r, 1))
            
            'add some code here to put import the cell value as required.
            
        Next r
    End If

Next tbl

WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing


End Sub

Private Function CellText(cell As Word.cell) As String
    CellText = Trim(Replace(cell.Range.Text, vbCr & Chr(7), ""))
End Function


Note the solution in the link below is suggested as being much faster if you are extracting multiple cells from a table.

http://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx

Also note that my solution assumes there is only one column. It can easily be adapted to read multi-columns.

hth
Chris
 
Last edited:
This seems to work:

Code:
Public Sub ImportEvents()

Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim tbl As Word.Table

Set WordApp = New Word.Application
Set WordDoc = WordApp.Documents.Open("C:\temp\ms word file.doc")

For Each tbl In WordDoc.Tables
    If CellText(tbl.cell(1, 1)) = "Event" Then
        For r = 2 To tbl.Rows.Count
        
            Debug.Print "Row: "; r, "text: "; CellText(tbl.cell(r, 1))
            
            'add some code here to put import the cell value as required.
            
        Next r
    End If

Next tbl

WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing


End Sub

Private Function CellText(cell As Word.cell) As String
    CellText = Trim(Replace(cell.Range.Text, vbCr & Chr(7), ""))
End Function
Note the solution in the link below is suggested as being much faster if you are extracting multiple cells from a table.

http://msdn.microsoft.com/en-us/library/aa537149(v=office.11).aspx

Also note that my solution assumes there is only one column. It can easily be adapted to read multi-columns.

hth
Chris
Hi
I really appreciate your answer, works like a charm for importing the table content, much obliged
I wonder if you have an answer to the second question, ie linking the same word file to a record (theoretically like a hyperlink) or just loading up all the word file into a cell like an attachement?
BEst regards
 

Users who are viewing this thread

Back
Top Bottom