运行时错误 91;将数据从 Excel 传输到 Word Table
RunTime Error 91; Transferring Data from Excel to Word Table
我在处理目前发现的看似简单的问题时遇到了问题。我正在尝试通过 VBA 将 excel 工作簿中的数据 table 传送到 word 文档中的 table。这是我到目前为止发现并稍作更改的代码...
Sub GetData()
Dim strPath As String
Dim strFileName As String
Dim strFileExtension As String
Dim strFullName As String
strPath = "file path here"
strFileName = "file name here"
strFileExtension = "Extension here"
strFullName = strPath & strFileName & strFileExtension
Set objWorkbook = objExcel.Workbooks.Open(strFullName)
'Set the text of the cell from Excel to the cell in the specified table in
'Word (the second table in this instance)
ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1") _
.Cells(2, 1)
'Close Excel bits
objWorkbook.Close
Set objWorkbook = Nothing
End Sub
我发现的最初错误是没有在参考资料中勾选 excel 对象库和简单的语法错误。修复这些后,我现在得到 'RunTime Error 91 Object Variable or With Block Variable not set'。当我尝试设置 objWorkbook 变量时会发生此错误。我声明了这些 public 变量...
Public objExcel As Excel.Application
Public objWorkbook As Excel.Workbook
Public objWorksheet As Excel.Worksheet
Public objRange As Excel.Range
然而,当我查找这个错误时,我发现我需要声明这些 public 个变量。不知道从这里去哪里。如果有人能把我推向正确的方向,那将不胜感激。另外,感谢您迄今为止提供的所有帮助,这个网站可以挽救生命。
试试这个:
Sub GetData()
Dim strPath As String
Dim strFileName As String
Dim strFileExtension As String
Dim strFullName As String
dim objExcel As Object, objWorkbook As Object
strPath = "file path here"
strFileName = "file name here"
strFileExtension = "Extension here"
strFullName = strPath & strFileName & strFileExtension
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strFullName)
'Set the text of the cell from Excel to the cell in the specified table in
'Word (the second table in this instance)
ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1").Cells(2, 1)
'Close Excel bits
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
End Sub
顺便说一句,如果您使用上面使用的 CreateObject
函数,则不需要添加引用。
您有几个问题:
- 没有定义(设置)
objExcel
应用程序
- 正确执行后没有关闭(清理)
- Excel 个对象的 Public 个不必要的声明
我在处理目前发现的看似简单的问题时遇到了问题。我正在尝试通过 VBA 将 excel 工作簿中的数据 table 传送到 word 文档中的 table。这是我到目前为止发现并稍作更改的代码...
Sub GetData()
Dim strPath As String
Dim strFileName As String
Dim strFileExtension As String
Dim strFullName As String
strPath = "file path here"
strFileName = "file name here"
strFileExtension = "Extension here"
strFullName = strPath & strFileName & strFileExtension
Set objWorkbook = objExcel.Workbooks.Open(strFullName)
'Set the text of the cell from Excel to the cell in the specified table in
'Word (the second table in this instance)
ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1") _
.Cells(2, 1)
'Close Excel bits
objWorkbook.Close
Set objWorkbook = Nothing
End Sub
我发现的最初错误是没有在参考资料中勾选 excel 对象库和简单的语法错误。修复这些后,我现在得到 'RunTime Error 91 Object Variable or With Block Variable not set'。当我尝试设置 objWorkbook 变量时会发生此错误。我声明了这些 public 变量...
Public objExcel As Excel.Application
Public objWorkbook As Excel.Workbook
Public objWorksheet As Excel.Worksheet
Public objRange As Excel.Range
然而,当我查找这个错误时,我发现我需要声明这些 public 个变量。不知道从这里去哪里。如果有人能把我推向正确的方向,那将不胜感激。另外,感谢您迄今为止提供的所有帮助,这个网站可以挽救生命。
试试这个:
Sub GetData()
Dim strPath As String
Dim strFileName As String
Dim strFileExtension As String
Dim strFullName As String
dim objExcel As Object, objWorkbook As Object
strPath = "file path here"
strFileName = "file name here"
strFileExtension = "Extension here"
strFullName = strPath & strFileName & strFileExtension
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strFullName)
'Set the text of the cell from Excel to the cell in the specified table in
'Word (the second table in this instance)
ActiveDocument.Tables(1).Cell(2, 2).Range.Text = objWorkbook.Sheets("Sheet1").Cells(2, 1)
'Close Excel bits
objWorkbook.Close
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
End Sub
顺便说一句,如果您使用上面使用的 CreateObject
函数,则不需要添加引用。
您有几个问题:
- 没有定义(设置)
objExcel
应用程序 - 正确执行后没有关闭(清理)
- Excel 个对象的 Public 个不必要的声明