Email attachment

Eljefegeneo

Still trying to learn
Local time
Today, 11:34
Joined
Jan 10, 2011
Messages
899
While I know this subject has been posted about a thousand times, I still can't figure out what I am doing wrong. All I want to do it bring up a form from which the user can send an email, and on the form is an unbound text box [Text1] in which the user can paste the attachment path.

Code:
Dim olApp As New Outlook.Application
Dim mItem As Outlook.MailItem  ' An Outlook Mail item
Set olApp = CreateObject("Outlook.Application")
Set mItem = olApp.CreateItem(olMailItem)
' Add the To/Subject/Body to the message and display the message
With mItem
    .To = Me.To
    .CC = CC2
    .BCC = BCC2
    .Subject = Me.SubjectLine
    .Body = Me.MessageBody
    .Attachments.Add (Text1)  "This produces an error"
'this next line works perfectly
   ' .Attachments.Add ("C:\Users\Gene\Documents\Filename.pdf")
    .Display    ' To show the email message to the user
End With
So, when I paste the file location into Text box [Text1] I get the Run Time error that the path does not exist or is not valid. Yet when I use the next line which is the same path, all is OK.

What am I doing wrong or rather where is my code in error. Thanks
 
Have you tried

Me.Text1
 
Just before checking the post I thought of that but it didn't work either. Could It be that the field isn't bound?
 
I think I have discovered what is happening, but I do not know how to correct it.

Debugging the code I discovered that somehow there were quotation marks being added to the beginning and end of the file. That is, if I had the file to be attached pasted into the text box that I wanted to attach,

"e:\email\doc1.pdf" it somehow becomes "("e:\email\doc1.pdf")" to be attached.

All I am doing is selecting a file to be attached, using shift right click and selecting copy as path and pasting it in the text box. The text box then shows:

"e:\email\doc1.pdf"

But when I add the opening and closing (), I get quotation marks outside the (). The code then produces the error that this is an invalid file name or directory.

How do I get rid of the outside " marks?
 
I have spent another few hours search for an answer, but nothing appears. Do anyone see either what I am doing wrong to get the extra leading and trailing quotation marks or how to get rid of them? Surely this is not an unsolvable problem. Thank you.
 
Can you post the db here?
 
Thank you so much for offering to help. I made a watered down version of the DB attached and proceeded to make sure most of the code worked properly. Alas, I found that I had forgotten to include a library, and once that was done discovered something. I was trying to look at a big picture when I should have been concentrating on a very small one - a problem I seem to have. I realized that all I had to do was remove the leading and trailing quotation marks that come from copying the file path and pasting it into the form. So I used the replace and Ltrim functions and it now seems to work properly.

But I would like to know if I am doing ti correctly, thus posting the DB. Any comments would be appreciated. Thanks again.
 

Attachments

I guess I don't understand the problem, as I've never had quotes getting in the path. That said, I typically use one of the various file dialog methods to let the user "choose" the file, and grab the path from that. If what you've done solves the problem, I'd probably let sleeping dogs lie.
 
I think that the problem arose because when you select a file by the "copy path" method, Shift-Right Click of the mouse, copy path and then paste into the form, Microsoft puts quotes around the file.

And yes, it now seems to work fine.
 

Users who are viewing this thread

Back
Top Bottom