Upload attachment to Outlook as relative path - vba

I have code that creates a PDF of a sheet and saves it to a relative path. I'm then trying to upload that PDF to an email that displays (not sends). I want this all to happen at once as I click a button (I've assigned a macro to run both macros to this button).
However, I'm having trouble getting the attachment to work. Is it possible to save the attachment as a relative path? I'm doing it this way because the code needs to be standardized so I can use it on quite a few spreadsheets just by copying and pasting.
The specific code I'm using for the email is below.
On Error Resume Next
With OutMail 'Todos dados do email
.to = ""
.CC = ""
.BCC = ""
.Subject = "" & CompanyName & " - Invoice - " & Data & ""
.Body = "Dear ," & Chr(10) & Chr(10) & "Please, find enclosed the invoices regarding the (COLOQUE ACORDO AQUI) between our companies."
.Attachments.Add 'Adicionar Attachments
'.Attachments.Add ("C:\test.txt")
.Display '.Display para mostrar o email
End With

This is how Microsoft recommends it be done.
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\Test.doc", _
olByValue, 1, "Test"
myItem.Display
See here: Attachments.Add Method

Related

Is there a way to create a new Outlook email from Access 2002 without using the SendObject command?

I have a client that is using Access 2002 because it allows Replication. He is using this on Windows 10 with Outlook from Office 365.
The goal is to create a new email with all of the info filled in and attach a scanned proposal so that my client can review the email, make any changes that he wants and then send it.
In Access, the SendObject command creates and opens a plain text email and while this email is open my Outlook macro to scan a document and attach it to the email will not run.
So I would like to create a new Outlook email from Access that allows me to run my Outlook macro.
Or if I could get Access 2002 to create an email and attach the scanned document to it, I think I could get by with using msgboxes to verify specific items.
Below is the Access macro with the SendObject command followed by the Outlook macro.
Private Sub EmailProposal_Click()
'Access macro.
Dim stDocName As String
Dim stEmailAddress As String
Dim stSubject As String
Dim stMessage As String
stDocName = "rptProposal"
stEmailAddress = Forms!RequestForm!EmailAddress.Value
stSubject = "PROPOSAL"
stMessage = "Your proposal is attached." & vbCrLf & vbCrLf & "If you have any questions, please call us."
'Email the proposal.
DoCmd.SendObject acReport, stDocName, acFormatRTF, stEmailAddress, , , stSubject, stMessage
End Sub
Sub Scan()
'Outlook macro.
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
On Error Resume Next
Dim objCommonDialog As WIA.CommonDialog
Dim objImage As WIA.ImageFile
Dim strPath As String
Set objCommonDialog = New WIA.CommonDialog
'This shows the dialog box. I'd rather tell it what to do instead of having to manually choose each time.
Set objImage = objCommonDialog.ShowAcquireImage
strPath = Environ("TEMP") & "\TempScan.jpg" 'Save the scan.
If Not objImage Is Nothing Then
objImage.SaveFile strPath ' save into temp file
On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
ActiveInspector.WordEditor.Application.Selection.Inlineshapes.AddPicture strPath 'Insert into email. I want to attach it instead.
End If
End If
Kill strPath
Else
MsgBox "The Scan macro in Outlook did not find a document." & vbCrLf & vbCrLf & _
"Please place the proposal in the printer so it can be scanned.", vbOKOnly
End If
lbl_Exit:
Set objImage = Nothing
Set objCommonDialog = Nothing
Exit Sub
ErrHandler:
Beep
Resume lbl_Exit
End Sub
It seems you just need to automate Outlook for sending out emails with the required content set up. Take a look at the following articles that give you the basics of Outlook automation:
Automating Outlook from a Visual Basic Application
Automating Outlook from Other Office Applications
Sub Send_Mail_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "eugene#astafiev.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Automation email in Excel

