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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 FyeOwner, Developing Solutions LLCCommented:
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
VGuerra67Author Commented:
i get the following error message.
" Method 'To' of object'_mailitem' failed
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

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 FyeOwner, Developing Solutions LLCCommented:
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
Dale FyeOwner, Developing Solutions LLCCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 FyeOwner, Developing Solutions LLCCommented:
"it was actually a typo "

LOL, we all know about those!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.