我正在尝试将数据从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
答案 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
要确保的其他要点是: -
最后您的更正代码如下。
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
答案 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