• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sending email in outlook from a specific account

inet

New Member
I am trying to send mail from a specific account. This account in Outlook is not the default account. Really I have no idea what I am doing but am using this code that I found on http://stackoverflow.com/questions/...ow-to-choose-which-account-to-send-email-from. I need to build credits to ask a question there and therefore my post here. This code is also written for MS Access. I am using it in Excel.

On line
Code:
Set olAccount = oApp.Account
I get a compile error "Assignment to constant not permitted"

Here follows the code

Code:
Sub sendEventMail(em As String, ccc As String, Subj As String, desc As String)

    On Error Resume Next
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.CreateItem(olMailItem)
    Set olAccount = oApp.Account
    Set olAccountTemp = oApp.Account
    Dim foundAccount As Boolean
    Dim strFrom As String
    strFrom = "coastal@xtraordinarywomen.co.za"

    foundAccount = False
    Set olAccounts = oApp.Application.Session.Accounts
    For Each olAccountTemp In olAccounts
        Debug.Print olAccountTemp.smtpAddress
        If (olAccountTemp.smtpAddress = strFrom) Then
            Set olAccount = olAccountTemp
            foundAccount = True
            Exit For
        End If
    Next

    If foundAccount Then
        Debug.Print "ACCT FOUND!"
        With oMail
            .To = em
'            .Body = "Message!"
            .htmlBody = desc
            .Subject = Subj
            Set .SendUsingAccount = olAccount
        End With
    Else
        Debug.Print "No acct found"
    End If

    Set oApp = Nothing
    Set oMail = Nothing
    Set olAccounts = Nothing
    Set olAccount = Nothing
    Set olAccountTemp = Nothing
End Sub
__________________________________________________________________
Mod edit : post moved to appropriate forum
 
My mentor David Wood from AdvancedApps has helped me out here. I am sharing it. In this case what makes it much easier is that the user can select the email address rather than account number. sendCaller loops through the accounts until it finds this email address. From there on it will call sendFile from where the message will be send.


Code:
Sub sendCaller()
'creates outlook application
'chooses an email address and finds the corresponding account number

    Dim OutApp As Object
    Dim i As Integer, accNo As Integer

    Set OutApp = CreateObject("Outlook.Application")
    emailToSendTo = "name@domain.com"  'put required email address

'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 accNo = i
    Next i
   
    sendFile accNo
   
End Sub


Code:
Sub sendFile(accountNo As Integer)
    Dim OutApp As Object
    Dim OutMail As Object


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

   
    With OutMail
       
        .To = "recipient@domain.com"
        .Subject = "Test"
        .Body = "Body"
        Set .SendUsingAccount = OutApp.Session.Accounts.Item(accountNo)
        .Send
    End With
End Sub
 
Back
Top