将数据从Excel传输到预先存在的Word表

时间:2016-06-24 19:47:44

标签: excel vba word-vba

我正在尝试将数据从Excel导出到预先存在的Word表格。

代码到达For Each wdCell In wdDoc.Tables循环后,我收到运行时错误

  

'91'对象变量或With with block变量未设置。

有没有办法让这段代码将数据传输到7列?

Sub ExportDataWordTable()

Const stWordDocument As String = "C:\Users\jfournier\Desktop\VBA Macro Files\TESTQUOTE.docm"

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdCell As Word.Cell
Dim i As Long
Dim j As Long
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim vaData As Variant

Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet2")

ReDim vaData(1 To 10, 1 To 5)

With wsSheet
    vaData = .Range("B3:H20")
End With

'Here we instantiate the new object.
Set wdApp = New Word.Application

'Here the target document resides in the same folder as the workbook.
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)

'Import data to the first table and in the first column of a table in Microsoft Word.
For j = 1 To 5
    i = 0

    For Each wdCell In wdDoc.Tables(2).Columns(j).Cells
        i = i + 1
        wdCell.Range.Text = vaData(i, j)
    Next wdCell

Next j

'Save and close the document.
With wdDoc
    .Save
    .Close
End With

'Close the hidden instance of Microsoft Word.
wdApp.Quit

'Release the external variables from the memory
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "The data has been transferred to Test.doc", vbInformation

End Sub

enter image description here

3 个答案:

答案 0 :(得分:1)

我已在您的代码中观察到以下几点进行更正。

您正在设置文件的完整路径TESTQUOTE.docm

Const stWordDocument As String = "C:\Users\jfournier\Desktop\VBA Macro Files\TESTQUOTE.docm"

稍后您正在设置目标文档所在的路径与工作簿位于同一文件夹中。

Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)

这会在路径字符串中产生冲突。你应该只提一下。

Const stWordDocument As String = "TESTQUOTE.docm"

您希望代码将数据传输到7列。您已将范围视为B3:H20 但您已将变体vaData设置为仅限5列。

 ReDim vaData(1 To 10, 1 To 5)

此外,您只循环5列。

'Import data to the first table and in the first column of a table in Microsoft Word.
For j = 1 To 5

这两行需要更改为: -

ReDim vaData(1 To 10, 1 To 7)

For j = 1 To 7

要确保的其他要点是: -

  1. 您已设置参考Microsoft Word对象库 对应于您的Excel版本。我有Excel 2016所以我有 设置对Microsoft Word 16.0对象库的引用。
  2. 您的Word文件应存在于您拥有的同一目录中 存储了ThisWorkbook宏文件。我曾经和Macro合作过 Excel宏文件,而不是Word VBA编辑器。
  3. 您的TESTQUOTE word文档应具有正确的表结构 对应于范围B3:H20,即18行和7列。它 从Excel运行VBA程序时,应处于关闭状态 文件。
  4. 最后您的更正代码如下。

     Sub ExportDataWordTable()
    
        Const stWordDocument As String = "TESTQUOTE.docm"
    
        Dim wdApp As Word.Application
        Dim wdDoc As Word.Document
        Dim wdCell As Word.Cell
        Dim i As Long
        Dim j As Long
        Dim wbBook As Workbook
        Dim wsSheet As Worksheet
        Dim vaData As Variant
    
        Set wbBook = ThisWorkbook
        Set wsSheet = wbBook.Worksheets("Sheet2")
    
        ReDim vaData(1 To 10, 1 To 7)
    
        With wsSheet
            vaData = .Range("B3:H20")
        End With
    
        'Here we instantiate the new object.
        Set wdApp = New Word.Application
    
        'Here the target document resides in the same folder as the workbook.
        Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)
    
        'Import data to the first table and in the first column of a table in Microsoft Word.
        For j = 1 To 7
        i = 0
    
        For Each wdCell In wdDoc.Tables(1).Columns(j).Cells
            i = i + 1
            wdCell.Range.Text = vaData(i, j)
            Next wdCell
    
        Next j
    
        'Save and close the document.
        With wdDoc
        .Save
        .Close
        End With
    
        'Close the hidden instance of Microsoft Word.
        wdApp.Quit
    
        'Release the external variables from the memory
        Set wdDoc = Nothing
        Set wdApp = Nothing
        MsgBox "The data has been transferred to TESTQUOTE.docm", vbInformation
    
    End Sub
    

    我已经在样本数据上测试了这个程序,并且我附加了Excel样本数据的快照和在Word文档上获得的结果。 Data Filled Word table Excel sample data

答案 1 :(得分:1)

以下是您尝试的另一种方法:

for child in self.children {
    if ["goToHomeText", "noText", "yesText", "box"].contains(child.name) {
        child.removeFromParent()
    }
}

注意:由于要复制的行数不同,因此上面的代码假定使用范围决定了要复制的行数。通过这种方法,您的Word表仅需要其标题行。

答案 2 :(得分:0)

  

[已解决]

     

错误4605 –“此方法或属性不可用,因为剪贴板为空或无效”

/api/cors?url=http://10.131.12.49:8010/get.me.data?opt=All