eMail Databases with attachment option (1 Viewer)

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
Hello everybody,

Attached there is an eMail Database which can use for the following:

1. To choose the customer to send an email
2. To set up your email style and information to send through

What i want to do (and really missing and would be very helpful) is the option for attached files.

Can someone help me with the code so to do that?
 

Attachments

  • eMails.accdb
    668 KB · Views: 58

June7

AWF VIP
Local time
Yesterday, 20:30
Joined
Mar 9, 2014
Messages
5,466
For the oMail object use:

.Attachments.Add Me.FilePath
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
For the oMail object use:

.Attachments.Add Me.FilePath

Hi june,

Where exactly to change this code line?

Private Sub cmdEmail_Click()
Dim strSubj As String, strBody As String
Dim ol As Object, oMail As Object

If InStr(1, Nz(Me.email, ""), "@") Then
If Not IsNull(Me.TemplateTitle) Then
strSubj = Nz(Me.eMailSubject.Column(1))
strBody = Nz(DLookup("eMailBody", "eMails", "eMailID = " & Me.TemplateTitle))
End If
On Error Resume Next
Set ol = CreateObject("Outlook.Application")
Set oMail = ol.CreateItem(0&)
With oMail
.To = Nz(Me.email)
.Subject = strSubj
If Me.IsHtmlBody Then
.BodyFormat = 2
.HTMLBody = strBody
Else
.BodyFormat = 1
If strBody <> vbNullString Then .Body = RemoveHTML(strBody)
End If
.Display
End With
If Not ol Is Nothing Then Set ol = Nothing
End If
End Sub
 

Trevor G

Registered User.
Local time
Today, 05:30
Joined
Oct 1, 2009
Messages
2,341
Add this before the:

.Attachments.Add Me.FilePath
.Display
 

Trevor G

Registered User.
Local time
Today, 05:30
Joined
Oct 1, 2009
Messages
2,341
The 5th line from the bottom is .Display place the line just above it

.Attachments.Add Me.FilePath
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
The 5th line from the bottom is .Display place the line just above it

.Attachments.Add Me.FilePath

Hi all,

Trevor, does not work (mean doesn't includes the attachment) even after the replacement of the above line ( .Display)

Any idea?

Private Sub cmdSend_Click()
Dim strSubj As String, strBody As String
Dim ol As Object, oMail As Object

If InStr(1, Nz(Me.email, ""), "@") Then
If Not IsNull(Me.TemplateTitle) Then
strSubj = Nz(Me.eMailSubject.Column(1))
strBody = Nz(DLookup("eMailBody", "eMails", "eMailID = " & Me.TemplateTitle))
End If

On Error Resume Next
Set ol = CreateObject("Outlook.Application")
Set oMail = ol.CreateItem(0&)
With oMail
.To = Nz(Me.email)
.Subject = strSubj
If Me.IsHtmlBody Then
.BodyFormat = 2
.HTMLBody = strBody
Else
.BodyFormat = 1
' If strBody <> vbNullString Then .Body = RemoveHTML(strBody)
End If
.Attachments.Add Me.FilePath
' .Display
End With
If Not ol Is Nothing Then Set ol = Nothing
End If
' Me.Grabb = True
Me.Form.Requery

Forms!Contacts.Grabb = True
Forms!Contacts.Form.Requery
End Sub
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
For the oMail object use:

.Attachments.Add Me.FilePath

June,

You told to use the .Attachments.Add Me.FilePath for the oMail.....What exactly you mean? Could you please modify the code behind?

Private Sub cmdSend_Click()
Dim strSubj As String, strBody As String
Dim ol As Object, oMail As Object

If InStr(1, Nz(Me.email, ""), "@") Then
If Not IsNull(Me.TemplateTitle) Then
strSubj = Nz(Me.eMailSubject.Column(1))
strBody = Nz(DLookup("eMailBody", "eMails", "eMailID = " & Me.TemplateTitle))
End If

On Error Resume Next
Set ol = CreateObject("Outlook.Application")
Set oMail = ol.CreateItem(0&)
With oMail
.To = Nz(Me.email)
.Subject = strSubj
If Me.IsHtmlBody Then
.BodyFormat = 2
.HTMLBody = strBody
Else
.BodyFormat = 1
' If strBody <> vbNullString Then .Body = RemoveHTML(strBody)
End If
.Attachments.Add Me.FilePath
.Display
End With
If Not ol Is Nothing Then Set ol = Nothing
End If
' Me.Grabb = True
Me.Form.Requery

' Forms!Contacts.Grabb = True
Forms!Contacts.Form.Requery
End Sub
 

GinaWhipp

AWF VIP
Local time
Today, 00:30
Joined
Jun 21, 2011
Messages
5,900
Hmm, are you using the complete file path and the name of the file for *Me.txtFilePath*?
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
Hmm, are you using the complete file path and the name of the file for *Me.txtFilePath*?

The path its ok, i thing the problem is to dim the attachements but because of my poor knowlege of vba i cannot do it..

Is it possible to modify the code?
 

GinaWhipp

AWF VIP
Local time
Today, 00:30
Joined
Jun 21, 2011
Messages
5,900
Well then you need to provide the error message or the line it stops at. All I see it is *...it doesn't work...*, this tells us nothing. Does the eMail work without the attachments part?
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
It isn't any error appear!!! Just do nothing!!! The outlook doesn"t open!!! Without the attachment line everything is ok.
 

GinaWhipp

AWF VIP
Local time
Today, 00:30
Joined
Jun 21, 2011
Messages
5,900
Okay, don't get excited... I had no idea until you just stated it. Reviewing code you posted will be back shortly.
 

GinaWhipp

AWF VIP
Local time
Today, 00:30
Joined
Jun 21, 2011
Messages
5,900
Well, these is nothing wrong with that line. In other words, you don't need to Dim the attachment because you are using it within the *With* statement. So, the only that comes to mind is the File Path. Is that the name of the Control on your Form?
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:30
Joined
Sep 21, 2011
Messages
14,238
The FilePath is just a generic name June7 gave to a control that would hold the path to the file you are trying to attach.?

O/P you are meant to replace that variable with whatever you are using for the file path.
For now just hard code it, something like "C:\temp\This is the file I want to attach.txt" but please make sure it is a file that exists on your computer.
 

June7

AWF VIP
Local time
Yesterday, 20:30
Joined
Mar 9, 2014
Messages
5,466
FilePath is name of textbox on frmContacts in OP's database I downloaded from post1. Code is behind that form.

Unfortunately, I did not test code. Just tried and Outlook does open but is not attaching file. Which is odd because this should work. Here is code in my db that does work:

Code:
Private Sub EmailWithSig()
Dim oApp As Object, OMail As Object, signature As String
Set oApp = CreateObject("Outlook.application")
Set OMail = oApp.CreateItem(0)
With OMail
    .To = "email address here"
    .Subject = "Subject"
    .Attachments.Add "C:\AlaskaLTC.pdf"
    .Display 'email must be displayed for the next line to work
    .HTMLBody = "Message." & vbNewLine & .HTMLBody
    '.Send
End With
Set OMail = Nothing
Set oApp = Nothing
End Sub
 
Last edited:

GinaWhipp

AWF VIP
Local time
Today, 00:30
Joined
Jun 21, 2011
Messages
5,900
Well, turned into a Function and added LocalHost and it works...
Code:
[B]Function[/B] EmailWithSig()

Dim oApp As Object, OMail As Object, signature As String
Set oApp = CreateObject("Outlook.Application", "[B]LocalHost[/B]")
Set OMail = oApp.CreateItem(0)
With OMail
    .To = "email address here"
    .Subject = "Subject"
    .Attachments.Add "C:\AlaskaLTC.pdf"
    .Display 'email must be displayed for the next line to work
    .HTMLBody = "Message." & vbNewLine & .HTMLBody
    '.Send
End With
Set OMail = Nothing
Set oApp = Nothing
End Function
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
Morning,

Please all, try to use the attached example (new one) as well as add a Path and test it..!!!

Unfortunately you just see the outlook became to open and then close immediately without send the email....

Really don't know....
 

Attachments

  • eMails.accdb
    712 KB · Views: 51

June7

AWF VIP
Local time
Yesterday, 20:30
Joined
Mar 9, 2014
Messages
5,466
Yes, this was happening in your first version.

I just tried hard-coding of a filepath in the code and it works. So there is something about referencing the textbox. Stepping through code, I see the value of textbox is the filepath but for some reason Attachments.Add is not acknowledging it. So I created a string variable and set it with value of textbox. This code works:

strAttach = Me.FilePath
.Attachments.Add strAttach

Don't ask me why.
 

gstylianou

Registered User.
Local time
Today, 07:30
Joined
Dec 16, 2013
Messages
357
FilePath is name of textbox on frmContacts in OP's database I downloaded from post1. Code is behind that form.

Unfortunately, I did not test code. Just tried and Outlook does open but is not attaching file. Which is odd because this should work. Here is code in my db that does work:

Code:
Private Sub EmailWithSig()
Dim oApp As Object, OMail As Object, signature As String
Set oApp = CreateObject("Outlook.application")
Set OMail = oApp.CreateItem(0)
With OMail
    .To = "email address here"
    .Subject = "Subject"
    .Attachments.Add "C:\AlaskaLTC.pdf"
    .Display 'email must be displayed for the next line to work
    .HTMLBody = "Message." & vbNewLine & .HTMLBody
    '.Send
End With
Set OMail = Nothing
Set oApp = Nothing
End Sub


Morning my friend June,

Ok, i will test it but how can i write correct the code in line .Attachments.Add "C:\AlaskaLTC.pdf" in order to take the path into FilePath Field?
 

Users who are viewing this thread

Top Bottom