6

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

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 >":

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.

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

Private Sub EmailButton_Click()
    Dim EmailThis As String

    EmailThis = CreateEmailWithOutlook("myname@company.com", "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

|date filter use DateTime::format function which doesnt support locales. See this question and write your own twig extension.

4

1 に答える 1

8

問題は、Outlook ライブラリ参照が有効になってolMailItemいると、予約済みの定数であるということです。それは問題ではないと思いますがDim olMailItem as Outlook.MailItem、変数を設定しようとすると問題が発生します。

完全な説明は次のとおりです。

olMailItemオブジェクト変数として宣言しました。

  • 割り当てステートメントの右側でObjectは、オブジェクトのインスタンスに値を設定する前に this を参照しています。オブジェクトが自分自身を割り当てようとしているため、これは基本的に再帰的なエラーです。
  • 別の潜在的なエラーがあります。olMailItem以前に割り当てられていた場合、このステートメントは別のエラーを発生させます (Mismatch定数olMailItemは整数であるため、おそらくエラーですが、この名前を不適切に使用すると、予期されるObject場所にを渡して不一致エラーが発生する可能性がありますInteger

この変数の名前を などの別の名前に変更してみて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
于 2013-06-28T13:57:47.173 に答える