Results 1 to 7 of 7
  1. #1
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253

    help with Runtime error 91 "Object variable or With block variable not set"

    I am trying to create a new email with access. I have the following code and i keep getting the runtime error 91 "Object variable or With block variable not set" on the line -- Set olMailItem = olApp.CreateItem(olMailItem) -- this code is in a module. i am calling this code from a form and an on-click event of a button. i believe it is something simple but i cannot figure it out. any help is greatly appreciated. thank you very much in advance. -Walker

    Code:
    Private Sub Command5_Click()
    
        Call CreateEmailWithOutlook("nitewalk@gmail.com", "Test", "TestTest")
        
    End Sub



    here is the module code:

    Code:
    Option Compare Database
    Option Explicit
    
    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 Outlook.Application
        Dim olMailItem As Outlook.MailItem 'An Outlook Mail Item
        
        '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

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    I don't see where you Set olApp?
    Set olMailItem = olApp.CreateItem(olMailItem)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Sorry Micron. I forgot to include that i took out this line -- Set olApp = New Outlook.Application -- it was on the line before the set olMailItem. I took it out because i read that the NEW would make it not trusted and i thought maybe that was causing the problem.

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Never heard of that, but then again...
    I don't see how declaring such an object but not Setting it will ever work. AFAIK, New keyword is for when you use early binding and have set a reference to the library. Thus it is
    Dim objOl as Object
    Set objOl = CreateObject(Outlook.Application).

    If early, then
    Dim objOl as Outlook.Application
    Set objOl = New Outlook.Application

    Of course, if using New with automation you probably want to test for the app already being open rather than just arbitrarily opening another instance of it.

  5. #5
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    Thank you for explaining that Micron. I added the top one and then i figured out that i needed to change another couple lines of code.

    I changed
    Dim olMailItem As Outlook.MailItem 'An Outlook Mail Item
    'Create a new email object
    Set olMailItem = olApp.CreateItem(olMailItem)

    to:

    Dim olMsg As Outlook.MailItem
    Set olObj = CreateObject("Outlook.Application")
    Set olMsg = olObj.CreateItem(olMailItem)

    It all works properly when outlook is open or closed.

    Thank you for your help.
    -Walker

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    NP, glad you got it solved.

  7. #7
    NightWalker's Avatar
    NightWalker is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Apr 2016
    Location
    Midwest USA
    Posts
    253
    I don't completely understand it but I got it. Thank you for your help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error 91 Object Variable or Block Variable Not Set
    By mindbender in forum Programming
    Replies: 5
    Last Post: 05-01-2017, 12:01 PM
  2. Replies: 6
    Last Post: 02-11-2016, 02:05 PM
  3. Object variable or With block variable not defined
    By PorscheMan in forum Programming
    Replies: 3
    Last Post: 01-16-2013, 01:53 PM
  4. Replies: 13
    Last Post: 06-12-2012, 09:52 PM
  5. Replies: 0
    Last Post: 08-10-2011, 11:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums