+ Reply to Thread
Results 1 to 15 of 15

Send Emails when PC is locked

Hybrid View

china99boy Send Emails when PC is locked 07-22-2019, 09:45 AM
6StringJazzer Re: Send Emails when PC is... 07-22-2019, 10:26 AM
china99boy Re: Send Emails when PC is... 07-22-2019, 02:07 PM
6StringJazzer Re: Send Emails when PC is... 07-22-2019, 04:18 PM
china99boy Re: Send Emails when PC is... 07-23-2019, 09:45 AM
6StringJazzer Re: Send Emails when PC is... 07-23-2019, 10:17 AM
china99boy Re: Send Emails when PC is... 07-23-2019, 10:45 AM
CK76 Re: Send Emails when PC is... 07-23-2019, 10:55 AM
china99boy Re: Send Emails when PC is... 07-23-2019, 11:21 AM
CK76 Re: Send Emails when PC is... 07-23-2019, 11:36 AM
china99boy Re: Send Emails when PC is... 07-23-2019, 11:40 AM
CK76 Re: Send Emails when PC is... 07-23-2019, 11:54 AM
china99boy Re: Send Emails when PC is... 07-23-2019, 01:39 PM
nordicdust Re: Send Emails when PC is... 07-23-2019, 01:03 PM
CK76 Re: Send Emails when PC is... 07-23-2019, 01:43 PM
  1. #1
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Send Emails when PC is locked

    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

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Send Emails when PC is locked

    The code in Sub CreateEmail is already sending the email. Why would you need sub SendEmail that uses SendKeys to click ALT+s?
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    Sorry, that last piece of code is being used for something else. Still the sub CreateEmail, generates the email but won't send while the PC is locked.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Send Emails when PC is locked

    Try commenting out the .Display line. If that doesn't work I will try to set up a test that is like what you are doing.

  5. #5
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    I am so sorry.. I inherited this and this is not my strongest field at all. It looks like I was interpreting the code wrong. Below is the code that I missed. Looks like it was using both the CreateEmail and SendEmail sub. Perhaps this method was used to get around the outlook prompts. We decided to not use outlook at all and just use CDO method. I tested the CDO and that work on a test. Can you by chance help me incorporate the CDO method for this instead of using Outlook and perhaps we will be able to send while the system is locked?

    Private Function NewCaseToDatabase(ByRef oCase As cls_RMCase) ' As Boolean
        If oCase.IsEmpLoan = True Then
        
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " is an employee loan." & vbCrLf
            
            Dim oEmail As cls_Email
            Set oEmail = New cls_Email
            
            oEmail.ToLine = "lendingEmailAddress”
            oEmail.Subject = "New Employee Loan Application"
            oEmail.MessageTitle = "Employee Loan, Case #: " & oCase.CaseNo & vbCrLf
            oEmail.Body = "Case Data: " & vbCrLf & vbCrLf & oCase.CaseData
            oEmail.CreateEmail
            oEmail.SendEmail
             
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " was forwarded to the employee lending inbox." & vbCrLf
        
            Exit Function
        End If

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Send Emails when PC is locked

    You don't show the code that references CDO objects I don't know what you need. I have no experience using CDO for email so I would have to research it from scratch. I have sent out a request to see if someone else can help.

  7. #7
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    Thank you... It won't let me post CDO code because it said I could post anything with links which the code contains. Perhaps I can send another way?

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Send Emails when PC is locked

    What is the data type of oCase.CaseNo? Is it just a string or is it some other data type?

    Also, it's bit hard to help without knowing what type of smtp you are using (ex: Office365, gmail, in-house smtp server?).

    These details are needed as CDO does not use client (i.e. where these setting are already configured), and config must be set in the code.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  9. #9
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    Hi the caseNo is a numeric value and we are using in-house server.

    Thanks

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Send Emails when PC is locked

    Ok. Then Ron de Bruin's code will do what you need.

    See link.

    https://www.rondebruin.nl/win/s1/cdo.htm

    You will need to uncomment bock and set fields for config (smtpserver, smtpserverport).
    As well, you should check that the smtp sever allows for anonymous send (sendusing = 2).

  11. #11
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    That is the code I need help with incorporating into my existing code and didn't know how to do so. I tested it and it work to send in a separate workbook, but don't know how to replace it with mine without messing up things. Thanks

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Send Emails when PC is locked

    Did it work as is when you use his code? Without incorporating into your code. You should check that before you implement to existing code.

    If it does it would be something like... Note that it's independent from your EMail class module.
    Private Function NewCaseToDatabase(ByRef oCase As cls_RMCase)
    
        If oCase.IsEmpLoan = True Then
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " is an employee loan." & vbCrLf
            Dim iMsg As Object
            Dim iConf As Object
            Dim strbody As String
            Dim Flds As Variant
            Set iMsg = CreateObject("CDO.Message")
            Set iConf = CreateObject("CDO.Configuration")
    
            iConf.Load -1    ' CDO Source Defaults
            Set Flds = iConf.Fields
            With Flds
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Fill in your SMTP server here"
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                .Update
            End With
    
            strbody = "Case Data: " & vbCrLf & vbCrLf & oCase.CaseData
    
            With iMsg
                Set .Configuration = iConf
                .To = "lendingEmailAddress"
                .CC = ""
                .BCC = ""
                .From = "SenderEmailAddress"
                .Subject = "New Employee Loan Application"
                .TextBody = "Employee Loan, Case #: " & oCase.CaseNo & vbCrLf & vbCrLf & vbCrLf & strbody
                .Send
            End With
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " was forwarded to the employee lending inbox." & vbCrLf
            Set iMsg = Nothing
            Set iConf = Nothing
            Set Flds = Nothing
            Exit Function
        End If
    End Function

  13. #13
    Registered User
    Join Date
    06-28-2010
    Location
    Raleigh
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Send Emails when PC is locked

    I am so excited and grateful to you guys. The solution below worked. Thank you so much.
    Quote Originally Posted by CK76 View Post
    Did it work as is when you use his code? Without incorporating into your code. You should check that before you implement to existing code.

    If it does it would be something like... Note that it's independent from your EMail class module.
    Private Function NewCaseToDatabase(ByRef oCase As cls_RMCase)
    
        If oCase.IsEmpLoan = True Then
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " is an employee loan." & vbCrLf
            Dim iMsg As Object
            Dim iConf As Object
            Dim strbody As String
            Dim Flds As Variant
            Set iMsg = CreateObject("CDO.Message")
            Set iConf = CreateObject("CDO.Configuration")
    
            iConf.Load -1    ' CDO Source Defaults
            Set Flds = iConf.Fields
            With Flds
                .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Fill in your SMTP server here"
                .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                .Update
            End With
    
            strbody = "Case Data: " & vbCrLf & vbCrLf & oCase.CaseData
    
            With iMsg
                Set .Configuration = iConf
                .To = "lendingEmailAddress"
                .CC = ""
                .BCC = ""
                .From = "SenderEmailAddress"
                .Subject = "New Employee Loan Application"
                .TextBody = "Employee Loan, Case #: " & oCase.CaseNo & vbCrLf & vbCrLf & vbCrLf & strbody
                .Send
            End With
            ufPortalServices.txtIntAppLog.Value = ufPortalServices.txtIntAppLog.Value & "     " & oCase.CaseNo & " was forwarded to the employee lending inbox." & vbCrLf
            Set iMsg = Nothing
            Set iConf = Nothing
            Set Flds = Nothing
            Exit Function
        End If
    End Function

  14. #14
    Forum Contributor
    Join Date
    03-08-2018
    Location
    Denmark
    MS-Off Ver
    2016 for Windows
    Posts
    413

    Re: Send Emails when PC is locked

    The problem seems to be with windows 10.
    I have tried this at work and I keep getting errors no matter what I try.

    When the pc locks or sleep mode, it won't run.

    Read this for deeper insight.

    https://www.thespreadsheetguru.com/b...a-macros-daily


    https://support.microsoft.com/en-us/...tion-of-office

  15. #15
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Send Emails when PC is locked

    What's the error message and at which line do you get the error?

    Issue most likely lies in your network environment (Firewall, smtp server setting/permission) and not in the code.
    I've tested code, replacing your variables with hard coded value in my environment and was able to send.

    Now, PC lock/sleep is another matter.

    Typically speaking, I'd use some other process if that's the case. But I typically use Task Scheduler for something like that.
    If you don't have it you may need some other process... like python timeloop script, or maybe PowerShell script using timer job.

    EDIT: Ah, I see that you got it solved. You are welcome and thanks for the rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel vba to auto-send customer emails (duplicate emails issue)
    By nadz84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2015, 10:08 AM
  2. Send Emails once expiry date is reached, and generate report based on emails sent
    By demonicscorpion in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2014, 05:36 AM
  3. [SOLVED] Macro To Send Emails with PDF: Multiple Emails and PDF's
    By totoga12 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-19-2014, 06:13 PM
  4. Send outlook emails with message in cells to individual emails associated with them
    By abinayan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2011, 06:11 AM
  5. send emails to a list
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-15-2010, 07:27 AM
  6. Sending macro emails using excel: Send emails with their passwords.
    By loveisblind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2009, 03:16 PM
  7. send emails
    By hanasamo in forum Excel General
    Replies: 1
    Last Post: 08-02-2005, 02:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1