I am trying to design an excel file that will help my company with recruitment.
The task is to create a sheet for candidates that the company speaks to, we will record all the records for the candidates including their first name, last name, mobile and email address. You can see a screenshot of how everything looks here: https://imgur.com/gallery/tvAIx
As you can see there are columns for when the company speaks to the candidate and when he sends his CV to us. At the end there is also a "CV reminders" column. It has the following code =IF(ISBLANK(F2), HYPERLINK("mailto:" & D2 & "?subject=" & $O$3 & "&body=" & $P$3, "Send reminder"), "All good")
The idea is so that if CV has not been received yet, you can press the cell and it will generate a reminder email for the candidate. I want to make all the process autonomous so that it can pick out the candidate name from the relevant cell and send him a generic email like:
"Hi name from cell,
Hope you are well.
We have spoken with you on date from cell. Have you had a chance to review your CV yet? Do you have any questions?"
I am sure it is possible with VBA just don't know how. Thank you.
You should be able to handle basic VBA usage in order to achieve this.
Below is the VBA code that sends an Outlook e-mail message for Office 2000-2016. Source is http://www.rondebruin.nl
You may put the code in the SelectionChange event of the requested cell(s) and change the Body, SendTo etc. portions according to your needs. (Appearently in your case, SendTo address and some parts of Body will come from particular cells on the row of your selected cell)
Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

using vba to send a chart in html form through outlook

I am using vba to send a chart in an html formatted email to outlook. The issue is only I am able to see the chart, none of the recipients can view it.
Fname = Environ$("temp") & "\Chart1.jpg"
ActiveWorkbook.Worksheets("eod summary").ChartObjects("Chart 1").Chart.Export _
Filename:=Fname, FilterName:="JPG"
With OutMail
.To = "first.lastname#aol.com"
.CC = ""
.Subject = h
.Attachments.Add Fname
s = "<p><img src=" & Fname & "></p>"
s = "<HTML><BODY><CENTER>" & s & "</CENTER><HTML><BODY>"
.HTMLBody = RangetoHTML(rng) & s & RangetoHTML(rng2)
.Display
.Send
End With
I am saving the file to my temp folder. There is a shared server that the recipients use, however not everyone refers it as the same drive (Z drive for some G drive for others). Therefore, I was unsuccessful when I tried retrieving the image from that filepath.
any help greatly appreciated!

Using VBA to send emails based on adjacent conditions

I'm a new VBA user and am trying to accomplish what I've described in the title using the code below.
I think it has something to do with creating dims specifically for cc/bcc/and to, but I'm not quite sure. in one column is a list of emails that have been filtered for based on specific conditions and in the column right next to it is either "" "cc" or "bcc". If it's blank, then it goes into "to" if it's cc" it goes into the .CC field etc. etc.
Sub SendList()
'DIM
Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim CurrFile As String
Dim emailRng As Range, cl As Range
Dim sTo As String
'SET
Set emailRng = ActiveSheet.Range("E3:E100").SpecialCells(xlCellTypeVisible)
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
'UPDATE WORKBOOK BEFORE SENDING
ActiveWorkbook.Save
CurrFile = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
'Need to find a way to automate to TO CC and BCC
With olMail
.To = sTo
.CC = ""
.BCC = ""
.Subject = "Audit Report XYZ" & " " & "-" & " " & Date
.Body = .Body & "Test" & vbCrLf & "Test2" & vbCrLf & "Test3"
.Attachments.Add "C:\Users\uq050e\Downloads\anyfile.xlsx" 'An audit report
.Display '.Send
End With
Set olMail = Nothing
Set olApp = Nothing
End Sub
It looks like the problem is not with Outlook VBA, but with reading the Excel's content. I'd suggest learning VBA and Excel a bit first, see Getting Started with VBA in Excel 2010.
You can use the Text property of the Range class to get the text for the specified object/cell.

Excel email reminder to outlook when excel gets updated

Ok , so, anyone know if it is possible to send an email reminder from excel to outlook automatically every time the excel sheet gets updated? If so, how that part of the macro would look like? I cant find it online anywhere.
The following code seems to work.
Right click on a worksheet and select 'View code'
and select Change event for Worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "someone#someemail.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub