Many times we find ourselves moving from one format to another in the office space and sometimes moving data from one format to another can be a real pain in the butt. Automating these processes is always handy and let’s be honest. We all love it when things are just… easier. The key many times is to understand both formats and how they can work together to bring your data from one medium to another.
Recently I found myself in a dilemma where my Report in MS Word was just not enough and I needed to aggregate a lot of the data from many reports… Well word is good for displaying data, but not so much for storing and managing it. So my solution: move it to MS Access. Then I started trying to figure out how to do that, very little luck in finding good documentation on this. So after not really finding a satisfactory answer… I thought I would share what I am using to import information from Word tables to Access.
Required References for this tutorial:
The first issue is to understand how MS Word labels and identifies tables. MS word will auto identify any table and label according to its order on the document. Therefore the tables are listed as Tables(1) - Tables(i), “i” being the total number of tables you have. To reference these tables from MS Access a few steps need to be taken.
First we must identify and access our word document:
Next we must open MS Word and declare our doc name:
Now we can reference tables on our word document with the following code:
You will notice in the code above we don’t use row and column but Cell(x,y). This is much easier to visualize as well as coding.
A few closing statements are needed to make sure we clear up our code:
This is our word table:
This is our database table:
And Finally, The full code to import the People table from People.docx to our database table tblPeople.
This is can modified and expanded in many ways, but in its simplest form that’s all there is too it.
NOTE: Experts! Please let me know what I can do to make this small article any better. (It is my first one)
Recently I found myself in a dilemma where my Report in MS Word was just not enough and I needed to aggregate a lot of the data from many reports… Well word is good for displaying data, but not so much for storing and managing it. So my solution: move it to MS Access. Then I started trying to figure out how to do that, very little luck in finding good documentation on this. So after not really finding a satisfactory answer… I thought I would share what I am using to import information from Word tables to Access.
Required References for this tutorial:
Code:
Microsoft Access Object Library Microsoft Office Object Library Microsoft Word Object Library
First we must identify and access our word document:
Code:
Dim appWord As Word.Application [I]'declare MS Word application[/I] Dim doc As Word.Document [I]'declare an instance of a document in MS Word[/I] Dim strDoc as String [I]'we will use this to pass a document name Word to later[/I] 'This can also be coded as Dim appword As Word.Application, doc As Word.Document, strDoc as String
Code:
strDoc = CurrentProject.Path & "\file.docx" [I]'declare document name[/I]
Set appWord = CreateObject("Word.Application") [I]'open MS Word[/I]
Set doc = appWord.Documents.Open(strDoc) [I]'open the document that is declared in strDoc[/I]
Code:
doc.Tables(1).Cell(1, 2).Range.Text [I]'this points us to table 1, row 1, column 2[/I]
A few closing statements are needed to make sure we clear up our code:
Code:
doc.Close: Set doc = Nothing [I]'this closes our word document and clears up the document selection [/I] appWord.Quit: Set appWord = Nothing [I]'this closes our instance of MS Word[/I]
Now let’s see this in action for a simple personnel table.
This is our word table:
Code:
People (Table 1) ======================== [U]| First Name | Last Name | SSN | Phone Number | Gender |[/U] | Data | Data | Data| Data | Data | | Data | Data | Data| Data | Data | | Data | Data | Data| Data | Data | | Data | Data | Data| Data | Data |
Code:
tblPeople ======================== [B][U]Field Name Data Type[/U][/B] FName Text LName Text SSN Text [B](PK)[/B] PhoneNumber Text Gender Text
Code:
Private Sub cmdImport_Click()
Dim appWord As Word.Application, doc As Word.Document
Dim dbs As DAO.Database, rst As DAO.Recordset, strDoc As String
Set appWord = CreateObject("Word.Application") [I]'establish an instance of word[/I]
strDoc = CurrentProject.Path & "\People.docx" [I]'set string to document path and file[/I]
Set doc = appWord.Documents.Open(strDoc) [I]'establish the document[/I]
Set dbs = CurrentDb [I]'establish the database to use (this is our current Database)[/I]
Set rst = dbs.OpenRecordset("tblPeople") [I]'establish the recordset[/I]
With doc.Tables(1) [I]'target table 1 in People.docx[/I]
For i = 2 To .Rows.Count [I]'cycle through rows in Tables(1) [we skip the first row because the table has headers][/I]
With rst
.AddNew [I]'creating a new record[/I]
![FName] = doc.Tables(1).Cell(i, 1).Range.Text
![LName] = doc.Tables(1).Cell(i, 2).Range.Text
![SSN] = doc.Tables(1).Cell(i, 3).Range.Text
![PhoneNumber] = doc.Tables(1).Cell(i, 5).Range.Text
![Gender] = doc.Tables(1).Cell(i, 6).Range.Text
.Update [I]'update the whole record[/I]
End With
Next [I]'go to next row in Tables(1)[/I]
End With
rst.Close: Set rst = Nothing [I]'close and clear recordset[/I]
db.Close: Set rst = Nothing [I]'close and clear database[/I]
doc.Close: Set doc = Nothing [I]'close and clear document[/I]
appWord.Quit: Set appWord = Nothing [I]'close and clear MS Word[/I]
End Sub
This is can modified and expanded in many ways, but in its simplest form that’s all there is too it.
NOTE: Experts! Please let me know what I can do to make this small article any better. (It is my first one)
Comment