简体   繁体   中英

Outlook template pull up Excel using VBA

I have a list of Data that contains various entry details (Made by, Date, Amount, etc.) stored on a excel worksheet. I am trying to auto support to people who made the entries in the first place.

My goals for the macro I'm writing are:

  1. Create a mail object
  2. Set mail object to outlook template file
  3. setting and then filling in the template with data about the original entry writer (thats who I will be sending it to), the comment they left about the entry, and pulling some other details for the template.
  4. Save to drafts for later review/send

This what I have to far:

Sub SendMail()
        Dim olApp As Outlook.Application 
         Dim olMail As Outlook.MailItem 
         Dim  blRunning As Boolean

         blRunning = True 
         on Error Resume Next 
         Set olApp = GetObject (,"Outlook.Application") 
         If olApp Is Nothing Then 
             Set olApp = New Outlook.Application 
             blRunning = False 
        End If  
        On Error GoTo 0 

        Set olMail = olApp.CreateItem(olMailItem) 
        With olMail 
            .Subject = "Automatic Entry Support" 
            .Recipients.Add "J.Doe@gmail.com"  
            .Attachments.Add "C:\Users\J.Doe\Documents\Project\Template draft.xlsx"  
            .Body = "Support message" 
            .Display ("Spell check, data attached?, SEND")
        End With
        If Not blRunning Then olApp.Quit

        Set olApp = Nothing
        Set olMail = Nothing
    End Sub

(There are lines in the code that I placed with "fake" lines to not show anything important).

Also, this is my first post on this site so please comment if I am not asking the question in the right format.

UPDATE : I am also receiving an error when trying to run the code: "Run-time error '-2147024894 (8007002): Automation error"

 .Body = "Support message" 

You may try to set the BodyFormat before setting the message body.

Most probably you have faced with a security issue in Outlook. "Security" in this context refers to the so-called "object model guard" that triggers security prompts and blocks access to certain features in an effort to prevent malicious programs from harvesting email addresses from Outlook data and using Outlook to propagate viruses and spam.

Possible workarounds are:

  1. Outlook Security Manager which allows to disable and enable security in Outlook dynamically.
  2. A low-level code which doesn't trigger security issues (Extended MAPI).
  3. Develop a COM add-in. In order to avoid Outlook object model security prompts, the add-in must derive all Outlook objects from this Outlook.Application object. In VSTO add-ins, that would be the Globals.ThisAddin.Application object.
  4. Deploying a GPO policy.

Read more about possible options in the Outlook "Object Model Guard" Security Issues for Developers article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM