Solved

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

Posted on 2008-06-13
6
1,500 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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