Send Attachment Fields to Email as Attachments (1 Viewer)

Eljefegeneo

Still trying to learn
Local time
Today, 15:27
Joined
Jan 10, 2011
Messages
904
Gasman: I had the same problem as you in that it failed on the line:

Code:
.To = Me.EmailTo
But will work if you use:

Code:
.To = Cstr(Me.EmailTo)
I think it has something to do with the references, but now I am way over my head.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:27
Joined
Oct 29, 2018
Messages
21,467
Gasman: I had the same problem as you in that it failed on the line:

Code:
.To = Me.EmailTo
But will work if you use:

Code:
.To = Cstr(Me.EmailTo)
I think it has something to do with the references, but now I am way over my head.

Hi. Just curious, did you get the same error with my code on the same computer?
 

Eljefegeneo

Still trying to learn
Local time
Today, 15:27
Joined
Jan 10, 2011
Messages
904
Yes, same error message.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:27
Joined
Sep 21, 2011
Messages
14,260
You Sir, are a genius.:cool:

That was annoying me so much, but I could not work out what was wrong with my setup.:banghead:

I found I could set the To in he immediate window, but not the subject or body last night. However having tried again this morning it allows both those two from the immediate window as .Subject and .HTMLBody :confused:
And adding CSTR() allows the code to run.:confused:

Fortunately my code is only for me these days and I would use early binding, but it still annoyed the hell out of me.

Thank you for helping me not to go insane. :D

Gasman: I had the same problem as you in that it failed on the line:

Code:
.To = Me.EmailTo
But will work if you use:

Code:
.To = Cstr(Me.EmailTo)
I think it has something to do with the references, but now I am way over my head.
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:27
Joined
Sep 21, 2011
Messages
14,260
Following on from the deepest thread, this also works

Code:
        .To = Me.EmailTo & ""
        .Subject = Me.EmailSubject & ""
        .HTMLBody = Me.EmailBody & ""

as does
Code:
        .To = Me.EmailTo.Value
        .Subject = Me.EmailSubject.Value
        .HTMLBody = Me.EmailBody.Value
but not particularly intuitive as we are always told .Value is the default property and does not need to be used, but in this context it does.:)

So three workarounds. Of the three mine my favourite would be the & "" version
 

isladogs

MVP / VIP
Local time
Today, 23:27
Joined
Jan 14, 2017
Messages
18,212
I've not really followed this thread so apologies if this has already been suggested.
Untested but I expect you'll find this also works

Code:
        .To = Nz(Me.EmailTo,"")
        .Subject = Nz(Me.EmailSubject,"")
        .HTMLBody = Nz(Me.EmailBody,"")
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:27
Joined
Sep 21, 2011
Messages
14,260
Colin,

Yes that works as well.

The other method was to assign the form control values to variables.

So five ways in all to get around the problem. :) I think that is enough.

I've not really followed this thread so apologies if this has already been suggested.
Untested but I expect you'll find this also works

Code:
        .To = Nz(Me.EmailTo,"")
        .Subject = Nz(Me.EmailSubject,"")
        .HTMLBody = Nz(Me.EmailBody,"")
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:27
Joined
Oct 29, 2018
Messages
21,467
The deeper question I would have is how come it doesn't happen all the time? For example, I've never run into this problem when I use .To = Me.txtTo
 

Gasman

Enthusiastic Amateur
Local time
Today, 23:27
Joined
Sep 21, 2011
Messages
14,260
The deeper question I would have is how come it doesn't happen all the time? For example, I've never run into this problem when I use .To = Me.txtTo

No idea,:eek:
I was inadvertently getting around it when I was trying .To = "ww" in the immediate window, without realising it. Then I probably tried .Subject = Me.Subject and then it errored again. Never thought to try a simple string, and I would never have thought to set it via a variable as I try an avoid that unless there is a specific reason, which of course in this case, there is.

Plus it worked fine for you and the o/p?

I will not forget this quickly, evrn with my bad memory. :D
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:27
Joined
Oct 29, 2018
Messages
21,467
By the way, what was the error message?
 

cdoner

Registered User.
Local time
Today, 15:27
Joined
Dec 1, 2013
Messages
25
@theDBguy

Hi. Hope it helps with your actual project. Good luck!

Yes sir, was able to successfully implement your code to my project. Your code was way more elegant that what I had previously. Super appreciative. I'll be studying your code for some time as I continue to challenge myself to become a better developer.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:27
Joined
Oct 29, 2018
Messages
21,467
@theDBguy

Yes sir, was able to successfully implement your code to my project. Your code was way more elegant that what I had previously. Super appreciative. I'll be studying your code for some time as I continue to challenge myself to become a better developer.
Hi. Glad to hear you got it to work. My code is elementary. Once you understood it, you may graduate up to the one Gasman provided. Good luck with your project.
 

Users who are viewing this thread

Top Bottom