PDA

View Full Version : Problem with sending mail within Form



mod2000
08-11-2011, 06:04 AM
Hi,
I have this simple problem. I am using this code to send mail:
Sub SendDocAsMsg()
Dim wd As Word.Application
Dim doc As Word.Document
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:\Current.doc", ReadOnly:=True)
Set itm = doc.MailEnvelope.Item
With itm
.To = "somemailhere"
.subject = "Subject"
.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
It works fine.

But when I make some form, and put CommandButton on it and do function like:
Private Sub CommandButton1_Click()
Call SendDocAsMsg
End Sub
it just doesn't work. No error shows, and e-mail is saved in drafts folder.
Why is that? What is the reason?