SendUsingAccount is not working (1 Viewer)

RogerCooper

Registered User.
Local time
Today, 07:00
Joined
Jul 30, 2014
Messages
277
I discovered that I did not have the proper object library. So for early binding, I have the following code:

Code:
Sub doEmailOutlook()
Dim oLook As Object
Dim oMail As Outlook.MailItem
Dim olAccount As Variant
Dim olAccounts As Variant
Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
Set olAccount = oLook.account
Set olAccounts = oLook.application.Session.accounts
Set olAccountTemp = oLook.account
Dim strEmail As String
Dim strMsg As String
Dim foundAccount As Boolean
Dim strFrom As String
foundAccount = False
strFrom = "[EMAIL="ar@spectroline.com"]ar@spectroline.com[/EMAIL]"
Set olAccounts = oLook.application.Session.accounts
     For Each olAccountTemp In olAccounts
        If (olAccountTemp.smtpAddress = strFrom) Then
            Set olAccount = olAccountTemp
            foundAccount = True
            Debug.Print olAccountTemp.smtpAddress
            Exit For
        End If
    Next

This fails on "Set olAccount = oLook.account" with the error "object doesn't support this property or method". What I am doing wrong?
 

isladogs

MVP / VIP
Local time
Today, 14:00
Joined
Jan 14, 2017
Messages
18,186
Which library have you now added?

As this code is nothing like the CDO code I use, I will leave others to answer your question.
However, I suggest you post the entire procedure.
 

RogerCooper

Registered User.
Local time
Today, 07:00
Joined
Jul 30, 2014
Messages
277
I finally was able to make this work.

Code:
Function Test2()
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)
    With OutMail                                 ' This creates a blank email and captures the users default signature.
        .BodyFormat = 2                          '2            'olFormatHTML
        .display
    End With
      
    With OutMail
        .To = "[EMAIL="rogercoop@aol.com"]rogercoop@aol.com[/EMAIL]"
        .CC = ""
        .BCC = ""
        .Subject = "test 2"
        .SendUsingAccount = OutApp.Session.accounts.Item(2)
        '.From 2
        '.Attachments.Add (VarAttachFile)
        '.HTMLBody = "test"
        .Send
        '.ReadReceiptRequested = False
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
End Function
 

Minty

AWF VIP
Local time
Today, 14:00
Joined
Jul 26, 2013
Messages
10,354
Roger - Glad you fixed it - but be aware that if someone else runs this on another computer, this line;
Code:
   .SendUsingAccount = OutApp.Session.accounts.Item(2)

Picks account 2 from YOUR machines outlook, this could be a completely different email on someone else's machine. That's why you really should interrogate the local machines accounts and get the correct account number to use. As per my original post.
 

RogerCooper

Registered User.
Local time
Today, 07:00
Joined
Jul 30, 2014
Messages
277
When I tried searching through the accounts, I couldn't get it to work. There may be a more generalized solution that does work, but I don't need to find it. What I think made the difference is this code:
Code:
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:00
Joined
Sep 21, 2011
Messages
14,042
FWIW I found this thread while trying to help another user in this thread https://www.access-programmers.co.uk/forums/showthread.php?t=304729

I used your code and Minty's function, as my test email account was third in the list
I amended Minty's code to use the name of the account and not the email address as the parameter for it is AcctToUse, but the logic is the same.

Code:
Public Function ListEMailAccounts(AcctToUSe As String) As Integer
    Dim OutApp As Object
    Dim i As Integer
    Dim AccNo As Integer
    Dim emailToSendTo As String
    
    Set OutApp = CreateObject("Outlook.Application")
    'emailToSendTo = "epsteel@gmail.com"                    'put required email address
    AccNo = 1
    'if smtp address=email we want to send to, acc no we are looking for is identified
    For i = 1 To OutApp.Session.Accounts.Count
        'Uncomment the Debug.Print command to see all email addresses that belongs to you
Debug.Print "Acc name: " & OutApp.Session.Accounts.Item(i) & " Acc number: " & i & " , email: " & OutApp.Session.Accounts.Item(i).SmtpAddress
        'If OutApp.Session.Accounts.Item(i).SmtpAddress = emailToSendTo Then
        If OutApp.Session.Accounts.Item(i).DisplayName = AcctToUSe Then

            AccNo = i
            Exit For
        End If
    Next i
    ListEMailAccounts = AccNo
    Set OutApp = Nothing
End Function
 

evillarrealca

New member
Local time
Today, 09:00
Joined
Mar 23, 2020
Messages
15
Hi Minty, just want to let you know that I just used the code and it works perfectly!. This was great since I couldn't make .SentOnBehalfOfName to work.
 

Users who are viewing this thread

Top Bottom