There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Office Automation Access/VB/Outlook


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
28-Feb-2004, 05:17 AM #1
Office Automation Access/VB/Outlook
I'm having a sport of bother using email via Outlook in VB within Access.

The procedure below is called to send a number of emails and all works OK. If I run the process again, it simply hangs (although it looks as though it managed to send the first message). Presumably Outlook is waiting/looking for something. Have I perhaps not closed the first process correctly? The basis of the code was grabbed from the Micro$oft site (which may be the root of the problem!)


Sub SendEmail(stTo As String, stPath As String)
' Start Outlook. If it's already running, we'll use the same instance...

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

' Logon. Doesn't hurt if we are already running and logged on...

Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon

' Send a message

Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

' Fill out & send message...

With olMail
.To = stTo
.Subject = "Test attached file"
.Body = "File attached" & vbCrLf & stTo & vbCrLf & stPath
.Attachments.Add stPath
.Send
End With

' Clean up...

olNs.Logoff

Set olNs = Nothing
Set olMail = Nothing
Set olApp = Nothing

End Sub
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
28-Feb-2004, 01:57 PM #2
I don't have Outlook installed (don't really want to), but notice in your code that you reference olMailItem when you set olMail, but where is your dim for olMailItem....

have you stepped through the code on both the first and second instances?

MBN
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
29-Feb-2004, 07:29 AM #3
That's a very good question! I lifted the code from http://support.microsoft.com/?kbid=220595 and there's no reference to it! I'll stick one in and see what happens.

I also would rather not have Outlook running but it seems I need it for this.

Stepping through doesn't help since it goes into one of the statements and never comes back again!

The whole process has no problems if Outlook is actually running in the first place.
__________________
Ken.

To err is human but to really foul things up takes a computer!
Rockn's Avatar
Computer Specs
Distinguished Member with 17,888 posts.
 
Join Date: Jul 2001
Location: Mexico of the North, MN
Experience: Disenfranchised American Male
29-Feb-2004, 12:13 PM #4
I was just reading something on this last night and cannot find the chapter at the moment. It had to do with closing the current session prior to open ing a new one, something to that effect.
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
29-Feb-2004, 01:56 PM #5
Stepping through doesn't help since it goes into one of the statements and never comes back again!

Doesn't it ever come back on the first pass, in order to complete the cleanup? At which stmt does it disappear?

MBN

PS: just took a look at that code from MS....if I were you, I'd start a new project, copy the whole thing in, and make sure it works as written. When that works, piece by piece remove the references to the other items (contact and appointment), and at each step test for sending success. But if you haven't tested the MS code in its entirety, you don't know if it will work to begin with.
__________________
I'll burn that bridge when I get to it!
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
29-Feb-2004, 06:18 PM #6
On the first pass it works fine. All statements are executed and the thing is closed proerly (assuming the code is correct!). The second time through it goes into

Set olMail = olApp.CreateItem(olMailItem)

never to appear again and the task has to be aborted. Even then, there is still an outlook.exe process running which also has to be aborted before the thing can be tried again.

I'll see whether the MS code is as rugged as I'd like.

I can get my process to work fine if I stick in a 'start up outlook' with
RetVal = Shell("outlook", vbMinimizedNoFocus)
although it doesn't start minimised and it does get the focus. Also a new instance starts every time and I've not (yet) found a way to close it down after I've sent the emails!


In response to an earlier question...
"but where is your dim for olMailItem"
olMailItem is, of course, an intrinsic constant.
__________________
Ken.

To err is human but to really foul things up takes a computer!
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
29-Feb-2004, 06:36 PM #7
Okay, I went ahead and installed Outlook (I have 2003), then used the MS version of the code - it worked.

I then used yours, and yes, it worked on two consecutive runs....the only difference is that I used a click event to point to your sub, and while in the click event I set the stTo and sPath variables there.

I can only assume, then, that it may have something to do with your attachment or address.....

I also do not have a problem with outlook opening. I just get this small warning window (jpg attached) when I click Yes, it sends the email.

MBN
Attached Thumbnails
office-automation-access-vb-outlook-outlookmessage.jpg  
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
29-Feb-2004, 06:41 PM #8
PS: just to add to your confusion.....it behaved differently with and without the msgbox after the .send.

hmmmmmmm

MBN
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
29-Feb-2004, 06:55 PM #9
Okay, had to disable my Norton AV and still found that if the process was too fast (I tried to send 3 in rapid succession), at least one of them did not arrive and the little app threw an error msg.

How many emails are you trying to put out at any given time?

MBN
__________________
I'll burn that bridge when I get to it!
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
01-Mar-2004, 03:03 AM #10
In my testing I'm sending 3 messages. My AV product is AVG free version which doesn't interecpt the outgoing messages in the same way and so is not a speed problem.

Also, I'm using Outlook 2000. I'm expecting to get the latest version later this week and I'll see if it behaves the same.

WRT the initiating of this procedure, I've now incorporated the code into the original click event and it behaves the same way.

Thanks for all your efforts. It's most helpful and suggests that my environment or version 2000 of Outlook may be behind the problem!
__________________
Ken.

To err is human but to really foul things up takes a computer!
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
01-Mar-2004, 12:20 PM #11
Glad to help, it was interesting to do.......

MBN
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
01-Mar-2004, 01:29 PM #12
Now don't you just hate it when...
You copy the whole thing to a different logical disk drive and the problem goes away! (and no, it wasn't a disk space issue).

To err is human but to truly **** things up akes a computer!
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
01-Mar-2004, 01:32 PM #13
from where to where?

Also, do you get the Yes/No msgbox when it accesses Outlook? Have you found any way to get rid of it?

MBN
Trentham's Avatar
Senior Member with 204 posts.
 
Join Date: Nov 2002
Location: Derbyshire, England
Experience: Advanced
02-Mar-2004, 03:08 AM #14
Went from drive L to drive C. Same physical drive. On checking I see that L is FAT (!) while C is FAT32.

I've never had the msg yes/no box appear. I'm not sending a jpg, just a txt, though I don't see that that would make any difference.

In an earlier incarnation I was sending via Outlook Express (though that couldn't handle attachments) and had to send an option to allow external programs to send emal silently.
__________________
Ken.

To err is human but to really foul things up takes a computer!
MustBNuts's Avatar
Senior Member with 1,930 posts.
 
Join Date: Aug 2003
Location: Nevada
02-Mar-2004, 12:54 PM #15
hmmmm, you aren't getting the yes/no box (I attached a jpg of it ealier)?

Are you still using the option to send silently? What sort of code?

MBN
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:42 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.