J
JohS
Hi. Would appreciate some feedback telling me why the code doesn't hit its
Recipient?
(The Email ends up in draft (and sometimes even in the Inbox) and I guess it
has something to do with the lack of recipient). In advance thanks, JohS
Private Sub btnSendEmail_Click()
Dim objword As Word.Application
Dim sMal As String
Dim DB As Database
Dim rec As Recordset
Dim sSql As String
On Error Resume Next
DoCmd.Hourglass True
Set DB = CurrentDb()
'Henter inn alle opplysninger om kunden som ble valgt
sSql = "SELECT tblKunder * FROM tblKunder WHERE (((tblKunder.FirmaNavn)= " &
Forms!frmKunder.lstFirmaNavn & "))"
Set rec = DB.OpenRecordset(sSql)
If Not rec.BOF Then
'Lager referanse til Word
strWordMal = Application.CurrentProject.Path & "\invitasjon.dot"
On Error Resume Next
Set objword = GetObject(, "Word.application")
If objword Is Nothing Then Set objword = CreateObject("Word.application")
sMal = objword.Documents.Add(Template:=strWordMal)
'Åpner et dokument basert på valgt mal
objword.Documents.Add sMal
'Aktiverer, synligjør og maksimerer Word
objword.Application.Activate
objword.Application.Visible = True
'objword.Application.WindowState = wdWindowStatemaximize
'Fyller inn tekst ved bokmerkene
objword.ActiveDocument.Bookmarks("bookmarkFirmanavn").Select
If Not IsNull(rec!lstFirmaNavn) Then objword.Selection.Text =
Me!lstFirmaNavn
objword.ActiveDocument.Bookmarks("bookmarkKontaktEmail").Select
If Not IsNull(rec!lstKontaktEmail) Then objword.Selection.Text =
Me!lstKontaktEmail
'Lager referanse til Word
strWordSave = Application.CurrentProject.Path & "\invitasjon.doc"
objword.ActiveDocument.SaveAs (strWordSave)
objword.Application.Visible = False
objword.Quit
Set objword = Nothing
End If
DoCmd.Hourglass False
DoCmd.Close
Dim objOutlook As Outlook.Application
Dim objMessage As Outlook.MailItem
Dim objEmailReceivers As Outlook.Recipient
Dim objAttachment As Outlook.Attachment
Dim strError As String
strError = "Mangler info"
Set objAttachment = strWordSave
Set objOutlook = GetObject(, "Outlook.application")
If objOutlook Is Nothing Then Set objOutlook =
CreateObject("Outlook.application")
Set objMessage = objOutlook.CreateItem(olMailItem)
With objMessage
Set objEmailReceivers = .Recipients.Add(Me!lstKontaktEmail)
.Subject = " test"
.Body = "test" & vbCrLf
If Not IsMissing(objAttachment) Then Set objAttachment =
..Attachments.Add(strWordSave)
.Save
.Send
'End If
End With
objOutlook.ActiveWindow
'objOutlook.Quit
Set objOutlook = Nothing
sMal = ""
Set DB = Nothing
Set rec = Nothing
sSql = ""
End Sub
Recipient?
(The Email ends up in draft (and sometimes even in the Inbox) and I guess it
has something to do with the lack of recipient). In advance thanks, JohS
Private Sub btnSendEmail_Click()
Dim objword As Word.Application
Dim sMal As String
Dim DB As Database
Dim rec As Recordset
Dim sSql As String
On Error Resume Next
DoCmd.Hourglass True
Set DB = CurrentDb()
'Henter inn alle opplysninger om kunden som ble valgt
sSql = "SELECT tblKunder * FROM tblKunder WHERE (((tblKunder.FirmaNavn)= " &
Forms!frmKunder.lstFirmaNavn & "))"
Set rec = DB.OpenRecordset(sSql)
If Not rec.BOF Then
'Lager referanse til Word
strWordMal = Application.CurrentProject.Path & "\invitasjon.dot"
On Error Resume Next
Set objword = GetObject(, "Word.application")
If objword Is Nothing Then Set objword = CreateObject("Word.application")
sMal = objword.Documents.Add(Template:=strWordMal)
'Åpner et dokument basert på valgt mal
objword.Documents.Add sMal
'Aktiverer, synligjør og maksimerer Word
objword.Application.Activate
objword.Application.Visible = True
'objword.Application.WindowState = wdWindowStatemaximize
'Fyller inn tekst ved bokmerkene
objword.ActiveDocument.Bookmarks("bookmarkFirmanavn").Select
If Not IsNull(rec!lstFirmaNavn) Then objword.Selection.Text =
Me!lstFirmaNavn
objword.ActiveDocument.Bookmarks("bookmarkKontaktEmail").Select
If Not IsNull(rec!lstKontaktEmail) Then objword.Selection.Text =
Me!lstKontaktEmail
'Lager referanse til Word
strWordSave = Application.CurrentProject.Path & "\invitasjon.doc"
objword.ActiveDocument.SaveAs (strWordSave)
objword.Application.Visible = False
objword.Quit
Set objword = Nothing
End If
DoCmd.Hourglass False
DoCmd.Close
Dim objOutlook As Outlook.Application
Dim objMessage As Outlook.MailItem
Dim objEmailReceivers As Outlook.Recipient
Dim objAttachment As Outlook.Attachment
Dim strError As String
strError = "Mangler info"
Set objAttachment = strWordSave
Set objOutlook = GetObject(, "Outlook.application")
If objOutlook Is Nothing Then Set objOutlook =
CreateObject("Outlook.application")
Set objMessage = objOutlook.CreateItem(olMailItem)
With objMessage
Set objEmailReceivers = .Recipients.Add(Me!lstKontaktEmail)
.Subject = " test"
.Body = "test" & vbCrLf
If Not IsMissing(objAttachment) Then Set objAttachment =
..Attachments.Add(strWordSave)
.Save
.Send
'End If
End With
objOutlook.ActiveWindow
'objOutlook.Quit
Set objOutlook = Nothing
sMal = ""
Set DB = Nothing
Set rec = Nothing
sSql = ""
End Sub