对象变量或有块变量未设置(错误91)请协助

问题描述:

我想为我的团队使用电子邮件功能的自动化,我也是一个菜鸟,所以请原谅这里的基本代码。我收到错误消息91对象变量或带块变量未设置对象变量或有块变量未设置(错误91)请协助

下面是代码:

Sub Notification() 
    Dim outobj, mailobj 
    Dim objUserPrmt1 As Object 
    Dim strUserPrmt1 
    Dim message, title, defaultValue As String 

    message = "Enter your issue" 
    title = "InputBox Demo" 
    defaultValue = "No Issue" 

    Set outobj = CreateObject("Outlook.Application") 
    Set mailobj = outobj.CreateItem(0) 
    Set strUserPrmt1 = objUserPrmt1.CreateItem(InputBox(message, title, defaultValue, 25, 45)) 

    With mailobj 
    .To = "[email protected]" 
    .Subject = "Notification:" strUserPrmt1 
    .Body = "Test" 
    '.Send 
    .Display 
    End With 

    'Clear the memory 
    Set outobj = Nothing 
    Set mailobj = Nothing 
    Set strUserPrmt1 = Nothing 
    Set objUserPrmt1 = Nothing 
End Sub 

希望有人能告诉我在哪里我都失败了。

+3

这是VBA而不是VBScript。 objUserPrmt1被使用但从未初始化。 –

+0

也许你的问题是'strUserPrmt1'。将它声明为'string'并使用'strUserPrmt1 = InputBox(message,title,defaultValue,25,45)'。 –

+0

不知道我需要初始化的地方,你能提供一个例子吗? –

问题是,你声明了一个对象变量objUserPrmt1,但是你从来没有将它设置为等于任何东西,因此它默认值为Nothing。不过,你想用什么话该位线:

Set strUserPrmt1 = objUserPrmt1.CreateItem(InputBox(message, title, defaultValue, 25, 45)) 

这无疑是抛出错误(一些东西,你应该在这个问题已经明确有关)的线。

您似乎试图使用Outlook对象模型的CreateItem方法。 Documentation显示这是一个Application对象的方法。既然你已经有这样一个对象(outobj),它根本不清楚objUserPrmt1应该有什么可能的目的。为什么不使用outobj

话虽如此,如果通过更换问题行:

Set strUserPrmt1 = outobj.CreateItem(InputBox(message, title, defaultValue, 25, 45)) 

你仍然可能会得到一个错误(虽然不是同样的错误)。这是因为我链接到的文档说CreateItem方法期望OlItemType constant,但您传递一个字符串。

InputBox返回一个字符串,所以我认为@AlexP是正确的,你想要的是可能只是:

strUserPrmt1 = InputBox(message, title, defaultValue, 25, 45) 

既然字符串不是一个对象是Set它并不需要 - - 刚刚分配。

我建议你简单地从你的代码中删除objUserPrmt1。它没有任何存在的理由,只是导致你的代码抛出错误。请确保您也摆脱了两行:

Set strUserPrmt1 = Nothing 
Set objUserPrmt1 = Nothing 

而且,它不会伤害宣布strUserPrmt1作为一个字符串变量,但其目前的隐含声明为一个变体就足够了。

+0

感谢约翰,这工作解决了错误91问题。 –

+0

现在我得到一个运行时错误13 - 类型不匹配,当我尝试以下:.Subject =“通知:”和strUserPrmt1,任何建议? –

+0

没关系,对不起,我弄明白了,我应该用&代替这个词并且 –