Attaching unsaved Excel file to email - excel

I have an Excel form for users to fill and send as an attachment (without having to save it locally on their computer).
The code works.
Dim Names()
Names = Array("testmail#gmail.com")
ActiveWorkbook.SendMail _
Recipients:=Names(), _
Subject:="Test subject"
I would like the email just to be created and not sent until the users have attached an additional file (found on their local computer).
I wrote the following code:
Dim olapp As Object
Dim olmail As Object
Dim wb As Workbook
Set olapp = CreateObject("outlook.application")
Set olmail = olapp.CreateItem(olMailItem)
Set wb = ActiveWorkbook
With olmail
.To = "testmail#gmail.com"
.Subject = "Test Subject"
.Body = ""
.Attachments.Add wb.FullName
.Display
My problem is that only the latest saved copy will be attached to the created email, and since the users will not have the form/Excel file stored locally on their computer, an empty form (or the last saved form) will be attached to the email.
Is there any way for an email to be created, with a copy of the workbook, but not to send it?

i tried this one-liner in the immediate window and it managed to send an unsaved file: Application.Workbooks("Book2").SendMail("my.email#company.com","Test Subject") you can use wb from your code instead of Application.Workbooks("Book2") in my example. Note that this will send the email, without the possibility to edit it.

Related

Is it possible to have an excel macro open outlook in browser and populate the fields of a new message as well as attach a file?

Question: Is it possible to create a macro that opens outlook in a web browser and populates the fields of a new message as well as attach a file? The outlook portion of the current macro only opens outlook in a browser.
ActiveWorkbook.FollowHyperlink Address:="https://outlook.office365.com/mail/**shared mailbox address**"
Background: I am trying to update an excel macro that currently saves a pdf of the sheet, opens the outlook application, fills out the necessary fields and attaches the saved pdf to the email. This macro has worked fine, but we have recently moved to using a shared mailbox to send the message. Now the users have encountered problems sending from the shared mailbox using the outlook application. The solution is to use outlook in the browser (edge), but the macro I currently have can only open outlook in the browser and requires the user to fill out all the fields and find and attach the saved pdf. There have been problems with this and I was hoping there was a way to automate the process like our old macro would.
Old macro:
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = ReportName
.CC = ""
.Subject = TempFileName
.Body = ""
.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
Well, Yes and No.
Yes, you can get VBA to do this... but No, it won't be remotely as easy as running it through the desktop application.
A workaround that still uses desktop application, is to
Give all users that need to send the email access to this inbox from their own desktop apps.
Use the ".SendUsingAccount" property
Sub ExampleSub()
Dim OLApp As Object
Dim NewMail As Object
Set OLApp = CreateObject("Outlook.Application")
Set NewMail = OLApp.CreateItem(0)
On Error Resume Next
With NewMail
.SentOnBehalfOfName = "MySecondaryAddress#Domain.com"
.To = "someaddress#gmail.com"
.CC = ""
.Subject = "Example Subject"
.Body = "Good Morning..."
'.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
End Sub

How to write a macro that send excel as a pdf to outlook web based not app based

I have a macro that creates a pdf and then send it to outlook to attach as a email but it tries to open the app instead of using the web based version. what code can I use to replace it with?
You need to save the workbook on the disk and then use the file path of the just saved file to add it as an attachment in Outlook. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. For example:
Sub AddAttachment()
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myItem = Application.CreateItem(olMailItem)
Set myAttachments = myItem.Attachments
myAttachments.Add "D:\Test.xlsx", _
olByValue, 1, "Test"
myItem.Display
End Sub
The code for automating Outlook desktop will not work for the browser out of the box.

Automate Outlook mail scheduler

I came across a code in Excel VBA which sends mail via Outlook based on given time ranges in the code. The "TO, CC, Subject and Mail Body" are all input in excel only.
The code works fine but .Send giving error 287-Application or object defined error. The code works well in my colleague's laptop, so I am guessing it is some setting error in my outlook or excel. I keep my outlook open while executing the code. The code is written below. Can anyone point to what might be wrong?
Sub Send_Email()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
'''''''''' Update Next ''''''''''''''
Call Update_Next_Schedule_Time
Application.OnTime sh.Range("K20").Value, "Send_Email"
''''''''''''''''''''''''''''''''''''''
Dim oa As Object
Dim msg As Object
Set oa = CreateObject("outlook.application")
Set msg = oa.createitem(0)
With msg
.To = sh.Range("C2").Value
.CC = sh.Range("C4").Value
.Subject = sh.Range("C6").Value
.Body = sh.Range("C8").Value
.display
.send
End With
End Sub
I believe this is due to your Outlook security policy. Check if it allows programmatic access. If not, and you cannot convince your IT department to make any change, then you should automate the opened outlook app rather that creating a new Outlook instance. Also, are you trying to send the email to yourself, by any chance?

Trying to create personalized emails with multiple attachments

Updated per suggestions from Jeeped:
I am looking for a method of creating a set of emails fitting the following parameters:
each email will be personalized to the recipient and based off a template letter set by my supervisor.
There will be a greeting line with their name and title, along with the names of the departments they oversee.
each email will have a set of documents specific to that recipient.
they should be saved to file for final inspection before they are sent.
column 5 that is not referenced in the code below is the column containing the department name.
The closest I have come is the following code:
Sub send_template_w/attachments()
On Error Resume Next
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.Mailitem
Set omail =.Createitem(olMailitem)
Dim I As Long
For i=2 To Range(“a100”).End(xlUp).Row
With omail
.Body = “Dear “ & Cells(i,1).Value
.To = Cells(i,2).Value
.CC = Cells(i,3).Value
.Subject = Cells(i,4).Values
.Attachments.Add Cells(i,6)
.Attachments.Add Cells(i,7)
.SaveAs Environ("HOMEPATH") &; "\My Documents\" & Cells(i,2).Value
End With
Next
End Sub
So far, this code will generate and save an email but what I want to do is use a present email template for these emails--either by adding the greeting at the beginning and department name into the body of the the email to be sent out. Can this be done through a word or Outlook document and if so, how?
Create a model of the mail. "Save As" to an .oft file. For example MyTemplate.oft
Instead of
Set omail =.Createitem(olMailitem)
there is
Set omail = o.CreateItemFromTemplate("C:\MyTemplate.oft").
To add the entries from the Excel sheet you could include unique placeholders in the body of the template then Replace with Excel values.

VBA open workbook attached to outlook template

Is there any way that one can open a workbook attached to an email template, edit, and save it prior to sending the message? I've created the mailitem object using Set Mesg = OutlookAp.CreateItemFromTemplate("C:\Template.oft") and I can see the attachment, but I can't see a way to open it thus far. If anyone has suggestions, or knows that this simply can't be done, I'm all ears.
Looks like I may have to save and edit the file prior to sending... Still open to ideas, but it looks like it simply isn't possible to open the attachment through VBA
I assume you are automating Outlook from Excel. This solution may work for you, but as you note it does rely on saving the attachment and re-attaching the manipulated version of the file. Assuming you can write the code which will "edit" the Workbook attachment, this should work for you.
Sub TestOutlookTemplate()
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim att As Outlook.Attachment
Dim templatePath As String
Dim tempFileName As String
Dim attWorkbook As Workbook
templatePath = "C:\users\david_zemens\desktop\Untitled.oft"
tempFileName = "C:\users\david_zemens\desktop\tempexcelfile.xlsx"
Set MyOutlook = CreateObject("Outlook.Application")
Set MyMail = MyOutlook.CreateItemFromTemplate(templatePath)
MyMail.Display
For Each att In MyMail.Attachments
If att.DisplayName Like "*.xls*" Then
att.SaveAsFile tempFileName
'Now that you have saved the file, delete the attachment
att.Delete
'Open the file
Set attWorkbook = Workbooks.Open(tempFileName)
'Perform manipulation on the file
attWorkbook.Sheets(1).Name = "Sheet ONE"
'Save fhe file
attWorkbook.Save
'Close the file
attWorkbook.Close
MyMail.Attachments.Add tempFileName
End If
Next
'Send your mail (make sure you have added a recipient
MyMail.Send
Set attWorkbook = Nothing
Set MyMail = Nothing
Set MyOutlook = Nothing
End Sub

Resources