Automate emailing current workbook to end users

Robert_Conklin

Board Regular
Joined
Jun 19, 2017
Messages
173
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I currently have a workbook that employs userforms that my end users fill out to send in part maintenance requests. Once we receive the workbooks from the end users I process their requests into out SAP system. Once I process the data, I have to click the email icon in my quick reference bar to email the open workbook back to the end user.

Is it possible to use VBA to trigger an automatic email sent back to the end user with the open workbook as an attachment?
 
Maybe something along the lines of this will assemble the email addresses...
Code:
Sub Mail_workbook_Outlook()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim SendTo As String
    Dim fndPlant As Range
    Dim firstAddress As String
    
    With Sheets("Lists").Range("L:L")
        Set fndPlant = .Find(What:=Me.T_02.Value, _
                             LookIn:=xlValues, _
                             LookAt:=xlWhole, _
                             SearchOrder:=xlByRows, _
                             SearchDirection:=xlNext, _
                             MatchCase:=False)
        If Not fndPlant Is Nothing Then
            firstAddress = fndPlant.Address
            Do
                SendTo = SendTo & "," & fndPlant.Offset(, 2).Value
                Set fndPlant = .FindNext(fndPlant)
            Loop While Not fndPlant Is Nothing And fndPlant.Address <> firstAddress
        End If
    End With
    
    'remove leading comma from SendTo string
    SendTo = Mid(SendTo, 2)

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

On Error Resume Next
With OutMail
    .to = SendTo                '<-- you can add email addresses separated by comma
    .CC = "SPAR@flocorp.com"
    .BCC = ""
    .Subject = "Spare Parts Maintenance"
    .Body = "The parts have been placed on today's load sheet and will be processed by EOB today.  This data has also been placed on the repository file."
    .Attachments.Add (Application.ActiveWorkbook.FullName)
    .Display                            '<-- .Display will show the email first for review
    '.Send                              '<-- .Send will auto send email without review
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I get an error message that stated:

Compile error - invalid use of Me keyword

I thought that the field name T_02 needed to be bracketed, but I still received the same error message.
 
Last edited:
Upvote 0
.
The email macro : Mail_workbook_Outlook (because of the line "What:=Me.T_02.Value") requires it be pasted in the User Form. The term ME refers to the user form and
the term T_02.Value refers to the textbox on the user form.

Where did you paste the macro ?
 
Upvote 0
Or...
if for some reason you don't want or can't have that particular user form loaded when running the email macro,
you could use the afterupdate or exit event of T_02 to write its value to a cell on a sheet, then reference the cell value.
 
Last edited:
Upvote 0
I just replaced the code that was in the module. I gathered from what you said above, that the code needed to be set in the user form. I did so, but when I ran the code, it did not produce an email. The code referenced in post #3 worked great, but it did not fill in the To: section of the email. Which is what I am trying to accomplish.
 
Last edited:
Upvote 0
.
Bob

The quickest means to determine the issue is to post your workbook on a 'cloud' site. Lots of folks use DROPBOX.COM. Its free. Or there are others free as well: Google, Amazon, etc.

I'll take a look when the workbook is available for download.

Of course ....don't include anything in the project that is confidential (real email addresses, phone numbers, etc.)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top