当我试图运行简单的代码时,我得到了“错误91:对象变量或未设置块变量”。这段代码可以在多台机器上运行。在更新或重新安装了几个应用程序后,一台计算机现在抛出错误91;Excel可能是也可能不是重新安装的应用程序之一。我的机器安装了Excel2010 (14.0.7143.5000),代码在上面运行得很流畅。机器制造问题有Excel 2010 (14.0.7015.1000)。我已经检查过了,没有发现遗漏的库引用。
以下(标准export-pdf)代码就是问题所在。当在第二个With块中引用"OutlApp“对象时,会抛出错误。
我不是一个专业的程序员,几乎不是业余的。任何帮助都将不胜感激。
Function ExportData()
Dim PrintRange As Range
Dim IsCreated As Boolean
Dim PDFData As String
Dim OutlApp As Object
Dim sh1 as Worksheet
Set sh1 = Sheets("Data")
Set PrintRange = Union(sh1.Range("B4:L70"), sh1.Range("B72:L138"))
PDFData = "F:\DATA\file " & Range("K4").Value & ".pdf"
With PrintRange
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFData, _
QUALITY:=xlQualityStandard, INCLUDEDOCPROPERTIES:=True, _
IGNOREPRINTAREAS:=False, OPENAFTERPUBLISH:=False
End With
On Error Resume Next
Set OutlApp = GetObject(, "OUTLOOK.APPLICATION")
If Err Then
Set OutlApp = CreateObject("OUTLOOK.APPLICATION")
IsCreated = True
End If
OutlApp.Visible = True
On Error GoTo 0
With OutlApp.CREATEITEM(0) '<---Error 91 thrown here
.Subject = "DATA PACKAGE " & Range("K4").Value
.To = Range("I15").Value
.BODY = ""
.attachments.Add PDFData
On Error Resume Next
.SEND
On Error GoTo 0
End With
Kill PDFData
If IsCreated Then OutlApp.Quit
Set OutlApp = Nothing
End Function发布于 2015-07-20 23:28:36
我不能百分之百确定,但问题似乎是这行失败了:
Set OutlApp = GetObject(, "OUTLOOK.APPLICATION")还有这一行:
Set OutlApp = CreateObject("OUTLOOK.APPLICATION")所以当你到了抛出错误91的代码行时-- OutlApp没有被设置。至于这些线路失败的原因,可能是用户没有安装Outlook?
发布于 2015-07-20 23:38:32
试着这样做(用你的with代码块来改变它):
Dim Mail
Set Mail = OutlApp.CreateItem(0)
With Mail
.Subject = "DATA PACKAGE " & Range("K4").Value
.To = Range("I15").Value
.BODY = ""
.attachments.Add PDFData
On Error Resume Next
.SEND
On Error GoTo 0
End With
Set Mail = Nothing发布于 2015-07-21 04:35:40
因此,在通过Windows Update几次之后,代码运行时没有错误。@JohnColeman说得对,object变量没有被设置,但我不知道为什么没有设置它,而且,更新是如何让一切顺利运行的。
https://stackoverflow.com/questions/31520115
复制相似问题