Solved

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

Posted on 2008-06-13
6
1,443 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
  • 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 119

Accepted Solution

by:
Rey Obrero 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 119

Expert Comment

by:Rey Obrero
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now