2017-07-03 150 views
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 

回答

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的後期綁定,刪除對它的引用並聲明變量爲(s)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 
+0

是的,我不小心粘貼之前,我糾正了幾個像DB錯誤的,我運行的代碼 - > DBS。這實際上現在運行良好,即使在前面和後面的文字段落的文本!謝謝!現在我唯一的問題是格式。每個單元以一個圓點結束,看起來幾乎像一個圓點。那個單詞的表/單元格是否是分隔符?我可以刪除它嗎? – Steven

+0

@Steven不確定你的意思。那些被'.Range.Text'拾取?你可以發佈一個樣本嗎? –

+0

我試圖在這裏發佈它未格式化,但它不斷被刪除。項目符號不包含在表格的數據集中,在任何地方。我將拍攝一個屏幕截圖併發布[imgur鏈接](https://i.imgur.com/YHjELbm.jpg) – Steven

相關問題