任何人都可以告訴我爲什麼我在下面的函數中收到「運行時錯誤'91'」消息嗎?它的發生在這條線:運行時錯誤'91'&Outlook.Application = <Object variable或With block variable not set>?
Set olMailItem = olApp.CreateItem(olMailItem)
而且,只要我在調試,我把我的光標移到此行,訪問給我這個消息:「Outlook.Application =沒有設置<對象變量或With塊變量>「:
Dim olApp As New Outlook.Application
我試圖創建一個按鈕,將打開的Outlook電子郵件信息,並允許將數據錄入員在發送前編輯消息。我檢查了我的引用,並且檢查了Microsoft Outlook 14.0 Object Library。
此外,如果您對提高代碼效率有任何建議,請分享。我對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
這解決了它!謝謝大衛! – candyA