2

I created a program for myself and a few of my colleagues. One of the functions of my program runs a macro that grabs data from the Excel sheet and opens a populated email in Outlook.

In order to run this macro you need to add the Object Library "Microsoft Outlook 16.0 Object Library".

Is there any way to do this automatically, so that all of my colleagues can run this macro?

Here's the code for my email generator.

' -- Drafts an email in Outlook -- '
Public Sub emailDraft()
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim masterWS As Worksheet
Dim masterWB As Workbook
Dim counter As Long

Set objOutlook = Outlook.Application
Set objMail = objOutlook.CreateItem(olMailItem)

Set masterWB = Workbooks("Master.xlsm")
Set masterWS = masterWB.Worksheets("MASTER SHEET")

objMail.To = masterWS.Range("F6").Value
objMail.CC = "[email protected]"
objMail.Subject = masterWS.Range("F7").Value
objMail.Body = masterWS.Range("F8").Value

objMail.Display
End Sub

enter image description here

3
  • 4
    You can also use late binding and skip the references altogether. Commented Feb 6, 2020 at 16:24
  • @Warcupine The link you sent is good, but still requires me to manually add a reference in order to run the code Commented Feb 6, 2020 at 16:29
  • @Warcupine I'll look into late binding - thank you Commented Feb 6, 2020 at 16:29

1 Answer 1

2

Thank you @Warcupine for your insight.

I was able to solve this by using late binding. My updated code is below.

' -- Drafts an email in Outlook -- '
Public Sub emailDraft()
Dim objOutlook As Object    'Outlook.Application
Dim objMail As Object       'Outlook.MailItem
Dim masterWS As Worksheet
Dim masterWB As Workbook
Dim counter As Long

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(olMailItem)

'objOutlook.CreateItem (olMailItem)

Set wb = ThisWorkbook
Set wsMAST = wb.Worksheets("MASTER")

objMail.To = wsMAST.Range("K2").Value
objMail.CC = "[email protected]"
objMail.Subject = wsMAST.Range("k3").Value
objMail.Body = wsMAST.Range("k4").Value

objMail.Display
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.