Problem with setting object using session ID (emailing ffrom VBA)

M

Mark Stephens

Hi all,

I found a tremendous chunk of code (see below) at :
http://www.outlookcode.com/codedetail.aspx?id=1333

It works a treat except for one thing, it creates the email but does not
send it (I am using Outlook 2007); it puts it in the drafts folder and you
then have to send it manually. I have had a go at debugging it and it seems
that the line:

Set itm = Application.Session.GetItemFromID(ID)

(near the end) is failing as when I break before the next line (itm.Send)
itm is empty (i.e. the code returned nothing).

Does anyone have any idea how I can fix this so the email just sends
automatically rather than being placed in the drafts folder and/or how to
fix the line that isn't working if they are not one and the same problem
(which I suspect they are).

Thanks and regards, Mark

_____________________________________________________________________

Sub SendDocAsMsg()
Dim wd As Word.Application
Dim doc As Word.Document
Dim itm As Object
Dim ID As String
Dim blnWeOpenedWord As Boolean
Dim sSubject As String
Dim sEmailAddress As String


On Error Resume Next

sSubject = "Follow up"
sEmailAddress = "(e-mail address removed)"

Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
blnWeOpenedWord = True
End If
Set doc = wd.Documents.Open(Filename:="C:\WRWRInfo1.docx",
ReadOnly:=True)
Set itm = doc.MailEnvelope.Item

With itm
.from = sEmailAddress
.To = sEmailAddress
.CC = sEmailAddress
.Subject = sSubject
.Save
ID = .EntryID
End With

Set itm = Nothing

Set itm = Application.Session.GetItemFromID(ID)
itm.Send
doc.Close wdDoNotSaveChanges
If blnWeOpenedWord Then
wd.Quit
End If

Set doc = Nothing
Set itm = Nothing
Set wd = Nothing
End Sub
 
S

Sheeloo

I could not compile it with Word 2007 and got the error "Method or data
member not found" pointing to Session...

But why are you even doing that? Simply replace .Save by .Send like in the
code below
With itm
.from = sEmailAddress
.To = sEmailAddress
.CC = sEmailAddress
.Subject = sSubject
.Save
ID = .EntryID
End With

and comment out the lines
'Set itm = Application.Session.GetItemFromID(ID)
'itm.Send



:
 

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