vba 运行时错误 '91' & Outlook.Application = <对象变量或未设置块变量>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17364673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:56:19  来源:igfitidea点击:

Run-time error '91' & Outlook.Application = <Object variable or With block variable not set>?

vbams-access-2010

提问by candyA

Can anyone tell me why I'm getting the "Run-time error '91'" message in my function below? It's happening on this line:

谁能告诉我为什么我在下面的函数中收到“运行时错误'91'”消息?它发生在这条线上:

Set olMailItem = olApp.CreateItem(olMailItem)

Also, whenever I'm in debug and I place my cursor over this line, Access gives me this message: "Outlook.Application = < Object variable or With block variable not set >":

此外,每当我进行调试并将光标放在这一行上时,Access 都会给我这条消息:“Outlook.Application = < Object variable or With block variable not set >”:

Dim olApp As New Outlook.Application 

I'm trying to create a button that will open an outlook email message and allow the data entry clerk to edit the message before sending it. I've checked my references and I have Microsoft Outlook 14.0 Object Library checked.

我正在尝试创建一个按钮,该按钮将打开 Outlook 电子邮件消息,并允许数据输入员在发送消息之前对其进行编辑。我检查了我的参考资料,并检查了 Microsoft Outlook 14.0 对象库。

Also, if you have any suggestions on making my code more efficient, please share. I'm fairly new to Access programming.

另外,如果您对提高我的代码效率有任何建议,请分享。我对 Access 编程还很陌生。

Private Sub EmailButton_Click()
    Dim EmailThis As String

    EmailThis = CreateEmailWithOutlook("[email protected]", "Testing e-mail Access database", "This is a test")
    DoCmd.SendObject acSendForm, "SubmitNewIdeaForm", , "My Name", , "Test", , True
    On Error GoTo CreateEmail_Exit

CreateEmail_Exit:
    Exit Sub

End Sub

Public Function CreateEmailWithOutlook(MessageTo As String, Subject As String, MessageBody As String)

    ' Define app variable and get Outlook using the "New" keyword
    Dim olApp As New Outlook.Application
    Dim olMailItem As Outlook.MailItem  ' An Outlook Mail item

    Set olApp = CreateObject("Outlook.Application")
    ' Create a new email object
    Set olMailItem = olApp.CreateItem(olMailItem)

    ' Add the To/Subject/Body to the message and display the message
    With olMailItem
        .To = MessageTo
        .Subject = Subject
        .Body = MessageBody
        .Display    ' To show the email message to the user
    End With

    ' Release all object variables
    Set olMailItem = Nothing
    Set olApp = Nothing

End Function

采纳答案by David Zemens

The problem is that, with the Outlook library reference enabled, olMailItemis a reserved constant, and I think when you are Dim olMailItem as Outlook.MailItemthat is not a problem, but trying to set the variable is causing a problem.

问题是,在启用 Outlook 库引用的情况下,它olMailItem是一个保留常量,我认为当您这样做时Dim olMailItem as Outlook.MailItem这不是问题,但尝试设置变量会导致问题。

Here is the full explanation:

这是完整的解释:

You have declared olMailItemas an object variable.

您已声明olMailItem为对象变量。

  • On the right side of the assignment statement, you are referencing this Objectprior to setting it's value to an instance of an object. This is basically a recursive error, since you have the object trying to assign itself itself.
  • There is another potential error, if olMailItemhad previously been assigned, this statement would raise another error (probably a Mismatcherror, since the constant olMailItemis an Integer but by using this name inappropriately, you may introduced the mismatch error by passing an Objectwhere an Integeris expected.
  • 在赋值语句的右侧,您Object在将其值设置为对象实例之前引用了 this 。这基本上是一个递归错误,因为您有对象试图给自己赋值。
  • 还有一个潜在的错误,如果olMailItem之前已经被赋值,这个语句会引发另一个错误(可能是一个Mismatch错误,因为常量olMailItem是一个整数,但是如果不恰当地使用这个名字,你可能会通过传递一个Objectwhere anInteger是期望的来引入不匹配错误。

Try changing the name of this variable olMailItemto something else, like mItem. This code is tested in Excel 2010, Windows 7, I think it should work for Access, too:

尝试将此变量的名称更改为其他名称olMailItem,例如mItem. 此代码在 Excel 2010、Windows 7 中进行了测试,我认为它也适用于 Access:

Dim olApp As New Outlook.Application
Dim mItem As Outlook.MailItem  ' An Outlook Mail item

Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(olMailItem)
' Add the To/Subject/Body to the message and display the message
With mItem
    .To = MessageTo
    .Subject = Subject
    .Body = MessageBody
    .Display    ' To show the email message to the user
End With

' Release all object variables
Set mItem = Nothing
Set olApp = Nothing