?
Solved

Access VBA to get email address from inputbox to pass data to outlook to email attachments

Posted on 2008-06-13
6
Medium Priority
?
1,532 Views
Last Modified: 2013-11-28
Hi everyone:

I am trying to add an option to my Access 2000 application that will allow the end user to click a command button that would open up an InputBox so that they could type in an email address, then have Access (working with outlook), open a new mail message, populate the recipient TO field (with the data from the InputBox) and attach the appropriate file to the email body and then send the email to the recipient To address.

I would like this to happen all behind the scenes so to speak (except for the InputBox which should display).

The code I have attached mostly works, except&.

1.      Data or email address entered into the InputBox does not get passed through to the email Recipient To field.
2.      The email is shown to the end user&waiting for an address to be typed into the TO field, then the end user has to press send.

Im sure it is possible to get this to work but am not familiar with InputBoxes or what happens to the data typed into them and Im fairly novice when it comes to VBA.

The .Recipients.Add sEmailAddress is hidden and all works as described. When I remove the  it still works but I get the warning about another application trying to send email&. And then I get You must specify an addresses&&BUT...

Is it possible to get this to pull the data from the InputBox and send the email with attachment immediately with no further interaction from the end user?

Thanks,

Kevin

Private Sub Command4_Click()
Application.Assistant.AssistWithAlerts = True
Dim olApp As Outlook.Application 
Dim olNewMail As Outlook.MailItem 
Dim sEmailAddress As String 
Dim fso
Dim file As String
Set olApp = New Outlook.Application 
Set olNewMail = CreateItem(olMailItem) 
With olNewMail
Address = InputBox("Type e-mail address")
.Recipients.Add sEmailAddress 
.Subject = "Worksheet Update" 
.Body = "Updated worksheet attached." 
With .Attachments
.Add "c:\test.doc" 'Attach file (.doc)
End With
'.Save
.Display
'.Send
End With
End Sub

Open in new window

0
Comment
Question by:Nivek333
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21784561
Clearly you don't have Option Explicit set at the top of your module.

If you did you might pick up spelling errors.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 21785085
try

Private Sub Command4_Click()
Application.Assistant.AssistWithAlerts = True
Dim olApp As Outlook.Application
Dim olNewMail As Outlook.MailItem
Dim sEmailAddress As String
Dim fso
Dim file As String
Dim eAddress As String
eAddress = InputBox("Type e-mail address")

Set olApp = New Outlook.Application
Set olNewMail = CreateItem(olMailItem)
With olNewMail
.To = eAddress
.Recipients.Add sEmailAddress
.Subject = "Worksheet Update"
.Body = "Updated worksheet attached."
With .Attachments
.Add "c:\test.doc" 'Attach file (.doc)
End With
'.Save
.Display
'.Send
End With
End Sub
0
 

Author Comment

by:Nivek333
ID: 21787566
Hi capricorn1,

Thanks so much for the assistance. What you provided worked for me quite well with me making a few small changes to my original code snippet posted. I really appreciate that you took the time to answer and I'm going to awared full points to you.

I'm not sure if I can ask another question or not (I am very new to EE) but it does relate specifically to the original code and question.

I am not familiar at all with InputBoxes but when the code is executed and the inputbox is displayed, I want to be able to stop or end the rest of the code if the user clicks the cancel button in the Inputbox. I have tried using IF and THEN but I'm not sure what the reference I am to use...i.e.

If ?????? = vbcancel then
end
end if

I might be using the wrong thing all together. Can I stop the process if the user clicks Cancel in the InputBox?

Thanks again for providing the information. It is much appreciated.


0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21788293


Private Sub Command4_Click()
Application.Assistant.AssistWithAlerts = True
Dim olApp As Outlook.Application
Dim olNewMail As Outlook.MailItem
Dim sEmailAddress As String
Dim fso
Dim file As String
Dim eAddress As String
eAddress = InputBox("Type e-mail address")

 if len(eaddress & "")=0 then exit sub


Set olApp = New Outlook.Application
Set olNewMail = CreateItem(olMailItem)
With olNewMail
.To = eAddress
.Recipients.Add sEmailAddress
.Subject = "Worksheet Update"
.Body = "Updated worksheet attached."
With .Attachments
.Add "c:\test.doc" 'Attach file (.doc)
End With
'.Save
.Display
'.Send
End With
End Sub
0
 

Author Closing Comment

by:Nivek333
ID: 31467147
thanks again!
0
 

Author Comment

by:Nivek333
ID: 21800067
Hi capricorn1:

Thanks for the help, all worked out fine and is exactly what I was after. Points awarded. Thanks again.

I was wondering...is it possible to populate a VB msgbox with the "Dim eAddress As String" data? i.e.

ans=msgbox=("Thank you, the document has been forwarded to "Dim eAddress As String"?") vbokayonly?

Thanks again for you assistance!

Kevin

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Make the most of your online learning experience.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Introduction to Processes
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question