Consulting

Results 1 to 2 of 2

Thread: Automate Macro execution

  1. #1

    Automate Macro execution

    Good afternoon all!

    I have a bit of code that essentially sends off an attachment from a given folder, and a subject via a secure email account. Which is fine and dandy, it all works no problems at all. But I want to be able to have this macro run at a certain point of the day on weekdays only.

    Preferably 9:30am mon - fri. I'm completely drawing a blank on how it can be done, can any of you help? Thanks in advance!

    My code is below

    Option Explicit
    
    
    
    
    Sub SendTEC()
        Const strPath As String = "NETWORK PATH"
        Dim strChar As String
        Dim strFile As String
        strFile = Dir$(strPath & "*.*") 'any file in the folder
        While strFile <> ""
            CreateMessage strPath & strFile, Mid(strFile, 2, 2)
            DoEvents
            strFile = Dir$()
        Wend
        Exit Sub
    End Sub
    
    
    Sub CreateMessage(strAtt As String, strChar As String)
        Dim oAccount As Account
        Dim olMail As MailItem
        Dim olAttach As Attachment
        Const strAcc = "EMAIL" 'the name of the account to use
        For Each oAccount In Application.Session.Accounts
            If oAccount.DisplayName = strAcc Then
                Set olMail = Outlook.CreateItem(olMailItem)
                With olMail
                    .SendUsingAccount = oAccount
                    .To = "DESTINATION EMAIL"
                    .Subject = strChar
                    .Body = "" ' clear the body
                    Set olAttach = .Attachments.Add(strAtt)
                    '.Display
                     .Send '- Restore after testing
                End With
                Exit For
            End If
        Next
    lbl_Exit:
        Set olMail = Nothing
        Set olAttach = Nothing
        Set oAccount = Nothing
        Exit Sub
    End Sub

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    You can see an example here Send an Email When a Reminder Fires then Send a message to someone when a reminder fires.

    Drop all lines that generate a message and call SendTEC instead.
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

Posting Permissions

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