Private Sub CommandButton2_Click() 'http://www.paulsadowski.com/WSH/cdo.htm
Dim objFile As Object
Dim objFilSysObj As Object
Dim objTexStr As Object
Dim objMessage As Object
Dim strFile As String
Dim strHTMLBody As String
''Bereik E1:G5 Tekst in Cel(E1:G5)
''SMTP-server smtp.gmail.com (Cel C3)
''Gebruikersnaam invoeren1 (Cel C4)
''Wachtwoord invoeren (Cel C5)
''Onderwerp test1 (Cel C6)
''Van naam Cees (Cel C7)
''Van E-mailadres [email]invoeren1@gmail.com[/email] (Cel C8)
''Aan E-mailadres [email]jan@hetnet.nl[/email] (Cel C9)
Set objMessage = CreateObject("CDO.Message")
With objMessage
.Subject = Range("Onderwerp").Value
.From = String(2, 34) & Range("Van_naam").Value & String(2, 34) & " <" & Range("Van_E_mailadres").Value & ">"
.To = Range("Aan_E_mailadres").Value
Range(Range("Bereik")).Copy
Set objFile = Workbooks.Add(1)
With objFile.Sheets(1)
.Cells(1).PasteSpecial 8 'kolom breedte, pakt geen xl-waarde door fout in excel
.Cells(1).PasteSpecial xlPasteValues
.Cells(1).PasteSpecial xlPasteFormats
.Cells(1).Select
Application.CutCopyMode = False 'verwijder cursor
End With
strFile = ThisWorkbook.Path & "/Tijdelijk.htm"
With objFile.PublishObjects.Add(xlSourceRange, strFile, objFile.Sheets(1).Name, objFile.Sheets(1).UsedRange.Address, xlHtmlStatic)
.Publish (True)
End With
Set objFilSysObj = CreateObject("Scripting.FileSystemObject")
Set objTexStr = objFilSysObj.GetFile(strFile).OpenAsTexTStream(1, -2)
strHTMLBody = objTexStr.ReadAll
objTexStr.Close
Kill strFile
objFile.Close (False)
Set objTexStr = Nothing
Set objFilSysObj = Nothing
strHTMLBody = Replace(strHTMLBody, "align=center x:publishsource=", "align=left x:publishsource=") 'links uitlijnen
.HTMLBody = strHTMLBody
With .configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Range("Gebruikersnaam").Value
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Range("Wachtwoord").Value
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True 'DEZE REGEL IS NODIG
.Update
End With
.Send
End With
End Sub