Sub ForwardToExcel() Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim iLastRow As Integer Dim iRow As Integer Dim sSearch As String Dim sTo As String Dim objMail As Outlook.MailItem '获取当前邮件 Set objMail = Application.ActiveInspector.CurrentItem '打开Excel文件 Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open("C:\MyExcel.xlsx") Set xlSheet = xlBook.Worksheets("Sheet1") '获取Excel表格最后一行 iLastRow = xlSheet.Cells(xlSheet.Rows.Count, "A").End(xlUp).Row '查找邮件内容在Excel中的行数 sSearch = objMail.Body For iRow = 1 To iLastRow If xlSheet.Cells(iRow, "A").Value = sSearch Then sTo = xlSheet.Cells(iRow, "B").Value Exit For End If Next iRow '将邮件转发到对应邮箱 If sTo <> "" Then Set objForward = objMail.Forward objForward.To = sTo objForward.Send End If '关闭Excel文件 xlBook.Close SaveChanges:=True xlApp.Quit Set xlSheet = Nothing Set xlBook = Nothing Set xlApp = Nothing Set objMail = Nothing End Sub 这个代码报错:运行时错误‘91’:对象变量或with块变量未设置,如何解决
时间: 2023-03-30 22:04:45 浏览: 230
这个问题可能是由于没有正确设置对象变量或with块变量引起的。您可以检查代码中是否有未设置的变量或对象,或者尝试使用“Set”语句为变量或对象分配值。您还可以尝试使用“On Error Resume Next”语句来避免出现运行时错误。
相关问题
Dim xlApp As Excel.Application 用户定义类型未定义
在 VBA (Visual Basic for Applications) 中,`Dim xlApp As Excel.Application` 这一行定义了一个名为 `xlApp` 的变量,并指定了它的数据类型为 `Excel.Application`,这是 Microsoft Excel 对象模型的一部分,代表整个 Excel 应用程序实例。当你尝试创建这样的变量时,如果没有先引用 Excel 库(如 `Microsoft Excel` 或 `Excel`),则会报“用户定义类型未定义”的错误。
这是因为你需要首先声明 `Excel` 对象库,例如:
```vba
Sub DeclareObject()
Dim objXL As Object ' 定义一个对象类型变量
Set objXL = CreateObject("Excel.Application") ' 创建并设置为Excel应用程序
End Sub
```
在这之后,你就可以安全地使用 `xlApp` 变量了:
```vba
Sub Main()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
' 然后你可以在这里使用 xlApp 对象执行Excel操作...
End Sub
```
确保在引用 `xlApp` 之前,你已经通过上述步骤导入了 `Excel.Application` 类型。
VB6 打开Excel,程序容易闭退,请优化源代码:Function ReadMateria() As Boolean Dim xlRow As Integer, xlBook As New Excel.Workbook, xlSheet As New Excel.Worksheet, xlApp As New Excel.Application Set xlApp = CreateObject("Excel.Application") Set xlBook = xlApp.Workbooks.Open(ProcessPath.Text) Set xlSheet = xlBook.Worksheets("Material") For xlRow = 1 To 30 xlSheet.Cells(xlRow + 5, xlCol + 2) = "00" Next If Not xlBook Is Nothing Then xlBook.Close (True) Set xlBook = Nothing Set xlSheet = Nothing xlApp.Quit Set xlApp = Nothing End Function
以下是经过优化后的代码,可以避免程序崩溃:
Function ReadMateria() As Boolean
Dim xlBook As Excel.Workbook, xlSheet As Excel.Worksheet, xlApp As Excel.Application
On Error GoTo ErrHandler
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(ProcessPath.Text)
Set xlSheet = xlBook.Worksheets("Material")
For xlRow = 1 To 30
xlSheet.Cells(xlRow + 5, xlCol + 2) = "00"
Next
xlBook.Close (True)
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
ReadMateria = True
Exit Function
ErrHandler:
xlBook.Close (False)
Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
ReadMateria = False
End Function
主要优化措施包括:
1. 使用 Dim 声明变量时,只需要声明变量类型即可,不需要再使用 New 关键字。
2. 使用 On Error GoTo ErrHandler 语句,避免程序出现错误时直接崩溃。
3. 在 ErrHandler 中关闭 Excel 文件,并释放所有对象的引用。
4. 将函数的返回值设为 True 或 False,以便在程序中判断是否读取成功。
阅读全文
相关推荐

















