Ok so this is the exact code I am using:
Sub ListOutlookAccounts()
Dim olApp As Object
Dim bWeStartedOutlook As Boolean
Dim lnOLAcc As Long
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
If olApp Is Nothing Then
MsgBox "Can't start Outlook", vbCritical
Exit Sub
Else
bWeStartedOutlook = True
End If
End If
On Error GoTo 0
For lnOLAcc = 1 To olApp.Session.Accounts.Count
Debug.Print olApp.Session.Accounts.Item(lnOLAcc) & " : This is account number " & lnOLAcc
Next lnOLAcc
If bWeStartedOutlook Then olApp.Quit
Set olApp = Nothing
End Sub
Sub TestSend()
Call SendReportUsingOLAccount("Company Noreply") 'the only way it would work was by account name not email
End Sub
Sub SendReportUsingOLAccount(strOLAcc As String)
Dim olApp As Object
Dim olMsg As Object
Dim bWeStartedOutlook As Boolean
Dim r As Long, lnOLAcc As Long
Dim s As String
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
If olApp Is Nothing Then
MsgBox "Can't start Outlook", vbCritical
Exit Sub
Else
bWeStartedOutlook = True
End If
End If
On Error GoTo ExitPoint
'Get account number for parsed account name:
For lnOLAcc = 4 To olApp.Session.Accounts.Count 'I change the 1 to 4 as that was the account number
If LCase(olApp.Session.Accounts.Item(lnOLAcc)) = LCase(strOLAcc) Then Exit For
Next lnOLAcc
If lnOLAcc > olApp.Session.Accounts.Count Then
MsgBox "Account " & strOLAcc & " not found!", vbCritical
GoTo ExitPoint
End If
For r = 6 To 1005
s = Range("B" & r).Value
If s <> "" Then
Set olMsg = olApp.CreateItem(0)
olMsg.To = s
olMsg.Subject = "Welcome"
olMsg.Body = "Hello, " & vbCrLf _
& "Thank you for becoming a member " & vbCrLf & vbCrLf _
& "Report Download: " & Range("J17").Value & vbCrLf _
& "Expiration: " & Range("I17").Text & vbCrLf & vbCrLf _
& "Thank you"
olMsg.Display
'olMsg.SendUsingAccount = olApp.Session.Accounts.Item(lnOLAcc)
'olMsg.Send
End If
Next r
Range("I17:J17").ClearContents
MsgBox "Emails have all been sent", vbInformation, "Company Sales"
ExitPoint:
On Error Resume Next
If bWeStartedOutlook Then olApp.Quit
Set olMsg = Nothing
Set olApp = Nothing
End Sub
here is the email list the first macro provides:
XXX : This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 4
XXX: This is account number 4
XXX: This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 4
XXX: This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 4
XXX: This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 4
XXX: This is account number 1
XXX: This is account number 2
XXX: This is account number 3
XXX: This is account number 4
XXX = An accounts name (EX: Company Noreply)
As you can see it doesn't give me any emails, it just gives me the account name and a number, and it repeats itself alot of times.
In the sendtest macro, if I didn't put account name instead of the email as you said to, it just gives me the email was not found error.
Even though I changed the account number it uses, it still goes to the default.
What do you recommend doing?