Emailing Random PIN numbers

bowlesstan

New Member
Joined
Dec 7, 2016
Messages
6
Hi this is my first post, I'm sorry if this has been asked before. Ok here goes.

Every evening at Approx 23:00 We send an email to our security depts with a daily pin, this is a random 4 digit code which which will be used code in our door access. We make up the numbers on a daily basis and Email these to three different internal departments. How would I go about getting excel to do this via a macr or something similar. Or am I just whistling in the wind.
Any help would be appreciated
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This is doable, but first, what email software are you using to send these messages?
 
Upvote 0
The below should work:

Code:
Private Sub send_pin()

Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

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

strbody = CInt(((9999 - 1000) * Rnd) + 1000)

On Error Resume Next

With OutMail
    
    .To = "someone@somewhere.com"
    .Subject = "Subject goes here"
    .Body = strbody

.Send

End With

On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub
 
Last edited:
Upvote 0
Sorry for being a noob but how do I execute this VBA script I am completely new to excel and VBA Scripting. Thanks
 
Upvote 0
Hi thanks for the reply - I've added a Click Box to execute and able to send For example a pin with subject Daily A PIN and a random 4 digit number to a number of recipients. Ok so how do I click on the box in excel which I've aptly named DAILY PIN do the same but this time send a random number with a different subject i.e. DAILY A PIN , DAILY B PIN and DAILY C PIN and each with different recipients ONE CLICK DOES ALL. Any help or advice would be appreciated.
 
Upvote 0
Try something like this:

Rich (BB code):
Private Sub send_pin()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim arrTo As Variant
    Dim arrSubject As Variant
    Dim intLp As Integer
    
    arrTo = Array("me1@email.com", "me2@email.com", "me3@email.com")
    arrSubject = Array("PIN# 1", "PIN# 2", "PIN# 3")

    For intLp = 0 To 2
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        strbody = CInt(((9999 - 1000) * Rnd) + 1000)
        
        On Error Resume Next
        
        With OutMail
            
            .To = arrTo(intLp)
            .Subject = arrSubject(intLp)
            .Body = strbody
        
        .Send
        
        End With
        
        On Error GoTo 0
        
        Set OutMail = Nothing
        Set OutApp = Nothing
    Next intLp


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,170
Members
448,870
Latest member
max_pedreira

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