0

I am trying to send Outlook email using Microsoft Access.

In the email, I want three cells of data.

Email
Pin
FirstName

I also would like to change the "From" email address from my default address to that of a secondary address I maintain.

Private Sub Command7_Click()

    Dim Msg As String
    Dim strFrom As String
    Dim strSubject As String
    
    Msg = "Dear " & FirstName & ",<P> &" _
    "Below is your Personal Identification Number."<P> &" _
    "<P> &" _
    & Pin "<P> &" _
    "This PIN is unique to you."<P> &" _
    "<P> &" _
    "You must safeguard, and not let anyone have access to your pin."<P> &" _
    "<P> &" _
    "To initiate a wire, have your PIN available and call us at 555-555-5555."<P> &" _
    "<P> &" _
    "Questions? Please reply to this email, or call us at 555-555-5555."
    
    Dim O As Outlook.Application
    Dim M As Outlook.MailItem
    
    Set O = New Outlook.Application
    Set M = O.CreateItem(olMailItem)
    
    strFrom = "[email protected]"
    strSubject = "ENCRYPT - Personal Identification Number (PIN)"
    
    With M
        .BodyFormat = olFormatHTML
        .HTMLBody = Msg
        .To = Email
        .From = strFrom
        .Subject = strSubject
        .Display
    
    End With
    
    Set M = Nothing
    Set O = Nothing

End Sub

If I remark everything except the first line of the Msg, leaving

Msg = "Dear " & FirstName & ",<P> &" _

it will generate an email with the correct information, with the exception of the "From" field in the email.

When I add lines to my Msg string, I get various errors, including Syntax errors, and expected: end of statement errors.

5
  • 3
    In your further lines the concatenation operator (&) is within the text. This should be always like this "String1" & "String2". Don't forget the leading/trailing spaces. Commented Jul 11 at 3:35
  • 1
    And for the From email you should use .SentOnBehalfOfName = strFrom instead of .From Commented Jul 11 at 7:08
  • this might help answers.microsoft.com/en-us/msoffice/forum/all/… Commented Jul 11 at 13:06
  • Use .From with CDO method of sending email. Your code is not using CDO, it is Outlook automation and .From cannot be set - it is read only. Commented Jul 11 at 17:43
  • Actually, you might want .SendUsingAccount. Review accessforums.net/showthread.php?t=80945 Commented Jul 11 at 18:45

1 Answer 1

0

Your string concatenation is a hot mess. You've got extra quotes all over the place. I think that part of what's creating your confusion is the excessive use of line continuation trying to emulate what the text looks like in your email. Try this as single line of code:

Msg = "Dear " & FirstName & ",\<P\> Below is your Personal Identification Number.\<P\>\<P\> & Pin & "\<P\>This PIN is unique to you.\<P\>\<P\> You must safeguard, and not let anyone have access to your pin.\<P\>\<P\>To initiate a wire, have your PIN available and call us at 555-555-5555.\<P\>\<P\>Questions? Please reply to this email, or call us at 555-555-5555."

If you get this to work, then add in the line continuation if you want to. Personally, I rarely use line continuation, since it just adds complexity that usually isn't needed.

Sign up to request clarification or add additional context in comments.

1 Comment

I actually did get this to work without the line continuations. I'm just going to leave it as is, without the line continuations. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.