Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

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

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
bignadad
Asked:
bignadad
  • 13
  • 7
  • 3
2 Solutions
 
gbanikCommented:
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
 
hnasrCommented:
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
 
hnasrCommented:
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
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
bignadadAuthor Commented:
Im getting an object/application defined error on this line

        .Attachments.Add "C:\image1.jpg"
0
 
bignadadAuthor Commented:
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
 
gbanikCommented:
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
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
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
 
hnasrCommented:
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
 
bignadadAuthor Commented:
Yes, That scenario is fine.
0
 
hnasrCommented:
0
 
hnasrCommented:
add 2 images to C: drive, f1.jpg and f2.jpg. You may use D: or any permissible path.
0
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
Nevermind its working now... Had to have outlook up and running....

give me just a sec to test further
0
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
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
 
hnasrCommented:
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
 
gbanikCommented:
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
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
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
 
hnasrCommented:
This may be related to User Account Control
Check:
http://technet.microsoft.com/en-us/library/cc709691(WS.10).aspx
0
 
bignadadAuthor Commented:
i have UAC turned off (never notify)

i looked within outlook security settings and didnt see anything there either
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 13
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now