Results 1 to 5 of 5
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    Open Outlook if it's not already open

    Hi Guys

    I have this code that runs after a tick box is ticked

    'this requires a refrence to outlook object library

    Dim bStarted As Boolean


    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
    On Error Resume Next

    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
    End If

    'Create a new mailitem

    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
    'Set the recipient for the new email
    .BodyFormat = olFormatHTML
    .To = Me.UsersEMailAddress
    '.CC = DLookup("SupportEmail2", "Settings", "ID = 1")
    .Subject = Me.NoteType
    .HTMLBody = Me.Notes & "This Ticket Has Been Closed By " & Me.ClosedBy
    '.Display
    .Send
    End With
    If bStarted Then
    'If we started Outlook from code, then close it
    oOutlookApp.Quit
    End If
    Set oOutlookApp = Nothing
    Set oItem = Nothing
    MsgBox "This Ticket Has Been Closed. An E-Mail Has Been Sent To " & Me.OpenedBY, vbOKOnly, "This Ticket Has Been Marked As Closed"
    End If

    this works if outlook is open, but if its not no e-mail is sent,
    how can I change this code so that outlook opens and sends the mail if it's not already open?

    im really sorry if this has been asked before but the more I look at this the more confused I'm getting

    many thanks

    Steve

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    How is this code invoked? It isn't clear from the code shown.
    You have the code to deal with the condition you describe
    Code:
    'Get Outlook if it's running
               Set oOutlookApp = GetObject(, "Outlook.Application")
               If Err <> 0 Then
                   'Outlook wasn't running, start it from code
                      Set oOutlookApp = CreateObject("Outlook.Application")
                      bStarted = True
            End If
    You shouldn't test with On Error Resume Next
    It isn't helpful since it simply masks/ignores an error.

    Use a proper error handling label and routine to help identify the error condition.

    God luck.

  3. #3
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Orange

    Thanks for the reply

    I have inherited this code from a previous dev, the code is invoked from a tick box called "Closed" after update event sorry I should have added all the code which is run, I have added it below

    Private Sub Closed_AfterUpdate()
    On Error GoTo Closed_AfterUpdate_Error

    Me.Closed.Locked = True
    Me.Notes.Locked = True
    Me.IssueClosed = Date
    Me.NoteEndTime = Now()
    Me.TicketClosedLBL.Visible = True
    Me.Section(acDetail).BackColor = colorTan_Encarnacion
    Me.Section(acFooter).BackColor = colorTan_Encarnacion

    'Dragusername taken from the module showdomainusername
    Me.ClosedBy = UserFullName 'DragUserName()
    Me.On_Hold.Locked = True
    Me.[Take Off Hold].Locked = True
    If IsNull(Me.UsersEMailAddress) Then
    MsgBox "This Ticket Has Been Closed. Because A Member Of The IT Department Created This Ticket No E-Mail Has Been Sent ", vbOKOnly, "This Ticket Has Been Marked As Closed"
    Else
    'this requires a refrence to outlook object libary
    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
    On Error Resume Next

    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
    End If
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
    'Set the recipient for the new email
    .BodyFormat = olFormatHTML
    .To = Me.UsersEMailAddress
    '.CC = DLookup("SupportEmail2", "Settings", "ID = 1")
    .Subject = Me.NoteType
    .HTMLBody = Me.Notes & "This Ticket Has Been Closed By " & Me.ClosedBy
    '.Display
    .Send
    End With
    If bStarted Then
    'If we started Outlook from code, then close it
    oOutlookApp.Quit
    End If
    Set oOutlookApp = Nothing
    Set oItem = Nothing
    MsgBox "This Ticket Has Been Closed. An E-Mail Has Been Sent To " & Me.OpenedBY, vbOKOnly, "This Ticket Has Been Marked As Closed"
    End If
    DoCmd.Close
    On Error GoTo 0
    Exit Sub
    Closed_AfterUpdate_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Closed_AfterUpdate of VBA Document Form_EditNewAssetNote"
    On Error GoTo 0
    Exit Sub

    End Sub

    in relation to the error handling, im not really sure where that would be placed. love access but still learning

    Many Thanks

    Steve

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    OK, we've all inherited somethings.
    Do you know how to step debug? If not, read this link by Chip Pearson. It mentions Excel, but it works with vba.
    Do you have a reference to the Outlook library?

    Make a comment of this line
    'On Error Resume Next it is causing access to ignore an error (or could be)

    Are you working on a copy of your database -- if not you should be?

    Put a breakpoint on the first line and step debug.

    Good luck.

  5. #5
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Orange

    yes I am working on a copy, I will comment out 'On Error Resume Next and read the link you suggested.

    will let you know how I get on.

    many thanks for the heads up

    Steve

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

Similar Threads

  1. Code doesn't open outlook and it should
    By mercapto in forum Programming
    Replies: 20
    Last Post: 09-21-2013, 04:03 PM
  2. Hyperlink in Form will not open Outlook
    By abuller in forum Access
    Replies: 1
    Last Post: 11-16-2012, 03:22 PM
  3. Pass date and open outlook
    By jaykappy in forum Programming
    Replies: 3
    Last Post: 10-04-2012, 07:06 AM
  4. Open Outlook window but don't send
    By Paul Taylor in forum Access
    Replies: 2
    Last Post: 04-07-2011, 11:51 AM
  5. Open Outlook & populate e-mail
    By NISMOJim in forum Forms
    Replies: 7
    Last Post: 10-09-2010, 01:01 PM

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