使用VBA将MS Word表导入MS Access表

很多时候,我们发现自己在办公空间中从一种格式转换为另一种格式,有时将数据从一种格式转换为另一种格式可能会带来极大的痛苦。 自动化这些过程总是很方便的,老实说。 当事情变得……容易时,我们所有人都喜欢它。 很多时候,关键是要了解两种格式以及它们如何协同工作以将您的数据从一种介质传递到另一种介质。

最近,我陷入了一个两难的境地,即我在MS Word中的报告还不够,我需要从许多报告中汇总很多数据……好吧,Word可以很好地显示数据,但对于存储和管理数据却不是那么有用。 所以我的解决方案是:将其移至MS Access。 然后,我开始尝试弄清楚该如何做,很少能为此找到好的文档。 因此,在未真正找到满意的答案之后……我想我将分享我用来将信息从Word表导入Access的内容。

本教程的必需参考:

Microsoft Access Object Library
Microsoft Office Object Library
Microsoft Word Object Library
第一个问题是了解MS Word如何标记和识别表。 MS word将根据其在文档上的顺序自动识别任何表格和标签。 因此,表被列为表(1)-表(i),“ i”是您拥有的表总数。 要从MS Access引用这些表,需要采取一些步骤。 首先,我们必须识别并访问我们的Word文档:
Dim appWord As Word.Application 'declare MS Word application 
Dim doc As Word.Document 'declare an instance of a document in MS Word 
Dim strDoc as String 'we will use this to pass a document name Word to later  
'This can also be coded as 
Dim appword As Word.Application, doc As Word.Document, strDoc as String
接下来,我们必须打开MS Word并声明我们的文档名称:
strDoc = CurrentProject.Path & "\file.docx"  'declare document name 
Set appWord = CreateObject("Word.Application") 'open MS Word 
Set doc = appWord.Documents.Open(strDoc) 'open the document that is declared in strDoc 
现在,我们可以使用以下代码在Word文档中引用表:
doc.Tables(1).Cell(1, 2).Range.Text 'this points us to table 1, row 1, column 2 
您会在上面的代码中注意到,我们不使用行和列,而是使用Cell(x,y) 。 这比编码和可视化都容易得多。 需要一些结束语以确保我们清除代码:
doc.Close: Set doc = Nothing 'this closes our word document and clears up the document selection  
appWord.Quit: Set appWord = Nothing 'this closes our instance of MS Word 
现在,让我们来看一下用于简单人员表的操作。
这是我们的单词表:
People (Table 1)
======================== | First Name | Last Name | SSN | Phone Number | Gender | 
| Data       | Data      | Data| Data         | Data   |
| Data       | Data      | Data| Data         | Data   |
| Data       | Data      | Data| Data         | Data   |
| Data       | Data      | Data| Data         | Data   |
这是我们的数据库表:
tblPeople
======================== Field Name    Data Type 
FName         Text
LName         Text
SSN           Text (PK) 
PhoneNumber   Text
Gender        Text
最后,是将People表从People.docx导入到我们的数据库tblPeople的完整代码。
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") 'establish an instance of word 
    strDoc = CurrentProject.Path & "\People.docx"  'set string to document path and file 
    Set doc = appWord.Documents.Open(strDoc) 'establish the document  
    Set dbs = CurrentDb 'establish the database to use (this is our current Database) 
    Set rst = dbs.OpenRecordset("tblPeople") 'establish the recordset  
    With doc.Tables(1) 'target table 1 in People.docx  
        For i = 2 To .Rows.Count 'cycle through rows in Tables(1) [we skip the first row because the table has headers]  
            With rst
                .AddNew 'creating a new record 
                    ![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 'update the whole record 
            End With 
        Next 'go to next row in Tables(1)  
    End With 
    rst.Close: Set rst = Nothing 'close and clear recordset 
    db.Close: Set rst = Nothing 'close and clear database 
    doc.Close: Set doc = Nothing 'close and clear document 
    appWord.Quit: Set appWord = Nothing 'close and clear MS Word  
End Sub

可以通过多种方式来修改和扩展它,但是以最简单的形式就可以了。

注意:专家! 请让我知道如何做才能使这篇小文章更好。 (这是我的第一个)

From: https://bytes.com/topic/access/insights/961343-importing-ms-word-tables-into-ms-access-tables-using-vba

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值