not sure how to pass a variable from my form to vba code

Here is the code:
Dim strEmail As String
Dim strMsg As String
Dim oLook As Object
Dim oMail As Object

Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
With oMail
    .ReplyRecipients.Add "vguerra@rigfinancialsolutions.com"
    .To = "'Me.EmailCC'"
    '.body = sBody
    .HTMLBody = sBody
    .Subject = "'Me.EmailSubject'"
    If sFile <> "" Then
        .Attachments.Add (sFile)
    End If
    If bFileAttach = True Then
        .Attachments.Add (CurrentProject.Path & "\YourFile.pdf")
    End If
    .Display
    If bPreview = True Then
        .Display
    Else
        .Send
    End If
End With

If bPreview = False Then
    Set oMail = Nothing
    Set oLook = Nothing
End If

I need to be able to send information from my access forms to this code.
for example.
.to = me.emailto
.body = me.emailmessage
VGuerra67Asked:
Who is Participating?
 
Dale FyeConnect With a Mentor Commented:
so does:

    With omail
        .To = Forms("frmOutlook").txt_MailTo
        .Subject = "automation test"
        .Body = "This is a test"
        .Send
    End With

Open in new window

0
 
Bill PrewCommented:
If the code you showed is located in form trigger logic, then you need to removed the quotes, for example change:

    .To = "'Me.EmailCC'"

to

    .To = Me.EmailCC


»bp
0
 
Dale FyeCommented:
Where is this code running from?  Is it in a forms code module or in a standard code module?  If in a forms code module, you should be able to simply use something like you have depicted:

.To = me.EmailTo

But if this is in a standard code module, not behind a form, then you need to specifically refer to the form with something like:

.to = forms("formName").EmailTo
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
VGuerra67Author Commented:
i get the following error message.
" Method 'To' of object'_mailitem' failed
0
 
Bill PrewCommented:
Do you have Outlook installed?  Did it load when you hit the CreateObject line?  Were there any errors didplayed from Outlook?


»bp
0
 
VGuerra67Author Commented:
yes outlook is installed and running no error message from outlook
0
 
Dale FyeCommented:
the following works for me in a subroutine:

    Dim oOutlook As Outlook.Application
    Dim omail As Outlook.MailItem
   
    On Error GoTo ProcError
   
    Set oOutlook = GetObject(, "Outlook.Application")
    Set omail = oOutlook.CreateItem(olMailItem)
    
    With omail
        .To = "myEmail.@emailserver.com"
        .Subject = "automation test"
        .Body = "This is a test"
        .Send
    End With

Open in new window

So, can you post your current code?
0
 
VGuerra67Author Commented:
  1. Private Sub EmailTaskCmd_Click()
  2. 'DoCmd.SendObject acSendNoObject, , acFormatHTML, "client@msn.com", "client2@msn.com", "client3@smg.com", "subject", "body", 1
  3. Dim strEmail As String
  4. Dim strMsg As String
  5. Dim oLook As Object
  6. Dim oMail As Object
  7. Set oLook = CreateObject("Outlook.Application")
  8. Set oMail = oLook.CreateItem(0)
  9. With oMail
  10.    .ReplyRecipients.Add "vguerra@rigfinancialsolutions.com"
  11.    '.To = Forms(Forms!Ctsk_ApptFrmUP_EmailFrm).EmailTo
  12.    '.To = Forms!Ctsk_ApptFrmUP_EmailFrm.EmailTo
  13.    .To = Forms("Ctsk_ApptFrmUP_EmailFrm").
  14.    '.body = sBody
  15.    .HTMLBody = sBody
  16.    .Subject = Forms("CTsk_ApptFrmUP_EmailFrm").EmailSubject
  17. If sFile <> "" Then
  18.        .Attachments.Add (sFile)
  19.    End If
  20.    If bFileAttach = True Then
  21.        .Attachments.Add (CurrentProject.Path & "\YourFile.pdf")
  22.    End If
  23.    .Display
  24.    If bPreview = True Then
  25.        .Display
  26.    Else
  27.        .Send
  28.    End If
  29. End With
  30. If bPreview = False Then
  31.    Set oMail = Nothing
  32.    Set oLook = Nothing
  33. End If
  34. Exit Sub
  35. doEmailOutlookErrExit:
  36.    Exit Sub
  37. doEmailOutlookErr:
  38.    MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
  39.    Resume doEmailOutlookErrExit
  40. End Sub
0
 
VGuerra67Author Commented:
I think that the problem is that my variables are not available once i set oOutlook = GetObject(, "Outlook.Application")
prior to that statement, I can do a debug.print and get my variable.
0
 
VGuerra67Author Commented:
thanks.  it was actually a type
0
 
Dale FyeCommented:
"it was actually a typo "

LOL, we all know about those!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.