Auto Send Email Data from Excel using Outlook

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hi I was wondering if the VBA code to auto run without opening the workbook/worksheet for the code to work?i know that the event is when open.But is there a way to make it run without opeing?
VBA Code:
Private Sub Workbook_Open()
    
    Dim i As Long
    Dim OutApp, OutMail As Object
    Dim strto, strcc, strbcc, strsub, strbody As String
    
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
   
    
    
    For i = 2 To Range("C65536").End(xlUp).Row
        If Cells(i, 6) <> "Y" Then
            If Cells(i, 3) - 7 < Date Then
                Set OutMail = OutApp.CreateItem(0)
                strto = Cells(i, 4).Value 'email address
                strsub = "Project " & Cells(i, 2).Value & " is due on Due date " & Cells(i, 3).Value 'email subject
                strbody = "Dear " & Cells(i, 1).Value & vbNewLine & "please update your project status" 'email body
                
                With OutMail
                    .To = strto
                    .Subject = strsub
                    .Body = strbody
                    .Send
                    '.display
                                    
                End With
                On Error Resume Next
                Cells(i, 5) = "Mail Sent " & Now()
                Cells(i, 6) = "Y"
                
            End If
        End If
    Next
    
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 

Attachments

  • EMAILAUTOSEND.jpg
    EMAILAUTOSEND.jpg
    82.2 KB · Views: 41

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Take this code from Workbook_Open and put it in standard module, for ex. Module1, as Sub SendEmail().
Then open Notepad and create file RunWithourXL.vbs
VBA Code:
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'\Path\Into\Your\File\XLFileWithThisMacro.xlsm'!Module1.SendEmail"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
WScript.Quit

save it and you can run it without opening excel.
 
Upvote 0
Take this code from Workbook_Open and put it in standard module, for ex. Module1, as Sub SendEmail().
Then open Notepad and create file RunWithourXL.vbs
VBA Code:
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'\Path\Into\Your\File\XLFileWithThisMacro.xlsm'!Module1.SendEmail"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
WScript.Quit

save it and you can run it without opening excel.
Just to clarify,ill moved my code to module?and create a notepad with your code and name it RunWithourXL.vbs?
 
Upvote 0
Yes. Excel file with code in module and pure txt file (with vbs extension, created in notepad)
If you done it correctly double click on this vbs file runs vba without opening this xlsm file.
 
Upvote 0

Forum statistics

Threads
1,223,516
Messages
6,172,777
Members
452,477
Latest member
DigDug2024

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