eMail Databases with attachment option (1 Viewer)

June7

AWF VIP
Local time
Today, 02:13
Joined
Mar 9, 2014
Messages
5,423
No, use your original code and modify with the two lines in post 19. I ran YOUR code with that simple modification and it works.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:13
Joined
Sep 21, 2011
Messages
14,038
My apologies June7, for the misunderstanding.


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
 

Minty

AWF VIP
Local time
Today, 10:13
Joined
Jul 26, 2013
Messages
10,353
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.

PMFJI - This is a common gotcha with Outlook Automation.

Using Me.SomeControl within a With OLObject... construct seems to always give issues, so creating variables to hold all the elements you need for the mail object is a really good practice to get into.

As an added benefit it also makes for easier debugging as well.
 

gstylianou

Registered User.
Local time
Today, 12:13
Joined
Dec 16, 2013
Messages
357
Really its so hard for me the vba......I tried to put the piece of line which June gave me but.......again stacked..!!!

Please somebody wants to help..?
 

Attachments

  • error.PNG
    error.PNG
    41.7 KB · Views: 43

Gasman

Enthusiastic Amateur
Local time
Today, 10:13
Joined
Sep 21, 2011
Messages
14,038
Are you making this up as you go along?
Are you using the code editor to write the code, or something else?

There is no Variable type in the list when typing Dim strAttach as ...., Variant yes, but not Variable. Well not in 2007 ?:confused:

I would have thought you would need String anyway.

Really its so hard for me the vba......I tried to put the piece of line which June gave me but.......again stacked..!!!

Please somebody wants to help..?
 

gstylianou

Registered User.
Local time
Today, 12:13
Joined
Dec 16, 2013
Messages
357
Are you making this up as you go along?
Are you using the code editor to write the code, or something else?

There is no Variable type in the list when typing Dim strAttach as ...., Variant yes, but not Variable. Well not in 2007 ?:confused:

I would have thought you would need String anyway.

Gasmn i use the vba editor ...
You mean the problem is because i put Variable and must be Variant? Please....if you want modify the code in order to solve the issue..(i will appreciate a lot)
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:13
Joined
Sep 21, 2011
Messages
14,038
Change the word Variable to String
Then please compile using Debug/Compile on the menu. This will highlight any other issues.

Computers are stupid, you and I might know that Variable is meant to be Variant, but a computer would not.

You should also have
Code:
Option Compare Database
Option Explicit

or at least the Explicit statement at the top of each module/form

This will then allow you to find spelling mistakes and the like.

I do appreciate that you are a learner at this, as I was (and still am, as it is a continuous process), but you have to start learning how to code, if you want to do complex tasks like this.

You will find code on the net and here that will do a lot of what you want, but you will need to tailor it on nearly every occasion.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:13
Joined
Sep 21, 2011
Messages
14,038
I see you do have those lines in your form, so you have not tried to compile?

FWIW, I added the code, but my previous post stands.

I could at least open this DB.

HTH
 

Attachments

  • eMails.accdb
    832 KB · Views: 42

June7

AWF VIP
Local time
Today, 02:13
Joined
Mar 9, 2014
Messages
5,423
Sorry, yes with the variable declaration there are 3 lines to add to your code:

Dim strAttach As String

strAttach = Me.FilePath
.Attachments.Add strAttach
 

Users who are viewing this thread

Top Bottom