Results 1 to 9 of 9
  1. #1
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2

    Unanswered: Select Account to send email in form

    I have the following code to send an email when a command button is clicked.

    Code:
    Private Sub Command24_Click()
    
    Dim myOb As Object
    Dim AutoSend As Boolean
    
    Set myOb = CreateObject("Outlook.Application")
    Set oMail = myOb.CreateItem(olMailItem)
    
         
    
    AutoSend = True
    
    With oMail
        
        .Body = "There is a Transport available on," & vbNewLine & Me.TBtransportdate.Value & " at " & Me.TBtransporttime.Value & " for " & Me.TBDuration.Value & " Hours." & vbNewLine & " If you are available for this transport please reply." & vbNewLine & " # " & Me.TBID.Value
        
         
        .Subject = "Available Transport"
        .BCC = "emailaddress"
       
        If AutoSend Then
            .Send
        Else
            .Display
        End If
    End With
    
        Set oMail = Nothing
      DoCmd.Close
    End Sub
    This works great. I now need to select a specific email account. Not having any luck.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Did you investigate the outlook api, properties and methods. On the face of it the session.accounts should get you in the right direction
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2
    healdem, I am having no luck with this.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    see https://msdn.microsoft.com/en-us/lib...code-snippet-1
    granted there is no VBA example there
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2
    I am having no luck figuring this problem out. What is the difference between these two lines of code. myob and OutApp

    Code:
    Set myOb = CreateObject("Outlook.Application")
    Code:
    Set OutApp = CreateObject("Outlook.Application")
    When I search for solutions to this problem I come up with differences like this and I don't know what to do with them.

    This post http://www.rondebruin.nl/win/s1/outlook/account.htm looks promising but I can't make it work. Getting frustrated.

  6. #6
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    differences like what?
    Code:
    Set myOb = CreateObject("Outlook.Application")
    SET Means that you are about to do soemthign with an object
    myOb is a variable which (presumably) has been defined/declared as an outlook object
    = means assign the value of the following to the former
    CreateObject is a function / method which creates (instantiates) an object
    ...of type Outlook.Application

    a variable should be declared, however I'd strongly recommend that you ALWAYS declare variables before first use (either set the option ion the version of Access or force it through code using OPTION EXPLICIT as the first or second lione of each and every form/report/code module) If you don't do this and allow variabels to be sued without declaration you can experience all manner of wierd errors and subtle problems as you think you are using the same variiabel but becuase you have spellt it differently elsewhere it ISN'T

    the code you posted is functionally identical, in both cases it instantiates a variable as an outlook object. the fact one is called myOb the other OutApp is a minor detail. to me stylistically calling the variable OutApp makes more sense as its immediately clear its an OUTlookAPPlication object but it doesn't matter whjat its actually called when you then refer to that object.

    bear in mind sample code posted on t'net may be for different versions of an object. so their sampole code may work for a specific version, several versions or all versions of Outlook. But generally core functions such as what you want have been there for a long long time, or if they have changsd there will be a newer way of doing this

    Bear in mind you may also need to explicitly set a reference to the outlook object
    Last edited by healdem; 03-23-16 at 08:36.
    I'd rather be riding on the Tiger 800 or the Norton

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    session.accounts.item(x) will return the email address of the x item in the collection
    oddly its a base 1 collection ie the first email address in the list is .item(1)

    referring to he code here:-
    when you create your actual email set the .SendUsingAccount property
    .SendUsingAccount = OutApp.Session.Accounts.Item(2) 'ie use the 2nd account
    concevibaly you could also use
    .SendUsingAccount = "Doug60M@mydomain.tld" 'presuming Doug60M@mydomain.tld is a valid email account already registered in Outlook. this approach is dangerous becuase its not picking up the actual registration within Outlook.
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    May 2014
    Posts
    35
    Provided Answers: 2
    Healdem, this does not work.

    .SendUsingAccount = "Doug60M@mydomain.tld"

    Give me a type mismatch error on the email address.

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    So read the documemtation for the sendusingaccount property...
    What does it STATE
    Besides which one thing you can guarantee is that 'doug60m@mydomain.tld' is NOT a valid email address
    Last edited by healdem; 03-23-16 at 05:48.
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •