By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

How to Attach the File in a Email using VB & Access

P: 35
Hai, am using the Email function with Attachments in Vb and Access Forms. The Emial function is working. Then how to attach the file with my mail. there is any Browse control is there.

anybody know pls let me know.

By

Tamilvanan
Mar 30 '07 #1
Share this Question
Share on Google+
35 Replies


vijaydiwakar
100+
P: 579
Hai, am using the Email function with Attachments in Vb and Access Forms. The Emial function is working. Then how to attach the file with my mail. there is any Browse control is there.

anybody know pls let me know.

By

Tamilvanan
try this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Form_Load()
  3. Set OLApp = CreateObject("Outlook.Application")
  4.         Set MyItem = OLApp.CreateItem(olMailItem)
  5.  
  6.         txtInsertFile.Text = "c:\ReadMe.txt"
  7.         MyItem.To = "xxx@gmail.com"
  8.  
  9.         MyItem.Subject = "Attach file"
  10.         MyItem.Body = "read this file "
  11.         Set MyAttachments = MyItem.Attachments
  12.         MyAttachments.Add txtInsertFile.Text
  13.         MyItem.Send
  14. End Sub
  15.  
Mar 30 '07 #2

P: 35
try this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Form_Load()
  3. Set OLApp = CreateObject("Outlook.Application")
  4.         Set MyItem = OLApp.CreateItem(olMailItem)
  5.  
  6.         txtInsertFile.Text = "c:\ReadMe.txt"
  7.         MyItem.To = "xxx@gmail.com"
  8.  
  9.         MyItem.Subject = "Attach file"
  10.         MyItem.Body = "read this file "
  11.         Set MyAttachments = MyItem.Attachments
  12.         MyAttachments.Add txtInsertFile.Text
  13.         MyItem.Send
  14. End Sub
  15.  

Hai, I am Trying to attach a file using your code. But i have the error message, while run this code. The error message was given below.

"Run-time error '2185':
You can't reference a property or method for a control unless the control has the focus."

Thanks

Tamilvanan
Mar 30 '07 #3

vijaydiwakar
100+
P: 579
Hai, I am Trying to attach a file using your code. But i have the error message, while run this code. The error message was given below.

"Run-time error '2185':
You can't reference a property or method for a control unless the control has the focus."

Thanks

Tamilvanan
this may be due to some another reason
mycode is perfect
Mar 30 '07 #4

P: 35
this may be due to some another reason
mycode is perfect

I need to Browse and select the file, and attach that file in the Email using Ms-Access 2003 and OutLook 2003.

Can you know how to Browse the file, let me know.
In the previous code, in my system isn't have the activex control properly. So thats why that is not working.

Kindly, let me know how to browse and attach the file please....

Thanks,
Tamilvanan
Mar 30 '07 #5

iburyak
Expert 100+
P: 1,017
In Visual Basic References check
Microsoft Outlook 11.0 Object Library

before running code above.


Good Luck.
Mar 30 '07 #6

P: 35
Hai.

Thanks for your replies.

I need to Browse the File. I need not to set the default file location and attach the file.Like this

txtInsertFile.Text = "c:\ReadMe.txt"

I need like this. If we will click the Browse button, the file window will be opened, and we select the Driver(C: or D: or E: ). Then we select the Directories and filename.Then we click the open, the whole file path was stored in a Text box.

This method i need it. Can you know how to do this.

Please let me know.

Thanks
Tamilvanan
Apr 2 '07 #7

vijaydiwakar
100+
P: 579
Hai.

Thanks for your replies.

I need to Browse the File. I need not to set the default file location and attach the file.Like this

txtInsertFile.Text = "c:\ReadMe.txt"

I need like this. If we will click the Browse button, the file window will be opened, and we select the Driver(C: or D: or E: ). Then we select the Directories and filename.Then we click the open, the whole file path was stored in a Text box.

This method i need it. Can you know how to do this.

Please let me know.

Thanks
Tamilvanan
for that use directory ctrl or u may use commandialogbox a comman ctrl of ms windows
Apr 2 '07 #8

P: 35
Hai.
I am Try to drag and drop that control But i cant.
Actually in Access forn Design view, Normal Toolbox does not contain that controls. So am go to click the more controls button. Then am select the Microsoft Common Dialog Control, Version 6.0 . Other Two Controls are not displayed.

The Error message displays, while select the control. The Erro Message is.

you don't have the license required to use this ActiveX control.
You tried to open a form containing an OLE object or an ActiveX control or you tried to create an ActiveX control. To obtain the appropriate license, contact the company that provides the licensed OLE object or ActiveX control.



So how to Rectify this error. Why this Error was Occured, while drag and drop the control.

Please post me the detail.

Thanks,

Tamilvanan
Apr 2 '07 #9

iburyak
Expert 100+
P: 1,017
Is it possible that you are not a local admin on your computer?
Visual Basic is writing something on your C drive first time you use an OCX. If you don't have rights to edit your C drive it could happen.

If you would be able to put Dialog Box control on your form use following code to load file text into a textbox which should be MultiLine = True and have ScrollBars.

[PHP]Dim sFileName As String, NextLine As String
Dim FH As Long

With CommonDialog1
.CancelError = True
.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
.FilterIndex = 2
.InitDir = "I use this property when I have default directory standard or in many cases I save last used path"
.ShowOpen

sFileName = .FileName
End With

FH = FreeFile()

Open sFileName For Input Access Read Shared As #FH

Do Until EOF(FH)
Line Input #FH, NextLine
Text1.Text = Text1.Text & NextLine & vbCrLf
Loop

Close #FH[/PHP]
Apr 2 '07 #10

P: 35
Is it possible that you are not a local admin on your computer?
Visual Basic is writing something on your C drive first time you use an OCX. If you don't have rights to edit your C drive it could happen.

If you would be able to put Dialog Box control on your form use following code to load file text into a textbox which should be MultiLine = True and have ScrollBars.

[PHP]Dim sFileName As String, NextLine As String
Dim FH As Long

With CommonDialog1
.CancelError = True
.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
.FilterIndex = 2
.InitDir = "I use this property when I have default directory standard or in many cases I save last used path"
.ShowOpen

sFileName = .FileName
End With

FH = FreeFile()

Open sFileName For Input Access Read Shared As #FH

Do Until EOF(FH)
Line Input #FH, NextLine
Text1.Text = Text1.Text & NextLine & vbCrLf
Loop

Close #FH[/PHP]

Hai,

I can't able to put the Dialog Box control in my form. So that am asking this question.

I need to Rectify the errors, while am try to put the control in my Access Form.

The Error Message Is :
you don't have the license required to use this ActiveX control.
You tried to open a form containing an OLE object or an ActiveX control or you tried to create an ActiveX control. To obtain the appropriate license, contact the company that provides the licensed OLE object or ActiveX control.


This have any difference for Vb Forms and Access Forms.

If, what's the difference between Vb Form and Access Form.

Becaouse, I am trying this mail functionality using Ms-Access Form only. But, i think it have the same characteristics as Vb Form.

Any ActivX control or Components need to install for this problem?
Apr 3 '07 #11

P: 35
Is it possible that you are not a local admin on your computer?
Visual Basic is writing something on your C drive first time you use an OCX. If you don't have rights to edit your C drive it could happen.

If you would be able to put Dialog Box control on your form use following code to load file text into a textbox which should be MultiLine = True and have ScrollBars.

[PHP]Dim sFileName As String, NextLine As String
Dim FH As Long

With CommonDialog1
.CancelError = True
.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
.FilterIndex = 2
.InitDir = "I use this property when I have default directory standard or in many cases I save last used path"
.ShowOpen

sFileName = .FileName
End With

FH = FreeFile()

Open sFileName For Input Access Read Shared As #FH

