Range per Mail versenden - Hyperlinks beibehalten

  • Excel

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von volti.

    Range per Mail versenden - Hyperlinks beibehalten

    Hallo,

    ich hatte vor mit einem Button und einem hinterlegten VBA Code einen ausgewählten Bereich ins Outlook zu kopieren.
    Mailadressen sind je nach Anwendungsfall spezifisch.
    Die Formatierung bleibt mir mit dieser Variante von Ron de Bruin nun endlich erhalten, jedoch verlieren sich dabei die noch Hyperlinks (welche spezifisch und niemals dieselben sind), die ich aber in jedem Fall benötige.
    Weiß jemand eine bessere Lösung, wo eine Änderung nötig ist bzw. wie das gelöst werden könnte?
    Die aktuelle Umsetzung war ein einfaches Strg+C im Excel und Strg+V in der neuen E-Mail, da möchte ich weg kommen...

    Quellcode

    1. Sub Mail_Selection_Range_Outlook_Body()
    2. Dim teilenummer As String
    3. teilenummer = ActiveSheet.Cells(1, 1).Value
    4. Dim rng As Range
    5. Dim OutApp As Object
    6. Dim OutMail As Object
    7. Set rng = Nothing
    8. On Error Resume Next
    9. Set rng = Selection.SpecialCells(xlCellTypeVisible)
    10. On Error GoTo 0
    11. If rng Is Nothing Then
    12. MsgBox "The selection is not a range or the sheet is protected" & _
    13. vbNewLine & "please correct and try again.", vbOKOnly
    14. Exit Sub
    15. End If
    16. With Application
    17. .EnableEvents = False
    18. .ScreenUpdating = False
    19. End With
    20. Set OutApp = CreateObject("Outlook.Application")
    21. Set OutMail = OutApp.CreateItem(0)
    22. On Error Resume Next
    23. With OutMail
    24. .To = ""
    25. .CC = ""
    26. .BCC = ""
    27. .Subject = "Q-Status für " & teilenummer
    28. .HTMLBody = RangetoHTML(rng)
    29. .Display
    30. End With
    31. On Error GoTo 0
    32. With Application
    33. .EnableEvents = True
    34. .ScreenUpdating = True
    35. End With
    36. Set OutMail = Nothing
    37. Set OutApp = Nothing
    38. End Sub
    39. Function RangetoHTML(rng As Range)
    40. Dim fso As Object
    41. Dim ts As Object
    42. Dim TempFile As String
    43. Dim TempWB As Workbook
    44. TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    45. rng.Copy
    46. Set TempWB = Workbooks.Add(1)
    47. With TempWB.Sheets(1)
    48. .Cells(1).PasteSpecial Paste:=8
    49. .Cells(1).PasteSpecial xlPasteValues, , False, False
    50. .Cells(1).PasteSpecial xlPasteFormats, , False, False
    51. .Cells(1).Select
    52. Application.CutCopyMode = False
    53. On Error Resume Next
    54. .DrawingObjects.Visible = True
    55. .DrawingObjects.Delete
    56. On Error GoTo 0
    57. End With
    58. With TempWB.PublishObjects.Add( _
    59. SourceType:=xlSourceRange, _
    60. Filename:=TempFile, _
    61. Sheet:=TempWB.Sheets(1).Name, _
    62. Source:=TempWB.Sheets(1).UsedRange.Address, _
    63. HtmlType:=xlHtmlStatic)
    64. .Publish (True)
    65. End With
    66. Set fso = CreateObject("Scripting.FileSystemObject")
    67. Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    68. RangetoHTML = ts.readall
    69. ts.Close
    70. RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
    71. "align=left x:publishsource=")
    72. TempWB.Close savechanges:=False
    73. Kill TempFile
    74. Set ts = Nothing
    75. Set fso = Nothing
    76. Set TempWB = Nothing
    77. End Function
    Hallo,

    lass das RangeToHTML-Geraffel weg und kopiere den Bereich über den Word-Editor.

    Hier Dein angepasster Code und ein weiteres Beispiel mit zusätzlichem Text und Signatur....

    VB.NET-Quellcode

    1. Sub Mail_Selection_Range_Outlook_Body()
    2. Dim teilenummer As String
    3. teilenummer = ActiveSheet.Cells(1, 1).Value
    4. Dim rng As Range
    5. Dim OutApp As Object
    6. Dim OutMail As Object
    7. Set rng = Nothing
    8. On Error Resume Next
    9. Set rng = Selection.SpecialCells(xlCellTypeVisible)
    10. On Error GoTo 0
    11. If rng Is Nothing Then
    12. MsgBox "The selection is not a range or the sheet is protected" & _
    13. vbNewLine & "please correct and try again.", vbOKOnly
    14. Exit Sub
    15. End If
    16. With Application
    17. .EnableEvents = False
    18. .ScreenUpdating = False
    19. End With
    20. Set OutApp = CreateObject("Outlook.Application")
    21. Set OutMail = OutApp.CreateItem(0)
    22. On Error Resume Next
    23. With OutMail
    24. .To = ""
    25. .CC = ""
    26. .BCC = ""
    27. .Subject = "Q-Status für " & teilenummer
    28. .Display
    29. rng.Copy
    30. .GetInspector.WordEditor.Range.Paste ' Bereich in Mail einfügen
    31. End With
    32. On Error GoTo 0
    33. With Application
    34. .EnableEvents = True
    35. .ScreenUpdating = True
    36. End With
    37. Set OutMail = Nothing
    38. Set OutApp = Nothing
    39. End Sub
    40. Private Sub Mail_BereichalsBereich_Word1()
    41. ' Sendet Mail mit integriertem Bereich als Bereich mit Signatur
    42. Dim WSh1 As Worksheet, WSh2 As Worksheet
    43. Dim sMailtext As String, sBer As String
    44. sBer = "AS1:AV12" ' Kopierbereich
    45. Set WSh1 = ThisWorkbook.Sheets("Tabelle1") ' Blatt mit Maildaten
    46. Set WSh2 = ThisWorkbook.Sheets("Tabelle2") ' Datenblatt
    47. WSh2.Range(sBer).Copy ' Bereich kopieren
    48. With CreateObject("Outlook.Application").CreateItem(0)
    49. .BodyFormat = 2 ' 2=HTML-Format, 3=Richtext
    50. .Subject = WSh1.Range("A2").Value ' Betreff
    51. .To = WSh1.Range("A3").Value ' Empfänger
    52. .CC = WSh1.Range("A4").Value ' Kopie
    53. sMailtext = WSh1.Range("A5").Value & vbLf
    54. .GetInspector ' Signatur holen
    55. .htmlbody = Replace(sMailtext, vbLf, "<br>") & .htmlbody
    56. .Display
    57. With .GetInspector.WordEditor.Application.Selection
    58. .start = Len(sMailtext) + 1
    59. .Paste ' Bereich in Mail einfügen
    60. End With
    61. End With
    62. End Sub


    Gruß
    Karl-Heinz