Solved

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

Posted on 2008-06-13
6
1,482 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

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.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

861 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