AW: Chart als HTML in Email Body kopieren
29.11.2019 08:55:42
Julian
Hallo,
es tut mir Leid. Ich bin neu hier und den Regeln nicht bewusst.
Meine Code sieht momentan wie folgt aus:
Sub direct_mail()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim eingabe As String
Dim name As String
Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("rngTabWorksheet").Range("A177:D338").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
eingabe = (Environ("USERNAME"))
name = (Environ("USERNAME"))
' If no entry exits program
If StrPtr(eingabe) = 0 Then Exit Sub
If eingabe = "" Then
MsgBox "Sie wurden nicht als autorisierter Nutzer erkannt.", vbCritical, "Fehlgeschlagen!" _
Exit Sub
ElseIf eingabe = "Beispiel1" Then
eingabe = "Beispiel@Beispiel.de"
ElseIf eingabe = "Beispiel2" Then
eingabe = "Beispiel2@Beispiel.de"
Exit Sub
End If
If name = "" Then
Exit Sub
ElseIf name = "jkr" Then
name = "Julian"
ElseIf name = "Beispiel" Then
name = "Beispiel"
Exit Sub
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = eingabe
.CC = ""
.BCC = ""
.Subject = "Produktionsbericht Bereich Metallbau - Stand " & Sheets("rngTabWorksheet"). _
Range("A2").Value
.HTMLBody = "Hallo " & name & ", folgend bekommst Du wie gewünscht den geforderten Bericht." _
& RangetoHTML(rng)
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Send
End With
On Error GoTo 0
MsgBox "Senden erfolgreich!", vbInformation, "Erfolgreich!"
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' By Ron de Bruin.
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
FileName:=TempFile, _
Sheet:=TempWB.Sheets(1).name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Da hatte ich mir vorgestellt, dass es eine Möglichkeit gibt, die untere Funktion auf Charts zu erweitern.