2016-12-29 73 views
-1

我想爲我的團隊使用電子郵件功能的自動化,我也是一個菜鳥,所以請原諒這裏的基本代碼。我收到錯誤消息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

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

回答

1

問題是,你聲明瞭一個對象變量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

沒關係,對不起,我弄明白了,我應該用&代替這個詞並且 –

相關問題