Hi all.
I have inherited a macro that gathers information and then sends an email if certain criteria are met with Outlook desktop. The system have been working flawlessly for sometime. We have noticed that the macro stops only when our windows 10 PC locks or goes to sleep. I did some researched and it appear the sendkeys won't allow email to be sent when the PC is locked.
I read somewhere that I could use windows scheduler to create a process to send at certain intervals. Because of our group policys on our system, Scheduler is disabled. Besides, we need the macro to send emails immediately.
Is there another method that I can use to get emails to be send whether or not the PC is locked? Below is the working code stored in an excel class module.
'---------------------------------------------------------------------------------------------------------------------
'Class: Email
'
' Description: Holds information and methods used to create and send emails.
' Instructions: Create an email class object, assign variables, and use methods.
' Using the class helps standardize the look of each email and makes updating easier.
'---------------------------------------------------------------------------------------------------------------------
'***********************************************************************************
'EMAIL PROPERTIES
'***********************************************************************************
Private pTo As String '"To" line
Private pFrom As String '"From" line
Private pCC As String '"CC" line
Private pBCC As String '"BCC" line
Private pMessageTitle As String 'Header of the message; first line of the body.
Private pBody As String 'Body of the message.
Private pSubject As String 'Subject
Private pCopyAdmins As Boolean 'Copy the portal admins on the message? (Resets, etc.)
Public Property Get ToLine() As String
ToLine = pTo
End Property
Public Property Let ToLine(Value As String)
pTo = Value
End Property
Public Property Get Subject() As String
Subject = pSubject
End Property
Public Property Let Subject(Value As String)
pSubject = Value
End Property
Public Property Get FromLine() As String
FromLine = pFrom
End Property
Public Property Let FromLine(Value As String)
pFrom = Value
End Property
Public Property Get CCLine() As String
CCLine = pCC
End Property
Public Property Let CCLine(Value As String)
pCC = Value
End Property
Public Property Get BCCLine() As String
BCCLine = pBCC
End Property
Public Property Let BCCLine(Value As String)
pBCC = Value
End Property
Public Property Get MessageTitle() As String
MessageTitle = pMessageTitle
End Property
Public Property Let MessageTitle(Value As String)
pMessageTitle = Value
End Property
Public Property Get Body() As String
Body = pBody
End Property
Public Property Let Body(Value As String)
pBody = Value
End Property
Public Property Get CopyAdmins() As Boolean
CopyAdmins = pCopyAdmins
End Property
Public Property Let CopyAdmins(Value As Boolean)
pCopyAdmins = Value
End Property
'***********************************************************************************
'EMAIL METHODS
'***********************************************************************************
'Create an Outlook Email object.
Public Sub CreateEmail()
Dim oLookApp As Object, oLookMail As Object, btime As String, ptype As String
Application.DisplayAlerts = False
On Error GoTo EmailError
Set oLookApp = CreateObject("Outlook.Application")
Set oLookMail = oLookApp.CreateItem(0)
With oLookMail
.To = pTo
If pFrom <> "" Then
.SentOnBehalfOfName = pFrom
End If
If pCC <> "" Then
.CC = pCC
End If
If pBCC <> "" Then
.BCC = pBCC
End If
If CopyAdmins = True Then
.CC = PortalAdminAddr
End If
.Subject = pSubject
.Body = pMessageTitle & vbLf & vbLf & pBody
.display
.send
End With
Exit Sub
EmailError:
MsgBox "Cannot open Outlook due to an error. Please contact System Admin." & vbCrLf & _
"Thank you.", vbOKOnly, "Outlook Error"
Application.DisplayAlerts = True
End Sub
'Send email
Public Sub SendEmail()
SendKeys "%{s}", True
End Sub
Bookmarks