August 11, 2020, 14:03:40

Neuigkeiten:

Wenn ihr euch für eine gute Antwort bedanken möchtet, im entsprechenden Posting einfach den Knopf "sag Danke" drücken!


Variabler Bereich einer Excel-Tabelle 1:1 in neue Outlook-Email im Textfeld

Begonnen von Pergus, Juni 13, 2018, 11:14:00

⏪ vorheriges - nächstes ⏩

Pergus

Hallo Excel-VBA'ler,

wie kann man einen variablen Bereich einer Excel-Tabelle 1:1 (also samt Formatierungen)
in eine neue Outlook-Email als Text (im "Body" (nach Nomenklatur), nicht in der Anlage) kopieren?
Optimal wäre, wenn die neue Email bereits mit Signatur (gespeichert als "Signatur-personalisiert") geöffnet werden könnte.

Manuell habe ich es bisher so gemacht:
- den Bereich in Excel kopieren
- in Outlook auf "Neue Email-Nachricht" klicken (wird bereits mit Signatur geöffnet) und
- im "Body" (nach Nomenklatur) der Email "Einfügen".

Der Makro-Recorder zeichnet leider nichts auf  :(
Da diese Routine täglich etliche Male zu erledigen ist, würde ich dies gerne automatisieren.

Hat wer eine Idee?

Danke schon mal im Voraus!

Lg aus Südtirol
Gustav
Win7, Office Professional 2010

Pergus

Hallo Allerseits!

Hat niemand einen Tipp für mich?  :'(

Wie bekomme ich per VBA einen variablen Bereich in Excel (z.B.: Worksheets("Test").Range("A1:C10")),
der bereits als kopiert bereit steht (.copy), in den Textfeld (Body) einer neuen Email in Outlook samt Formatierung [Strg V]?

Also:

Excel -> Tabelle ("Test") -> Bereich ("A1:C10") -> .copy => bis hierher funktionierts schon!
Outlook -> neue Email öffnen -> [Strg V] im Textfeld der Email (Body) => wie geht das?

Wäre wirklich dankbar für jede Hilfe!  :D

Lg aus Südtirol
Gustav
Win7, Office Professional 2010


Pergus

Ich hab's  ;D

auf folgender Web-Seite wurde ich fündig: http://www.rondebruin.nl/win/s1/outlook/bmail1.htm

Hier die Code dazu, genau wie ich's haben wollte:

Sub Mail_Sheet_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set rng = Nothing
    Set rng = ActiveSheet.UsedRange
    'You can also use a sheet name
    'Set rng = Sheets("YourSheet").UsedRange

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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


Das einzige was noch fehlt ist die Signatur... werde dazu ein neues Thema eröffnen!

Lg aus Südtirol
Gustav
Win7, Office Professional 2010

Pergus

Jetzt funktioniert alles inklusive Signatur!

Geändert habe ich lediglich die Zeilen, die ich mit '### markiert habe, siehe hier:

Option Explicit

Sub Mail_Sheet_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Stop
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim olOldBody As String '###
   
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set rng = Nothing
    Set rng = ActiveSheet.UsedRange
    'You can also use a sheet name
    'Set rng = Sheets("YourSheet").UsedRange

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Projekt: "
        .GetInspector.Display
        olOldBody = .HTMLBody '###
        .HTMLBody = RangetoHTML(rng) & "<br><br>" & olOldBody
'        .HTMLBody = .HTMLBody & .HTMLBody
        Stop
'        .Display   'or use .Send

    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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


(Quelle: http://www.rondebruin.nl/win/s1/outlook/bmail1.htm)

Für alle die das ebenfalls brauchen können!  ;)

Lg aus Südtirol
Gustav
Win7, Office Professional 2010