Solved

Need to Send Email From MS Access Form (Using VBA) With Mulitiple Pictures from Network Location using "Default Outlook Program"

Posted on 2010-11-25
23
806 Views
Last Modified: 2012-08-13
I have a Form that i would like to add a button to so i can generate and email with pictures for the "BAM" Number shown. The BAM number is my identifier for the pictures being sent... Sometime it will be 1 picture sometimes it might be 10. They are already reduced in file size and just need to be pullled from certain network location and attached to email....

I am using Access 2010 and i have tried adding COM and Access Add-Ins with no luck....
i have tried using this command
Dim MyOutlook As Outlook.Application
But i guess its calling for the addin that is not there?
0
Comment
Question by:bignadad
  • 13
  • 7
  • 3
23 Comments
 
LVL 13

Accepted Solution

by:
gbanik earned 250 total points
ID: 34214708
Modify this code to send mail (even if Outlook is closed)
Option Explicit

Public Sub SendMail()

    Dim olApp
    Set olApp = CreateObject("Outlook.Application")

    Dim olMail, oReci
    Set olMail = olApp.CreateItem(0)

    With olMail
        
        Set oReci = .Recipients.Add("oneperson@gmail.com")
        oReci.Type = 1

        Set oReci = .Recipients.Add("test123@yahoo.com")
        oReci.Type = 2

        .Subject = "This is a Test"
        .Body = "This is the body of the message." & vbCrLf & vbCrLf

        .Attachments.Add "C:\image1.jpg"
        .Attachments.Add "C:\image2.jpg"

        For Each oReci In .Recipients
            oReci.Resolve
        Next

        '.Display
        .Save
        .Send
        
    End With
    Set olApp = Nothing

End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
ID: 34214726
Do you intend to send one message with more than one attachment to one Recipient?
Can we assume pictures with known paths, like "C:\p1.jpg", "C:\p2.jpg", ....?
0
 
LVL 30

Expert Comment

by:hnasr
ID: 34214743
I can see  gbanik assumed the paths and multiple recipients. Check his comment!

Use olTo instead of 1 for type, and olCC instead of 2.
0
 
LVL 2

Author Comment

by:bignadad
ID: 34214994
Im getting an object/application defined error on this line

        .Attachments.Add "C:\image1.jpg"
0
 
LVL 2

Author Comment

by:bignadad
ID: 34215023
btw i also go the error on this line
        oReci.Type = 1
but when changine to olTo it fixed....

Also, i have the image1.jpg on the c:\
0
 
LVL 13

Expert Comment

by:gbanik
ID: 34215585
The best way to code for Outlook mails is to set the mail object to visible and then make alterations (and step through breakpoint) ... so you know which code is doing what.

To do so just use
olMail.display
in the code.

Please debug the code and check where exactly the error is coming. This will help resolve easily.
0
 
LVL 2

Author Comment

by:bignadad
ID: 34220479
Little confused on the breakpoints.. i tried the olMail.display and still getting same error as above..

it does not seem to process .Attachments.Add
0
 
LVL 2

Author Comment

by:bignadad
ID: 34225583
If i cant send through outlook is there another method?

i basically need a user to click a button and attach the pictures to an email to send to a recipient they will type in.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 34225845
Assume environment:

Disk drive C:
Image files C:\f1.jpg, C:\f2.jpg
A form: Form1 with a textbutton: txtEmail
A button:cmdSend

cmdSend_Click to send email to recipient in txtEmail with f1, mand f2 images attached.
Is this scenario ok?
0
 
LVL 2

Author Comment

by:bignadad
ID: 34225850
Yes, That scenario is fine.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 34225959
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 30

Expert Comment

by:hnasr
ID: 34226016
add 2 images to C: drive, f1.jpg and f2.jpg. You may use D: or any permissible path.
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226034
tried to enter an email address and clicked butt and it gives me following error

Run-Time error 287
Application-defined or object-defined error

highlights this line
Set objOutlookRecip = objOutlookMsg.Recipients.Add(Me![txtEmail])

when i rollover Set objOutlookRecip is says it = Nothing
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226054
Nevermind its working now... Had to have outlook up and running....

give me just a sec to test further
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226176
it works out of the dbase you sent but when i use your code in my database im getting an error

Compile Error
User-defined type not defined

It highlights
Private Sub cmdSend_Click()

and the following text is selected
objOutlook As Outlook.Application
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226351
Still trying to see why it wont work in my database, and not having much luck..

when i open your file it shows up as a mdb and mine is a accdb (later version)
this wouldn't be conflicting would it?
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226364
i guess so.. i tried copying your frm from access 2000 version into a new blank database and got same error....

this has to be done in a access 2000 environment?
0
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 250 total points
ID: 34226391
Missing reference to Outlook Object Library.

Show code editor.
Select menu Tools-References
Tick Microsoft Outlook 12.0 Object Library
Click OK
Run and try.
0
 
LVL 13

Expert Comment

by:gbanik
ID: 34226436
Again bignadad, could you run my code again? You should be able to do it with this code. Try after adding references.
Option Explicit

Public Sub SendMail()

    Dim olApp
    Set olApp = CreateObject("Outlook.Application")

    Dim olMail, oReci
    Set olMail = olApp.CreateItem(0)

    With olMail
        
        Set oReci = .Recipients.Add("oneperson@gmail.com")
        oReci.Type = 1

        Set oReci = .Recipients.Add("test123@yahoo.com")
        oReci.Type = 2

        .Subject = "This is a Test"
        .Body = "This is the body of the message." & vbCrLf & vbCrLf

        .Attachments.Add "C:\image1.jpg"
        .Attachments.Add "C:\image2.jpg"

        For Each oReci In .Recipients
            oReci.Resolve
        Next

        '.Display
        .Save
        .Send
        
    End With
    Set olApp = Nothing

End Sub

Open in new window

0
 
LVL 2

Author Comment

by:bignadad
ID: 34226437
Sweet.. works great

one more question... when outlook comes up it shows a allow/deny security windows that i have to click 3 times before it will send email.. any way to turn that off?
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226440
gbanik

your code would have worked fine .. my biggest problem was i didnt have outlook setup and didnt have it in references of visual basic...

thanks for everyone's help
i do have another question i am submitting that i need help figuring out....
0
 
LVL 30

Expert Comment

by:hnasr
ID: 34226484
This may be related to User Account Control
Check:
http://technet.microsoft.com/en-us/library/cc709691(WS.10).aspx
0
 
LVL 2

Author Comment

by:bignadad
ID: 34226550
i have UAC turned off (never notify)

i looked within outlook security settings and didnt see anything there either
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now