Consulting

Results 1 to 4 of 4

Thread: Run Outlook from Excel

  1. #1
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location

    Question Run Outlook from Excel

    hi all..
    i've found some vba code to send email's from excel ( see code below ).
    what i need is to run outlook before the code or check if outlook is already running. then run the code.

    many thanks

    Option Explicit
    
    Sub Mail_ActiveSheet_Outlook()
    'You must add a reference to the Microsoft outlook Library
        Dim OutApp As Outlook.Application
        Dim OutMail As Outlook.MailItem
        Dim WB As Workbook
        Dim strdate As String
        strdate = Format(Now, "dd-mm-yy h-mm-ss")
        Application.ScreenUpdating = False
        ActiveSheet.Copy
        Set WB = ActiveWorkbook
        With WB
            .SaveAs "Part Of ..." & ThisWorkbook.Name _
                  & " " & strdate & ".xls"
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(olMailItem)
            With OutMail
                .To = "a_user@anywhere.com"
                '.CC = ""
                '.BCC = ""
                .Subject = "stats 2005"
                .Body = "See Attached File Please...."
                .Attachments.Add WB.FullName
                'You can add other files also like this
                '.Attachments.Add ("C:\test.txt")
                .Send   'or use .Display
            End With
            .ChangeFileAccess xlReadOnly
            Kill .FullName
            .Close False
        End With
        Application.ScreenUpdating = True
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi there,

    If you're only looking to check if outlook is open or not, maybe you can use something like this ...

    Sub CheckOLopen()
    Dim AppOutLook As Object 
    On Error Resume Next 
    Set AppOutLook = GetObject(, "Outlook.Application") 
    On Error Goto 0 
    If AppOutLook Is Nothing Then 
        MsgBox "Need to open Outlook." 
    Else 
        MsgBox "Outlook is open, so no need to open it." 
    End If
    End Sub

  3. #3
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    thanks for the quick reply, just had a go with the code and keeps saying "Outlook is open, so no need to open it." when it's not!.

  4. #4
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    sorry my mistake it work. thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •