Forum

VBA required to aut...
 
Notifications
Clear all

VBA required to auto save excel file on current users desktop and open an email with file attached in one step

6 Posts
3 Users
0 Reactions
275 Views
(@monish)
Posts: 4
Active Member
Topic starter
 
Hello all,
 
Just joined the forum and wondering if you would be so kind enough to assist me with the following challenge.
 
I've an excel form and managed to have a macro created that attaches the file to an email when I click on a particular field. The macro is as below. I need to expand the macro so that it saves the file 1st to any user's desktop (not within any folder or the macro could perhaps create a temporary folder automatically if this required), then opens an email with the file attached.
 
I'd really appreciate your assistance as it is very challenging trying to find a particular solution via internet search.
 
Macro so far
Sub Low_Priority_Mail_workbook_Outlook()

 
Dim OutApp As Object
Dim OutMail As Object
 
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
 
On Error Resume Next
    With OutMail
        .to = "[email protected]"
        .CC = ""
        .BCC = ""
        .Subject = "Form"
        .Body = "Hi," & vbNewLine & vbNewLine & "Please look into this request." & vbNewLine & vbNewLine & "Thanks"
        .Attachments.Add ActiveWorkbook.FullName
        .Display
   
End With
    On Error GoTo 0
 
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set objMsg = Nothing
 

End Sub

 
*******************************************************************************************************
Form layout
The macros are linked to the "Submit" buttons.
 
QAAAABJRU5ErkJggg==
 
Posted : 13/02/2020 6:24 am
(@monish)
Posts: 4
Active Member
Topic starter
 

Please find file attached to this query

 
Posted : 13/02/2020 6:32 am
(@debaser)
Posts: 837
Member Moderator
 

You can get the desktop path using:

 

createobject("Wscript.Shell").specialfolders("desktop")

 

so you could use:

 

dim savedFile as string

savedfile = createobject("Wscript.Shell").specialfolders("desktop") & application.pathseparator & activeworkbook.name

activeworkbook.savecopyas savedfile

 

then later use savedFile in the .attachments.add line instead of activeworkbook.fullname

 
Posted : 13/02/2020 6:50 am
(@monish)
Posts: 4
Active Member
Topic starter
 

Thank you Velouria, this worked fine. Am I correct in saying this would work for any user who is working on the file?

I also want the file to be saved including current date e.g.  Support_Ticket_Form 12Feb20.xlsm

The original file name is Support_Ticket_Form.xlsm so I'd like to add the current date each time before a file is submitted by email otherwise the latest one will update the previous one and user will loose the history of requests submitted.

What would be the VBA addition for this?

 
Posted : 13/02/2020 10:01 am
(@monish)
Posts: 4
Active Member
Topic starter
 

I'm running into an issue as the file is saved originally to desktop with VBA. So next time user opens the same file VBA is trying to override it while it is still open and get the Run-Time error '1004' as per attachment.Capture.JPG

I'm now thinking may be it is best not to save the file to desktop and once email is sent the file should be removed from desktop.

The email just needs to have the file attached after data already is entered in the file so it doesn't come blank after submission.

Any suggestions?

The VBA so far is as below:

 

Sub High_Priority_Mail_workbook_Outlook()

Dim savedFile As String

savedFile = CreateObject("Wscript.Shell").specialfolders("desktop") & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveCopyAs savedFile

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.to = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "Material Support Ticket Form - High Priority"
.Body = "Hi team," & vbNewLine & vbNewLine & "Please look into this request." & vbNewLine & vbNewLine & "Thanks"
.Attachments.Add savedFile

.Display

Application.DisplayAlerts = False
Application.ActiveWindow.Close SaveChanges:=False

End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
Set objMsg = Nothing

End Sub

 
Posted : 13/02/2020 11:08 am
(@ben13)
Posts: 4
New Member
 

Hi Monish,

Using ActiveWorkbook can get messy when renaming, saving, and deleting files.

I'd also recommend using a yyyymmdd date format if you want to be able to sort files easily in chronological order.

Plus - does the email recipient need to run macros? If not, I'd leave the VBA out of the attachment.

Further, saving to the user's desktop is fine if you're going to remove the file straightaway, but I'd use their temp environment just to make sure that the file is never sitting right in front of them tempting them to open it.

 

savedFile = Environ("temp") & "Support_Ticket_Form_" & format(date, "yyyymmdd") & ".xlsx"

 

You can then delete the file from the user's temp after it's attached to the email:

 

Kill savedFile

 

Does this line...

    Application.ActiveWindow.Close SaveChanges:=False

...close the file containing the VBA? If so, you should probably get rid of it.

 
Posted : 14/02/2020 2:46 am
Share: