Link to home
Start Free TrialLog in
Avatar of Nivek333
Nivek333

asked on

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

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

Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Clearly you don't have Option Explicit set at the top of your module.

If you did you might pick up spelling errors.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nivek333
Nivek333

ASKER

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.




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
thanks again!
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