0
我有一份每天都会生成的特定工作文档,其中包括一段文字,后面跟着一张表格,其中包含一堆客户数据。我需要将该数据导入到Access表中。确定表格在Word文档中的起始位置
我发现了代码,我将在下面介绍这些代码,它就是这么做的。然而,它没有按预期工作。相反,它根本不工作。我预计这是因为doc这个词不是以表格而是以文字开始的。
所以我有两个选择。 1)找到一种方法来格式化每个文档,使其仅包含表格(我将不得不自动执行此操作,因为我们每天都会收到几十个这样的文件),或者2)调整代码,使其仅检测到表格文档。
有没有做这些事情的好方法?
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
是的,我不小心粘贴之前,我纠正了几个像DB错误的,我运行的代码 - > DBS。这实际上现在运行良好,即使在前面和后面的文字段落的文本!谢谢!现在我唯一的问题是格式。每个单元以一个圆点结束,看起来几乎像一个圆点。那个单词的表/单元格是否是分隔符?我可以删除它吗? – Steven
@Steven不确定你的意思。那些被'.Range.Text'拾取?你可以发布一个样本吗? –
我试图在这里发布它未格式化,但它不断被删除。项目符号不包含在表格的数据集中,在任何地方。我将拍摄一个屏幕截图并发布[imgur链接](https://i.imgur.com/YHjELbm.jpg) – Steven