I created a macro to open an email template, I want to add additional code so that it automatically attaches a specific file, can this be done?

Jeannie 0 Reputation points
2024-07-26T14:14:58.6+00:00

I am fairly new to the macro scene but I was able to create a macro to open an email template, I want to add additional code so that it automatically attaches a specific file, can this be done?

I know that you can nest macros and I believe that's what I will need to do, I am just not having any luck in finding any examples and I am not good enough yet to know how to layout it all out.

Any help will be greatly appreciated!

Outlook
Outlook
A family of Microsoft email and calendar products.
3,427 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,670 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Jeannie 0 Reputation points
    2024-07-26T14:45:05.3666667+00:00

    I copied the following from the Office VBA reference topic, which I know will attach the file.

    Sub AddAttachment()

    Dim myItem As Outlook.MailItem

    Dim myAttachments As Outlook.Attachments

    Set myItem = Application.CreateItem(olMailItem)

    Set myAttachments = myItem.Attachments

    myAttachments.Add "D:\Documents\Q496.xlsx", _

    olByValue, 1, "4th Quarter 1996 Results Chart"

    myItem.Display

    End Sub

    But do I have to input this as a nested command or can I it just run off the Open template macro that I created? If someone could show me an example of a nested macro that would be very helpful as well.

    0 comments No comments

  3. Michael Taylor 51,426 Reputation points
    2024-07-26T15:26:23.4233333+00:00

    If you already have a macro that creates an email from a template then you can simply add the attachment logic as part of that same macro. The only reasons you might leave it as a separate macro are: 1) you need to create an email without the attachment, and 2) the attachment logic is needed by other macros. If you don't need 2 macros though then don't do it as it adds work and makes maintaining them harder.

    Note: I haven't verified your macro code actually works, I'm just giving you a sample of how to do it.

    Sub CreateEmailWithAttachment ()
    
       ' Your existing macro code that creates the email from template goes here...
    
       ' You should already have the email created so this logic is not needed
       'Dim myItem As Outlook.MailItem
       'Set myItem = Application.CreateItem(olMailItem)
    
       ' New code to attach files...
       Dim myAttachments As Outlook.Attachments
       Set myAttachments = myItem.Attachments
    
       myAttachments.Add "D:\Documents\Q496.xlsx", _
          olByValue, 1, "4th Quarter 1996 Results Chart"
    
       myItem.Display  
    
    End Sub
    
    0 comments No comments