Solved Using GMail Accounts (1 Viewer)

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:34
Joined
Sep 12, 2006
Messages
15,656
I am trying to send emails in VBA by using a gmail account, rather than my default account.

I am using code to loop round my email accounts, but it doesn't display any of the 3 gmail accounts I have added to my outlook account.

It finds 5 POP accounts and an M/S Exchange Account, but it doesn't then show the IMAP accounts, which are the gmail accounts. See the PNG.
I tried changing the order of the gmail accounts to get them above the exchange account just in case, but it skips over them in the debug window.

It works correctly if I choose one the POP accounts as the sender.

I wasn't 100% sure my code was right, and getting to this point needed modification from the downloads I used, but this seems to iterate the collection correctly.
Is there maybe a larger collection than this one? Set olaccounts = MyOutlook.Session.Accounts

Can anyone help please?




Code:
select_outlook_account:
'we want to send emails from myemail.sendfrom
    
    Dim olaccounts As Object
    Dim olaccountTemp As Object
    
    Dim foundAccount As Boolean


    foundAccount = False   
    Set olaccounts = MyOutlook.Session.Accounts
    For Each olaccountTemp In olaccounts
        Debug.Print olaccountTemp.smtpAddress
        If (olaccountTemp.smtpAddress = MyEmail.sendfrom) Then
            foundAccount = True
            Set MyMail.sendusingaccount = olaccountTemp
            Return
        End If
    Next
    Set olaccountTemp = Nothing
    Set olaccounts = Nothing

Return
 

Attachments

  • Outlook Accounts.PNG
    Outlook Accounts.PNG
    3.6 KB · Views: 39

Gasman

Enthusiastic Amateur
Local time
Today, 10:34
Joined
Sep 21, 2011
Messages
14,301
I have only ever used POP with GMail accounts.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:34
Joined
Sep 12, 2006
Messages
15,656
I just set them up automatically, and that's what it did (IMAP). I will change one to POP and see if it makes a difference, although I'm not exactly sure how to do it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:34
Joined
Sep 21, 2011
Messages
14,301
Do not change one, not sure you even can TBH, just set up another as POP. Just go through the wizard.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:34
Joined
Sep 21, 2011
Messages
14,301
I can get to list the only IMAP account I have, but it is not a Gmail account?

NTL World : This is account number 1
JAG Property (IMAP) : This is account number 2
EPSteel HA : This is account number 3
Gmail : This is account number 4
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:34
Joined
Sep 21, 2011
Messages
14,301
I just added a dummy IMAP Gmail account.
I had to set 2 factor authetication and an app password

NTL World : This is account number 1
JAG Property (IMAP) : This is account number 2
EPSteel HA : This is account number 3
Gmail : This is account number 4
Gmail (IMAP) : This is account number 5
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:34
Joined
Sep 12, 2006
Messages
15,656
Infuriating

I was going to restart my PC, but decided to just close and restart Outlook, and that refreshed the collection and found the gmail accounts.
I think I've spent at least a couple of hours on this. :D

Tested, and it's working correctly, and issuing the emails now.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 10:34
Joined
Sep 21, 2011
Messages
14,301
Are you using my code to list the accounts?
No, I used something I had froma while back

I was going to restart my PC, but decided to just close and restart Outlook, and that refreshed the collection and found the gmail accounts.
Hmm, I just created the account and ran my code again. I did not need to close and reopen outlook? Then again I am on 2007, so perhaps they just changed a few things?
 

Users who are viewing this thread

Top Bottom