Do Until EOF(FH)
Line Input #FH, NextLine
Text1.Text = Text1.Text & NextLine & vbCrLf
Loop

Close #FH[/PHP]
Hai,
Thank you very much of your reply.
Am used ur code. But that have occured error while running those code.
The error message was.

Run-time error '424':
Object required

This error occured at the following line.
.CancelError = True

Can you know why this error could be occured.
If you know, pls let me know.

Thanks
Tamilvanan
Apr 3 '07 #12

vijaydiwakar
100+
P: 579
Hai,
Thank you very much of your reply.
Am used ur code. But that have occured error while running those code.
The error message was.

Run-time error '424':
Object required

This error occured at the following line.
.CancelError = True

Can you know why this error could be occured.
If you know, pls let me know.

Thanks
Tamilvanan
see this is a facility given expolicitly by Microsoft to tress Cancel Button click
u may avoid this line but for that ur code must be prepared for handling cancel btn
have u placed Commandialog box on ur form?
if yes check its name is same as that of commandialogebox1
which u've used in With bolck
Apr 3 '07 #13

P: 35
see this is a facility given expolicitly by Microsoft to tress Cancel Button click
u may avoid this line but for that ur code must be prepared for handling cancel btn
have u placed Commandialog box on ur form?
if yes check its name is same as that of commandialogebox1
which u've used in With bolck

Hai.
Sorry, i can't put the CommonDiaog Box in my form. Because the error occured, when i go to put the CommonDialog Box in my Access Form.

I already post the error message

You don't have the license required to use this ActiveX control.

You tried to open a form containing an OLE object or an ActiveX control or you tried to create an ActiveX control. To obtain the appropriate license, contact the company that provides the licensed OLE object or ActiveX control.



Then i try to Register the many Control, like comcat.dll, comctl32.dll, comdlg32.dll, COMDLG32.OCX.

The COMDLG32.OCX only was Registered Successfully. But i have the same problem.

The error is:



c:\windows\system32\comctl32.dll was loaded, but the DllRegisterServer entry point was not found.

This file can not be registered.


So, still i am stuck. Pls help me

I don't know why this error could be occured. if anybody know, how to solve this error.

Please let me know.
Apr 3 '07 #14

iburyak
Expert 100+
P: 1,017
Hai.
Sorry, i can't put the CommonDiaog Box in my form. Because the error occured, when i go to put the CommonDialog Box in my Access Form.

I already post the error message

You don't have the license required to use this ActiveX control.

You tried to open a form containing an OLE object or an ActiveX control or you tried to create an ActiveX control. To obtain the appropriate license, contact the company that provides the licensed OLE object or ActiveX control.



Then i try to Register the many Control, like comcat.dll, comctl32.dll, comdlg32.dll, COMDLG32.OCX.

The COMDLG32.OCX only was Registered Successfully. But i have the same problem.

The error is:



c:\windows\system32\comctl32.dll was loaded, but the DllRegisterServer entry point was not found.

This file can not be registered.


So, still i am stuck. Pls help me

I don't know why this error could be occured. if anybody know, how to solve this error.

Please let me know.
You should have edit rights on your C: drive.

Can you create a text file on your C drive with no problem?

If not this is a problem VB needs some rights on your local drive to make it work.

Also it is possible that you are using not authentic VB version.

Thank you.
Apr 3 '07 #15

P: 35
You should have edit rights on your C: drive.

Can you create a text file on your C drive with no problem?

If not this is a problem VB needs some rights on your local drive to make it work.

Also it is possible that you are using not authentic VB version.

Thank you.
I am not have the VB in my System. I have only the Ms-Access.
In my system i don't install the Visual Basic. I can only using the Ms-Access.
If i will go to install any ActiveX control for Ms-Access. Where i go to install the ActiveX Control, and how to Register the Controls?


Thanks
Tamilvanan
Apr 4 '07 #16

P: 35
You should have edit rights on your C: drive.

Can you create a text file on your C drive with no problem?

If not this is a problem VB needs some rights on your local drive to make it work.

Also it is possible that you are using not authentic VB version.

Thank you.
Yes. I have the rights to create the text file without any problem in c:
But, in my system don't have the VB. So that the problem araise?
If VB doesn't installed the system, where i go to install the ActiveX controls?
or
Must install the Vb in my system?

Please, reply me.

Thanks
Tamilvanan
Apr 4 '07 #17

vijaydiwakar
100+
P: 579
ya u may install vb
just proceed
Good Luck
Apr 4 '07 #18

P: 35
Thanks for all your replies.
Now i have access the Common Dialog Control.
But another one problem raised. When i go to Browse and select the file. The file content was stored in the textbox.
But i need to store the file path only. I am using the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.  
  3. Dim sFileName As String, NextLine As String
  4. Dim FH As Long
  5.  
  6. With CommonDialog1
  7.     .CancelError = True
  8.     .Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
  9.     .FilterIndex = 2
  10.     .InitDir = "I use this property when I have default directory standard or in many cases I save last used path"
  11.     .ShowOpen
  12.  
  13.     sFileName = .FileName
  14. End With
  15.  
  16. FH = FreeFile()
  17.  
  18. Open sFileName For Input Access Read Shared As #FH
  19.  
  20. Do Until EOF(FH)
  21.     Line Input #FH, NextLine
  22.     Text3.Value = Text3.Value & NextLine
  23. Loop
  24.  
  25. Close #FH
  26.  
  27.  End Sub
  28.  
Please post me the Details. Am waiting for any one reply.


Thanks
Tamilvanan
Apr 4 '07 #19

iburyak
Expert 100+
P: 1,017
Just change this:

[PHP]FH = FreeFile()

Open sFileName For Input Access Read Shared As #FH

Do Until EOF(FH)
Line Input #FH, NextLine
Text3.Value = Text3.Value & NextLine
Loop

Close #FH

End Sub[/PHP]

to this:

[PHP]Text3.text = sFileName [/PHP]
Apr 4 '07 #20

MMcCarthy
Expert Mod 10K+
P: 14,534
This thread is being moved to the Access forum.

ADMIN
Apr 4 '07 #21

MMcCarthy
Expert Mod 10K+
P: 14,534
Thanks for all your replies.
Now i have access the Common Dialog Control.
But another one problem raised. When i go to Browse and select the file. The file content was stored in the textbox.
But i need to store the file path only. I am using the following code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.  
  3. Dim sFileName As String, NextLine As String
  4. Dim FH As Long
  5.  
  6. With CommonDialog1
  7.     .CancelError = True
  8.     .Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt"
  9.     .FilterIndex = 2
  10.     .InitDir = "I use this property when I have default directory standard or in many cases I save last used path"
  11.     .ShowOpen
  12.  
  13.     sFileName = .FileName
  14. End With
  15.  
  16. FH = FreeFile()
  17.  
  18. Open sFileName For Input Access Read Shared As #FH
  19.  
  20. Do Until EOF(FH)
  21.     Line Input #FH, NextLine
  22.     Text3.Value = Text3.Value & NextLine
  23. Loop
  24.  
  25. Close #FH
  26.  
  27.  End Sub
  28.  
Please post me the Details. Am waiting for any one reply.


Thanks
Tamilvanan
I'm not sure what you are trying to do that this code is not currently doing. Can you explain further?

Mary
Apr 4 '07 #22

P: 35
Just change this:

[PHP]FH = FreeFile()

Open sFileName For Input Access Read Shared As #FH

Do Until EOF(FH)
Line Input #FH, NextLine
Text3.Value = Text3.Value & NextLine
Loop

Close #FH

End Sub[/PHP]

to this:

[PHP]Text3.text = sFileName [/PHP]


Thank you iburyak.
Am change the code as you said. But still i have the problem.

If i have change the code
Text1.Text = Text1.Text & NextLine & vbCrLf
as
Text3.text = sFileName
following error message will be shown.

you can't reference a property or method for a control unless the control has the focus
So i had stuck.

Please post me the details, i am waiting for reply.

Thanks
Tamilvanan
Apr 5 '07 #23

P: 35
I'm not sure what you are trying to do that this code is not currently doing. Can you explain further?

Mary

Hai. i am trying to send and receive email using Ms-Access. I am complete the Send Email. But still the attachment pending.
I need the browse button, to browse and select the file, the selected file path was stored in a text box.

Thats why am using the above coding. But if i go to browse and select the file using those code, the selected file content was stored in the text box.

I need only the path will be stored in that text box.

Can you know how this will be solve? Please let me know.

Thanks
Tamilvanan
Apr 5 '07 #24

iburyak
Expert 100+
P: 1,017
I'm not sure what you are trying to do that this code is not currently doing. Can you explain further?

Mary
Mary,

This is his problem and I am not sure how to help him in Access.


I am Try to drag and drop that control But i cant.
Actually in Access forn Design view, Normal Toolbox does not contain that controls. So am go to click the more controls button. Then am select the Microsoft Common Dialog Control, Version 6.0 . Other Two Controls are not displayed.

The Error message displays, while select the control. The Erro Message is.

you don't have the license required to use this ActiveX control.
You tried to open a form containing an OLE object or an ActiveX control or you tried to create an ActiveX control. To obtain the appropriate license, contact the company that provides the licensed OLE object or ActiveX control.


So how to Rectify this error. Why this Error was Occured, while drag and drop the control.

Please post me the detail.

Thanks,

Tamilvanan
Apr 5 '07 #25

iburyak
Expert 100+
P: 1,017
Hai. i am trying to send and receive email using Ms-Access. I am complete the Send Email. But still the attachment pending.
I need the browse button, to browse and select the file, the selected file path was stored in a text box.

Thats why am using the above coding. But if i go to browse and select the file using those code, the selected file content was stored in the text box.

I need only the path will be stored in that text box.

Can you know how this will be solve? Please let me know.

Thanks
Tamilvanan
ttamilvanan81,
you didn't reply to me if you are able to create any text file on your C drive.
It looks like you don't have rights that required running VBA and there is no way forum can help you with that.
Apr 5 '07 #26

MMcCarthy
Expert Mod 10K+
P: 14,534
Mary,

This is his problem and I am not sure how to help him in Access.
Ok so you've been trying to use the activeX control and you are getting the no licence warning. Try this ...

Go to the VBA Editor
Go to Tools - Referances - click Browse
Locate the activex file (comdlg32.ocx)
The add referance box will register your activeX into the list box on the left pane.

This should register the ActiveX control and stop this warning from appearing.

Mary
Apr 5 '07 #27

MMcCarthy
Expert Mod 10K+
P: 14,534
This code is opening the file and storing it in the textbox.
Expand|Select|Wrap|Line Numbers
  1. FH = FreeFile()
  2.  
  3. Open sFileName For Input Access Read Shared As #FH
  4.  
  5. Do Until EOF(FH)
  6.     Line Input #FH, NextLine
  7.     Text3.Value = Text3.Value & NextLine
  8. Loop
  9.  
  10. Close #FH
If all you want is the filepath then try ...

Expand|Select|Wrap|Line Numbers
  1.      Text3.Value = sFileName 
instead of that code.

Mary
Apr 5 '07 #28

P: 35
Ok so you've been trying to use the activeX control and you are getting the no licence warning. Try this ...

Go to the VBA Editor
Go to Tools - Referances - click Browse
Locate the activex file (comdlg32.ocx)
The add referance box will register your activeX into the list box on the left pane.

This should register the ActiveX control and stop this warning from appearing.

Mary
Hai. Thanks for your reply. Am already do that, to Register the COMDLG32.OCX. But i didn't do the Register.
Am tryed what u said as,

The add referance box will register your activeX into the list box on the left pane.

But the Available Reference box list out the control, named

Microsoft Common Dialog Control6.0 (sp3). Then the Same Error Message occured. Then what to do.

Did u meant the Add Reference Box (list box) or Available References list Box ?

If u meant is add Reference Box. Tell me how to pick the add reference box?

Am waiting for your reply.

Thanks
Tamilvanan
Apr 7 '07 #29

P: 35
ttamilvanan81,
you didn't reply to me if you are able to create any text file on your C drive.
It looks like you don't have rights that required running VBA and there is no way forum can help you with that.
Hai. I have the rights to create the text fiel in my C Drive.
That's no problem for creating the file.
am goes to create a text file, it's creted successfully.

Can u post the any other method.

Thanks
Tamilvanan
Apr 7 '07 #30

MMcCarthy
Expert Mod 10K+
P: 14,534
Hai. Thanks for your reply. Am already do that, to Register the COMDLG32.OCX. But i didn't do the Register.
Am tryed what u said as,

The add referance box will register your activeX into the list box on the left pane.

But the Available Reference box list out the control, named

Microsoft Common Dialog Control6.0 (sp3). Then the Same Error Message occured. Then what to do.

Did u meant the Add Reference Box (list box) or Available References list Box ?

If u meant is add Reference Box. Tell me how to pick the add reference box?

Am waiting for your reply.

Thanks
Tamilvanan
Did you follow these steps ...

1. Go to the VBA Editor
2. Go to Tools - Referances - click Browse
3. Locate the activeX file (comdlg32.ocx)
Apr 7 '07 #31

P: 35
Did you follow these steps ...

1. Go to the VBA Editor
2. Go to Tools - Referances - click Browse
3. Locate the activeX file (comdlg32.ocx)
Thanks.
I already do this steps. But again i have the same problem.

I don't know why this error could be occured.


Thanks
Tamilvanan
Apr 9 '07 #32

MMcCarthy
Expert Mod 10K+
P: 14,534
Thanks.
I already do this steps. But again i have the same problem.

I don't know why this error could be occured.


Thanks
Tamilvanan
I honestly don't know. If the library is ticked in the list of references it should be fine.

Mary
Apr 10 '07 #33

P: 35
I honestly don't know. If the library is ticked in the list of references it should be fine.

Mary
Fine and Thanks for your replies.
If you know how to Receive Email using Access and Outlook. The Receive Email will be work as follows.

On the receiving end, the user receives the email at the Outlook. When they opens the Email, the message box should have a button called “Save to Access”.

When this button is pressed, a dialog box should pop up where the user can choose a contact name and a job name and click on SAVE

Here contact name and job name are in the access table fields. We go to select any one contact and any one job for the particular Email. Then the Email details are stored into a Access table.


If you know this, post me please...

Thanks
Tamilvanan
Apr 10 '07 #34

P: 2
hai

how to attach the file using the vb.net with code
Aug 2 '07 #35

P: 4
try this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Form_Load()
  3. Set OLApp = CreateObject("Outlook.Application")
  4.         Set MyItem = OLApp.CreateItem(olMailItem)
  5.  
  6.         txtInsertFile.Text = "c:\ReadMe.txt"
  7.         MyItem.To = "xxx@gmail.com"
  8.  
  9.         MyItem.Subject = "Attach file"
  10.         MyItem.Body = "read this file "
  11.         Set MyAttachments = MyItem.Attachments
  12.         MyAttachments.Add txtInsertFile.Text
  13.         MyItem.Send
  14. End Sub
  15.  
Excellent, thanks for this, I have been looking for something like this for a long time.

I deleted the .text from the txtInsertFile.Text line and am able to use PDF file formats. Works wonders, will save me so much time.
Sep 21 '07 #36

Post your reply

Sign in to post your reply or Sign up for a free account.