Import Data from a Word Table to Access

MarionD

Registered User.
Local time
Today, 04:05
Joined
Oct 10, 2000
Messages
425
Hi All,

Is it possible to import Data from a Word Table into an Access Table?

I've been trying for hours now and I just can't find a way!

Thanks
Marion
 
Thanks for the Info Pat. Will search the Word Help.
 
Here is what you need to know conceptually to attack your problem. This should not be used IN PLACE of Word Help. Just read over this and look up the help for anything you are not sure about.

First, you want to open the .DOC file by creating a Word Application. This is a CreateObject call for the "Word.Application" and you have to store the result in a variable of type Object.

Open your document and make it the active document. To do this, you need the collection called Documents in Word. You can open a document by using the Object.Documents.Open method on the application object. This adds the opened document to the Application's collection of documents. You then use the .Activate method (with the file spec as the argument to .Activate) to make that document the ActiveDocument. ActiveDocument is a special prefix that lets you bypass the syntax

Object.Documents(n)

where 'n' is the document number.

OK, once it is open, you need to know these facts: An active document has a collection of tables called (duh!) Tables. The first table in the collection is Tables(1). The last table in the collection is ActiveDocument.Tables(ActiveDocument.Tables.Count).

For EACH TABLE, you have a collection of Rows numbered from ActiveDocument.Tables(n).Rows(1) to ...Rows(...Rows.Count). (I am not repeating the qualifier prefixes but they are there.)

For EACH ROW, you have a collection of Cells numbered from ActiveDocument.Tables(n).Rows(m).Cells(1) to ...Cells(...Cells.Count). Again I removed the prefixes. You need to realize that a cell might contain other things than text. To get the text from a cell, you need to know the path is .Range.Text because you could have a sub-range selected, so you need the UNQUALIFIED Range property when you mean the whole thing. And you need the .Text to distinguish it from other attributes of the cell.

Now, the quick way out of this is to define a Word.Table variable and do a

PHP:
Set wtTbl = ActiveDocument.Tables(n)

where the 'n' is the table number. That shortens the prefix. And/Or, you can define a Word.Row and then use it as

PHP:
Set wrRow = wtTbl.Rows(m)

Remember to qualify your data types by using the Word prefix in the Dim statements. Word.Row, Word.Table, Word.Cell, etc. You will, of course, need references to the Word Object Library so that means assuring you have the appropriate References file checked in the Tools>>References path from module screen.

When all of this is done, getting a reference from out of a particular word table might be as easy as

stA = ActiveDocument.Tables(1).Rows(2).Cells(3).Range.Text

By the way, this works the other way, too. There is ALSO a collection called Columns that maps at a right angle to the way Rows are mapped. The Columns each have collections of Cells to represent the Rows. So you could have done the same thing with

stA = ActiveDocument.Tables(1).Columns(3).Cells(2).Range.Text

Either one of these stores the text from the entire contents of third column of the second row of the first table of the active document into string stA.

In the Word help, particularly the VBA part, read up on these things before you actually use them. Once you get the components figured out, extracting text from a Word Table is easy. Now getting the formatting data? That is harder because the formatting model is a LOT uglier. Don't ask. You don't want to know.
 

Users who are viewing this thread

Back
Top Bottom