插入Word表行以评估数据库表行

时间:2014-10-16 20:53:47

标签: ms-word access-vba ms-access-2010

我有一个包含字段

的访问表
ID
Field1
Field2
Field3

我还有一个带标题的单词表

ID | Field1 | Field2 | Field3

如何从表格中自动将表格中的所有数据导入评估数据库?

1 个答案:

答案 0 :(得分:1)

自动导入并不容易,但您可以通过编程方式进行。

从Access,您可以执行以下操作:

dim base as string: base = "INSERT INTO tblname (ID, Field1, Field2, Field3) VALUES ("
dim sql as string
dim ii as long
dim jj as long
dim wrd as object
dim wrdDoc as object
'
docmd.setwarnings false
set wrd = createObject("Word.Application")
wrd.visible = false
set wrdDoc = wrd.Documents.Add("name of word document containing table")
with wrdDoc.Tables(1)   'assuming first table in document
    for ii = ? to .Rows.Count      'if the table has column headings, ? = 2, else 1
        sql = base
        for jj = 1 to 4            '4 = count of columns
            sql = sql & iif(jj = 1, "", ",") & CStr(.Cell(ii, jj))
        next jj
        sql = sql & ")"
        docmd.runsql sql
    next ii
end with
docmd.setwarnings true
wrd.Quit
set wrddoc = nothing
set wrd = nothing