PDA

View Full Version : Sub routine isn't sending outlook message..



DanOfEarth
06-17-2010, 10:26 AM
This sub routine creates an email, adds attachents, uses a MS Word doc for the body, then sends the email off.

I was adding things to my sub here, and suddenly when I hit the button to run this, it no longer "sends" the email. It just saves a draft of it in Outlook. Very weird.

Sub SendDocAsMsg2()
Dim wd As Object
Dim doc As Object
Dim itm As Object
Dim ID As String
Dim blnWeOpenedWord As Boolean
On Error Resume Next

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:\Documents and Settings\Dan\Desktop\SHORT SELL\Email3.docx", ReadOnly:=True)
Set itm = doc.MailEnvelope.Item
With itm
.To = ActiveCell.Text
.Subject = "Important: A bulk of the Contracts to sign. Please forward to seller"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9a - Authorization 1st Mortgage - MK - " & Range("Address") & ".pdf"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9b - Authorization 1st Mortgage - ZS - " & Range("Address") & ".pdf"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "13 - HAFA Letter - " & Range("Address") & ".pdf"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "12 - Notice of Contract - " & Range("Address") & ".pdf"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "17 - Agreement of Negotiator - " & Range("Address") & ".pdf"
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "10a - Termination of Authorization - 1st - " & Range("Address") & ".pdf"
If Dir(Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9c - Authorization 2nd Mortgage - MK - " & Range("Address") & ".pdf") <> "" Then
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9c - Authorization 2nd Mortgage - MK - " & Range("Address") & ".pdf"
Else
End If
If Dir(Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9d - Authorization 2nd Mortgage - ZS - " & Range("Address") & ".pdf") <> "" Then
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "9d - Authorization 2nd Mortgage - ZS - " & Range("Address") & ".pdf"
Else
End If
If Dir(Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "10b - Termination of Authorization - 2nd - " & Range("Address") & ".pdf") <> "" Then
.Attachments.Add Range("DIRECTORY") & Range("Agent") & " - " & Range("Address") & "\Orig Docs\" & "10b - Authorization 2nd Mortgage - 2nd - " & Range("Address") & ".pdf"
Else
End If

.Save
ID = .EntryID

Set itm = Application.Session.GetItemFromID(ID)
itm.Send
doc.Close wdDoNotSaveChanges
End With

If blnWeOpenedWord Then
wd.Quit
End If

Set doc = Nothing
Set itm = Nothing
Set wd = Nothing

End Sub



As noted in the red script, I've got the .Send in there. Any thoughts?

p45cal
06-17-2010, 01:43 PM
Temporarily comment out the
On Error Resume Next
and step through it with F8

DanOfEarth
06-17-2010, 03:22 PM
Hmmm....

I did that and now it's flagging the line:

Set wd = GetObject(, "Word.Application")


Wasn't expecting that one. And...that's not even related to the ".Send" problem.....

p45cal
06-17-2010, 03:47 PM
OK, leave the On Error resume Next where it was, but add

On Error Goto 0 (that's a zero at the end)
immediately before the line which starts:
Set doc = wd.Documents.Open...Now step through it.

DanOfEarth
06-17-2010, 03:59 PM
Cool,

What it's flagging is the line near the end:

Set itm = Application.Session.GetItemFromID(ID)

I'm not very familiar with that method.

p45cal
06-17-2010, 04:39 PM
Nor am I.
Do you need the lines:
ID = .EntryID
Set itm = Application.Session.GetItemFromID(ID) What are they there for? Won't it work without them?

I'd guess that the Application referred to in Application.Session.GetItemFromID(ID) is the Excel application which doesn't have a session property. Outlook does, and Outlook is itm's parent so
ID = .EntryID
Set itm = itm.Application.Session.GetItemFromID(ID) might work.
If not, should there be a reference to MS Outlook in Tools|References of the VBE?

I don't code for Outlook a lot.



Cool,

What it's flagging is the line near the end:

Set itm = Application.Session.GetItemFromID(ID)
I'm not very familiar with that method.

DanOfEarth
06-17-2010, 04:47 PM
I also don't know Outlook much...or rather it's been since Outlook 2000 that I've messed with it. I copped the original script somewhere and added a bunch of things, and it was working great.

Then "poof".

I'll google that one....

DanOfEarth
06-17-2010, 05:31 PM
I dunno,

According to what I know, it shouldn't be flagging that as an error.

p45cal
06-17-2010, 06:05 PM
Did my suggestion work? (Set itm = itm.Application.Session.GetItemFromID(ID) )
it did here.

Tinbendr
06-17-2010, 06:09 PM
Set itm = doc.MailEnvelope.Item
With itm
'.......

Set itm = Application.Session.GetItemFromID(ID)
itm.Send
doc.Close wdDoNotSaveChanges
End With

Could it be that you reassigned the object INSIDE the With?

Should it be?

doc.Close wdDoNotSaveChanges
End With

Set itm = Application.Session.GetItemFromID(ID)
itm.Send

DanOfEarth
06-17-2010, 06:22 PM
Actually,

I already moved that back, and it still flags the same line.

Shred Dude
09-29-2010, 08:38 PM
In my first read through that code what jumped out at me was the line:


Set itm = Application.Session.GetItemFromID(ID)

Since you refer to "ActiveCell" earlier in the routine I'm assuming you're running this from within Excel. So, your reference to "Application" would be to the Excel Application, when what you need there is the Outlook.Application.

You can either start an instance of Outlook and do the Send, or go to Outlook and send it. Try using the same lines of code you use near the beginning to get at Word, modified for "Outlook.Application"


As an observation, I don't see you making any modifications to that Word Document. You're sending it as is. A boilerplate document?

Using the mailenvelope of the Word document will create an email with the document displayed as an HTML body within the mail item. That makes the email look nice when the boilerplate document contains lots of formatting and/or graphics. Since you're not changing that document at all however, you might consider just creating a mail item, with a simple message, and then attach the boilerplate letter as an attachment along with all the other pdf docs. That way you eliminate the mailenvelope and could use a simpler set of code to create and send the email message.

I take it you've read through Ron DeBruin's site on sending emails from Excel?

Good luck with it!

Shred

DanOfEarth
09-30-2010, 06:59 AM
Actually,

Wow,,,,it's been awhile since I messed with that code.

I tried both ways.

But originally, the issue was that I was using the main paragraph of the word doc as the content or main paragraph of my email, using excel to write the greeting. I then added the attachments programatically as well. It was working fine until a made a change somehwere and I just could NOT debug it. I'll check out Ron Bruin's site, although that sounds extremely familiar. Let me look at that code again.