Problemer med Outlook send mail
Hejsa - Har vist fået lidt hjælp omkring det her før, men nu er der noget galt igen.Hvis ikke begge email felter er udfyldt, så får jeg en fejl. Hvor er det lige i koden, jeg skal have noget rettet til.
Kode:
Private Sub mailconfirmation_Click()
On Error GoTo Err_mailconfirmation_Click
Dim stDocName As String
Dim stLinkCriteria As String
Dim Beskedtekst As String
Dim A As String
Dim B As String
Dim C As String
Dim K As String
stDocName = "Confirmation PDF"
stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenReport stDocName, acNormal, , stLinkCriteria
If IsNull(Trim(Me.Email)) = True Or IsEmpty(Trim(Me.Email)) = True Then
A = "none"
Else
A = Me.Email
End If
If IsNull(Trim(Me.Kontaktemail)) = True Or IsEmpty(Trim(Me.Kontaktemail)) = True Then
C = "none"
Else
C = Me.Kontaktemail
End If
B = "Confirmation # " & Me.ID + 10000
K = "C:\Documents and Settings\" & Environ("Username") & "\Skrivebord\Confirmation.pdf"
Beskedtekst = "Attention: " & Me.Kontaktperson & vbNewLine & vbNewLine
Call SendMessage(True, A, C, B, Beskedtekst, K)
Exit_mailconfirmation_Click:
Exit Sub
Err_mailconfirmation_Click:
MsgBox Err.Description
Resume Exit_mailconfirmation_Click
End Sub
Mailsendefunktion:
Public Function SendMessage(DisplayMsg As Boolean, ParRecipient As String, ParCCRecipient As String, ParSubject As String, ParBody As String, Optional AttachmentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
' Opret Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Opret meddelelsen.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
If ParRecipient = "none" And ParCCRecipient = "none" Then
MsgBox "No Email Address Available" & Chr(13) & "Sending is cancelled", , "Error"
Exit Function
Else
If ParRecipient = "none" Then
Else
If IsEmailValid(ParRecipient) = True Then
' Tilføj modtager.
Set objOutlookRecip = .Recipients.Add(ParRecipient)
objOutlookRecip.Type = olTo
Else
MsgBox "Main Email isn't valid:" & Chr(13) & ParRecipient, vbQuestion, "Error"
Exit Function
End If
End If
If ParCCRecipient = "none" Then
Else
If IsEmailValid(ParCCRecipient) = True Then
'Tilføj evt. CC modtager.
Set objOutlookRecip = .Recipients.Add(ParCCRecipient)
objOutlookRecip.Type = olCC
Else
MsgBox "Contact Email isn't valid:" & Chr(13) & ParCCRecipient, vbQuestion, "Error"
Exit Function
End If
End If
End If
' Sæt Emne, Indhold og Prioritet.
.Subject = ParSubject
.Body = ParBody
.Importance = olImportanceNormal 'High importance
' Tilføj evt. vedhæftet fil.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
' Kontroller navne.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Skal meddelelsen vises inden afsendelse?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Function
