MailItem Attachments Inconsistency

G

Geoff

Outlook 2002:

Attachments to a Mailitem object are not attached when the code runs at
normal speed, but are attached if I place a Break on the Display line (last
line in code snippet below) and press F8 to execute the Display statement in
step mode. Although the MailItem is saved before attaching the attachments,
I'm still getting this inconsistent result. It seems like a timing issue,
but DoEvents doesn't help. Any clues please?

Very many thanks.
Regards
Geoff

Dim objMI as Outlook.MailItem
Dim objATTACHMENTS as Outlook.Attachments

Set objMI = mobjOL.CreateItem(olMailItem)
.... some objMI properties set here, like "To"...
objMI.Save
Set objATTACHMENTS = objMI.Attachments
objATTACHMENTS.Add "PathNameToFile", olByValue

objMI.Display
 
G

Geoff

Hi Ken,
Try saving the item again after you add the attachments.

Thanks for your reply.

I thought you might like to know the rather interesting result.

It transpires my clean up code was in error. By trying to ensure that the
attachments object variable was "properly" destroyed, my clean up code
removed each attachment from the attachments collection before setting the
attachments collection variable to nothing. Naturally, the clean up code ran
*after* the Display method was used, so the MailItem was already on screen
when the clean up code ran. Although it happened too fast to see it, the
attachments were in the MailItem initially but were removed so fast as not
to be seen.

What's interesting is that, if the code was stopped any time after the
Display method but before the clean up, the attachments could be seen in the
MailItem. Stepping through the clean up code showed each attachment being
removed one at a time from the MailItem as the attachment was removed from
the attachments collection.

Interestingly, as might be expected, this behaviour was *not* replicated for
the Send method. Presumably, after Send, the MailItem is already in the
Outbox by the time my clean up code ran and it was too late for the clean up
code to remove attachments.

Omitting removal of attachments from the attachments collection solved the
problem when using the Display method.

If you (or others) have any further comments, I'd be glad to hear them.

Thanks for your interest.
Regards
Geoff
 
G

Geoff

If the user hasn't started Outlook, is there a way of making Outlook visible
after CreateObject("Outlook.Application")?

TIA.
Geoff
 
K

Ken Slovak - [MVP - Outlook]

Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oExpl As Outlook.Explorer

Set oNS = oOL.GetNameSpace("MAPI")
oNS.Logon

Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
Set oExpl = oInbox.GetExplorer
oExpl.Display
 
G

Geoff

Hi Ken,

Very many thanks. Easy when you know how!

I was pretty ashamed at having to ask such a basic question. But by way of
explanation, I'm more used to the Access, Word and Excel object models. The
Outlook object model is a mystery. From where I'm coming from, it seemed
logical to look for an Application.Visible property or an Application.hWnd
property (to use an API call). It didn't occur to me that getting an
explorer on one folder within the application would reveal the whole
application.

I've read the wrong books on Outlook. I have several, but not, I regret to
say, by you.

I'm primarily an Access developer. Do you have any recommendations for an
Outlook reference I can dip into to find solutions, not a course book that
makes too many presuppositions about earlier knowledge? I'm not a serious
Outlook developer but I want a good grounding in Outlook programming basics.
I'll do course work if it's engaging, not if it's dry as dust.

I see some of your older books are being sold at bargain prices on Amazon.
Given the above, would any of them fit the bill for an Office 2002
developer? Don't be bashful but an honest answer please! We retired nerds
are not rolling in it you know! I'm happy to invest in several. And a good
one for the future direction of Outlook.

Let me know if you can't answer for Newsgroup conduct reasons and perhaps I
could try you at mvps?

Thanks again.
Regards
Geoff
 
K

Ken Slovak - [MVP - Outlook]

There's virtually nothing out there that deals with things on the level of
hWnd's or Win32 API calls. That sort of stuff is all undocumented and what I
know about it is what I've hacked. By the same token try to find any Office
app that exposes a hWnd property. When I need that, which I often do I first
try GetForeGroundWindow and from there move to more obscure coding,
especially when WordMail windows are involved.

For a beginner in Outlook programming I'd highly recommend Sue Mosher's
book. It's geared for beginners who know VBA/VBScript/VB and covers Outlook
VBA and forms, but not COM addins.

The only Outlook 2002 programming books I know of are Randy Byrne's Building
Applications with Microsoft Outlook 2002 from MS Press and Tom Rizzo's
Programming Outlook & Exchange, also from MS Press. I think Randy's is
better, but neither is a beginner Outlook book. My Wrox book is geared to
the professional level also, the Sams book was written for beginners but
marketed for professional level. I have reservations about that book, the
lead author wasn't really an Outlook programmer (although my chapters are
excellent <g>).

There will be a few Outlook 2007 programming books coming out, Randy is
doing one and so am I. Sue is also updating her book. I'm not sure about
Tom, I haven't spoken to him in a while.

I'd say start with Sue's book: .
She's a great teacher and writes very clearly and her credentials are
impeccable. That I was her tech editor I don't think prejudices me very much
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top