确定表格在Word文档中的开始位置

时间:2017-07-03 16:32:50

标签: vba import ms-word access-vba

我每天都会制作一份特定的工作文档,其中包含一段文字,后跟一个包含大量客户数据的表格。我需要将该数据导入Access表。

我找到了代码,我将在下面包含这些代码。但是它没有按预期工作。相反,它根本不起作用。我预计这是因为doc这个词不是以表格开头,而是以文字开头。

所以我有两个选择。 1)找到一种格式化每个文档的方法,使其只包含表格(我必须自动执行此操作,因为我们每天都会收到几十个这样的文件)或2)调整代码以便它只检测 < / em> doc中的表。

有没有办法做这些事情的好方法?

Option Compare Database

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 & "\cmoSheet.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("cmoSheetTbl") 'establish the recordset

With doc.Tables(1) 'target table 1 in cmoSheet.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
                ![ReviewerName] = doc.Tables(1).Cell(i, 1).Range.Text
                ![ProductDesc] = doc.Tables(1).Cell(i, 2).Range.Text
                ![NPI] = doc.Tables(1).Cell(i, 3).Range.Text
                ![LastName] = doc.Tables(1).Cell(i, 5).Range.Text
                ![FirstName] = doc.Tables(1).Cell(i, 6).Range.Text
                ![ProviderType] = doc.Tables(1).Cell(i, 7).Range.Text
                ![Specialty] = doc.Tables(1).Cell(i, 8).Range.Text
                ![BatchID] = doc.Tables(1).Cell(i, 9).Range.Text
                ![AdditionalDocs?] = doc.Tables(1).Cell(i, 10).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

1 个答案:

答案 0 :(得分:1)

嵌套With必须与外部With相关联。此外,添加Option Explicit会在您的代码中显示一些错误。

db.Close: Set rst = Nothing

应该是:

dbs.Close: Set dbs= Nothing

由于在声明变量时创建了对Word的早期绑定,因此您只需使用New关键字创建实例:

Dim appWord As Word.Application, doc As Word.Document
Set appWord = New Word.Application

如果要创建对Word的后期绑定,请删除对它的引用并将变量声明为Object

Dim appWord As Object, doc As Object
Set appWord = CreateObject("Word.Application")

试试这个:

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 = New Word.Application 'establish an instance of word
    strDoc = CurrentProject.Path & "\cmoSheet.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("cmoSheetTbl") 'establish the recordset

    With doc.Tables(1) 'target table 1 in cmoSheet.docx

        Dim i As Integer
        For i = 2 To .Rows.count 'cycle through rows in Tables(1) [we skip the first row because the table has headers]

            rst.AddNew 'creating a new record
            rst![ReviewerName] = .Cell(i, 1).Range.Text
            rst![ProductDesc] = .Cell(i, 2).Range.Text
            rst![NPI] = .Cell(i, 3).Range.Text
            rst![LastName] = .Cell(i, 5).Range.Text
            rst![FirstName] = .Cell(i, 6).Range.Text
            rst![ProviderType] = .Cell(i, 7).Range.Text
            rst![Specialty] = .Cell(i, 8).Range.Text
            rst![BatchID] = .Cell(i, 9).Range.Text
            rst![AdditionalDocs?] = .Cell(i, 10).Range.Text
            rst.Update 'update the whole record

        Next 'go to next row in Tables(1)
    End With

    rst.Close: Set rst = Nothing 'close and clear recordset
    dbs.Close: Set dbs = 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