In Outlook, create a VBA Script for sending email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blzr511
    New Member
    • Jul 2014
    • 3

    In Outlook, create a VBA Script for sending email

    Hello,

    A simple request I am sure, but I am unclear of the procedure. I have limited experience with VB, so here goes:

    I am trying to create a simple VB application that will bring up a form box with a TextInput for an email address and a SEND (caption) button only.

    The function of this application will, when the SEND button is clicked Outlook 2010 will:

    1. Open and populate the To: field with the email address from the Text Box input.
    2. Attach a file rom a specified folder (same with all outgoing emails)
    3. Close Outlook

    Basically our secretary should have this Form Input box on her desktop, type an email address and hit SEND.

    Any detailed assistance is appreciated.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    What about the 'Right Click'/'Send to'/'Mail recipient'?

    If your secretary is not smart enough to do that, your secretary should get a basic computer course.

    This will benefit you, and your company.

    Comment

    • Blzr511
      New Member
      • Jul 2014
      • 3

      #3
      An option, yes...but there is a reason for the method I have inquired about!

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        In Outlook press ALT+F10

        Create a new 'UserForm', and add a Textbox, and a Button on it.

        Give the butten a correct name, and doubleclick it.

        enter next code:
        Code:
        Private Sub Send_Click()
            Dim msg As Outlook.MailItem
            Set msg = Application.CreateItem(olMailItem)
            msg.Subject = "Hello World!"
            msg.To = Me.TextBox1.Value
            msg.Attachments.Add "c:\temp\test.pdf", olByValue, 2, "test"
            UserForm1.Hide
            msg.Send
            Set msg = Nothing
        End Sub

        after that, rightclick on 'Modules', and create a module with next code:
        Code:
        Sub mailthis()
            UserForm1.Show
        End Sub
        save everything,

        a shotcurt with the following command should start tha macro (providing the names are equal):
        Code:
        "C:\Program Files\Microsoft Office 15\root\office15\outlook.exe" /autorun "Project1.mailthis"

        Comment

        • Blzr511
          New Member
          • Jul 2014
          • 3

          #5
          Perfect, thank you Luuk, that does work! Keeping the form open is perfect, however the line for closing Outlook upon sending is what I also need.

          I played with the following, but unable to mesh:

          objMail.Send

          objOutlook.Quit
          Set objMail = Nothing
          Set objOutlook = Nothing

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            Code:
            Application.Quit
            will close Outlook,
            but only if the mail is already send,
            otherwise some notification is show about Outlook being busy sending a mail.....

            Comment

            Working...