【问题标题】:Identifying where a table begins in a Word doc识别表格在 Word 文档中的开始位置
【发布时间】:2017-12-07 00:08:20
【问题描述】:

我有一个每天生成的特定工作文档,其中包括一段文本,然后是一个包含一堆客户数据的表格。我需要将该数据导入 Access 表中。

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

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

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

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

【问题讨论】:

    标签: ms-access import ms-word vba


    【解决方案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
    

    【讨论】:

    • 是的,我在纠正一些错误(如 db -> dbs)之前不小心粘贴了正在运行的代码。现在实际上运行良好,即使表格前后有一段文本!谢谢!现在我唯一的问题是格式。每个单元格都以一个点结尾,看起来几乎像一个要点。是那个词的表格/单元格分隔符吗?我可以删除它吗?
    • @Steven 不知道你的意思是什么子弹。那些被.Range.Text 接走的人吗?可以发个样本吗?
    • 我试图在此处发布未格式化的内容,但它一直被删除。项目符号不包含在表的数据集中,任何地方。我将截屏并发布imgur link here
    • 嗯.. 试试Replace(.Cell(i, 1).Range.Text, Chr(149), vbNullString).
    • 我试过rst![ReviewerName] = Replace(doc.Tables(1).Cell(i, 1).Range.Text, Chr(149), vbNullString),但很遗憾,没有任何变化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多