I am trying to run a simple macro in excel vba to create a new email and attach two spreadsheets. But it won't work. It stops at the first line everytime. Dim objOutlook As Outlook.Application
Do I need to install Microsoft Outlook 9.0 Object Library? When I go to Tools-->references I checked off MS outlook 9.0 Object Library. Then it tell me it is missing MS outlook 9.0 Object Library.
Appreciated if someone can give me some insight. I am new at write macros.
Thanks in advance.
Expand|Select|Wrap|Line Numbers
- Sub SendMsgwithAttachment()
- Dim objOutlook As Outlook.Application
- Dim objOutlookMsg As Outlook.MailItem
- Dim objOutlookRecip As Outlook.Recipent
- Dim objOutlookAttach As Outlook.Attachment
- Dim RecipList As String
- Dim AttachmentPath As String
- Dim SubjectName As String
- Sheets("Sheet1").Select
- RecipList = Range("B1").Value
- AttachmentPath = Range("B2").Value
- SubjectName = Range("B4").Value
- Filename = Range("B6:B7").Value
- On Error Resume Next
- Set objOutlook = CreatObject("Outlook.Application")
- Set objOutlookMsg = objOutlook.createitem(olmailitem)
- With objOutlookMsg
- Set objOutlookRecip = .Recipients.Add(RecipList)
- objOutlookRecip.Type = olTo
- .Subject = SubjectName
- .Body = " "
- If Not IsMissing(AttachmentPath) Then
- Set objOutlookAttach = .Attachments.Add(AttachmentPath) & "\" & Array(Filename)
- End If
- For Each objOutkRecip In .Recipients
- objOutlookRecip.Resolve
- If Not objOutlookRecip.Resolve Then
- Exit Sub
- End If
- Next
- .Display 'Display the message
- End With
- Set objOutlookMsg = Nothing
- Set objOutlook = Nothing
- End Sub