Live-Forum - Die aktuellen Beiträge
Datum
Titel
16.10.2025 17:40:39
16.10.2025 17:25:38
Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender

Forumthread: Formular per E-Mail versenden VBA (OS&Win)

Formular per E-Mail versenden VBA (OS&Win)
01.12.2016 15:47:46
Jamie
Liebe Community
ich komme gleich mit einem zweiten Thema.
Da ihr beim ersten Thema mir bereits super helfen konntet.
Ich benötige den Code, damit es mit MAC OS und Windows funktioniert.
Es soll das Tabellenblatt (blattgeschützt) per E-Mail im Anhang an einen Empfänger senden.
Hierbei erhalte ich bei "Set outObj = CreateObject ("Outlook.Application") ein Problem.
Liegt es am Mac OS? Wie kann ich den Code so darstellen das es für beide Versionen greift?
Freue mich auf Euer Feedback und bin sehr gespannt auf die Lösung.
Euer Jamie
Sub einzelnes_Blatt_senden()
ActiveSheet.Unprotect Password:="password"
Dim strBlatt As String
Dim strDatei As String
Dim strPfad As String
Dim outObj As Object
Dim Mail As Object
Dim strBodyText As String
Set outObj = CreateObject("Outlook.Application")
Set Mail = outObj.CreateItem(0)
strPfad = "C:\Temp"
strBlatt = ActiveSheet.Name
Sheets(strBlatt).Copy
ActiveWorkbook.SaveAs strPfad & "\" & ActiveSheet.Name
strDatei = ActiveWorkbook.FullName
With Mail
.To = "spoc@empf.ch"
'.CC = ""
.Subject = "Artikelstammdaten" 'Betreff
.BodyFormat = 2 '2 = HTML, 1 = Text
.Attachments.Add strDatei
.Body = strBodyText
End With
'** Erzeugte Datei schließen
Workbooks(Dir(strDatei)).Close
'** Erzeugte Datei wieder löschen
Kill (strDatei)
'** E-Mail anzeigen
Mail.Display
ActiveSheet.Protect Password:="password"
End Sub

Anzeige

6
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Dafür gibt es schon...
02.12.2016 10:37:05
Jamie
Hallo Case
vielen Dank, super Seite! Habe zwar bei Dr. Google nachgefragt, aber kam nicht auf eine solche Seite.
Ich habe jetzt mal folgende zwei Beispiele getestet und bei beiden bekomme ich eine Fehler-Meldung gemäss (Sub oder Function sind nicht definiert)
Ich habe den Code im Worksheet abgelegt sowie auch ein Modul erstellt, bei beidem bekomme ich es nicht hin.
Mach ich einen Denkfehler?
Sub Mail_workbook_Excel2011_1()
ActiveSheet.Unprotect Password:="password"
'For Excel 2011 for the Mac and Outlook 2011
'Note: The workbook must be Saved ones
Dim wb As Workbook
If Val(Application.Outlook) 


Sub Mail_Workbook_Excel2011_2()
'For Excel 2011 for the Mac and Outlook 2011
Dim wb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
If Val(Application.Version) 

Anzeige
Dast steht doch...
02.12.2016 10:57:46
Case
Hallo, :-)
... alles klar im Text drin: ;-)
Note: To run the mail examples you must copy the macro of your choice and also two functions named MailFromMacWithOutlook and KillFileOnMac that you find on the bottom of this page. If you want to test more then one macro you only have to copy the two functions one time.
Servus
Case

Anzeige
AW: Dast steht doch...
02.12.2016 12:01:12
Jamie
Meinst du das?
Application.Version
hier habe ich mit .Version und Outlook versucht.
ging beides nicht.
oder stehe ich auf dem Schlauch?
Nein - Du musst die...
02.12.2016 13:02:12
Case
Hallo, :-)
... beiden Funktionen - "MailFromMacWithOutlook" und "KillFileOnMac" noch in das Modul kopieren! ;-)
Servus
Case

Anzeige
AW: Nein - Du musst die...
02.12.2016 15:28:17
Jamie
*klatschinsGesicht*
habe den Fehler gefunden, ich habe Mac Excel 2016 nicht 2011...
Hierbei musste ich diese Anleitung fahren:
http://www.rondebruin.nl/mac/macmail/macoutlook2016.htm
Jetzt hat mich aber das Ganze auf eine super Idee gebracht.
Wie kann ich bestimmte Zellen Inhalt vom Excel im Bodytext (E-Mail) Wiederspiegeln?
Mein jetziger Code ist:
Sub Mail_WorkSheet_or_Worksheets_in_Mac_Excel_2016_with_Outlook3()
'Only working in Excel 2016 for the Mac with Outlook 2016 as mail program
'Do not forget to add the 3 functions into your own workbook
'More Mail codes : http://www.rondebruin.nl/mac/ _
mail.htm
Dim Sourcewb As Workbook, DestWB  As Workbook, sh As Worksheet
Dim strbody As String, TempFileName As String
'Check if the AppleScriptTaskExcelScriptFile is in the correct location
If CheckAppleScriptTaskExcelScriptFile(ScriptFileName:="RDBMacOutlook.scpt") = False Then
MsgBox "Sorry the RDBMacOutlook.scpt file is not in the correct location, " & _
"Visit http://www.rondebruin. _
nl/mac/macmail/macoutlook2016.htm
for more information."
Exit Sub
End If
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Set reference to the source workbook
Set Sourcewb = ActiveWorkbook
'Create the body text in the strbody string
'The first and last line are used to set the font and font size
strbody = ""
strbody = strbody & "Hi there" & "
" & "
" & _ "This is line 1" & "
" & _ "This is line 2" & "
" & _ "This is line 3" & "
" & _ "This is line 4" strbody = strbody & "
" 'Copy the ActiveSheet to a new workbook, you can also use : Sourcewb.Sheets("MySheetName"). _ Copy 'Or for more sheets : Sourcewb.Sheets(Array("Sheet1", "Sheet3")).Copy ActiveSheet.Copy Set DestWB = ActiveWorkbook 'Delete the button on the one sheet workbook On Error Resume Next DestWB.Sheets(1).DrawingObjects.Visible = True DestWB.Sheets(1).DrawingObjects.Delete On Error GoTo 0 'Change all cells in the worksheets to values if you want 'I use .Value = .Value because PasteSpecial with CutCopyMode = False not work on a Mac 'For Each sh In DestWB.Worksheets 'With sh.UsedRange '.Value = .Value 'End With 'Next sh 'Enter the name of the file we just create TempFileName = "Part of " & Sourcewb.Name & " " _ & Format(Now, "dd-mmm-yy h-mm-ss") 'Call the MailWithMacOutlook2016 function to save the new file and create the mail 'When you use more mail addresses separate them with a , 'Look in Outlook>Preferences for the type and name of the account you want to use 'If accounttype is empty it will use the default mail account, accounttype can be pop or _ imap 'Note : It will use the signature of the account that you choose MailWithMacOutlook2016WorkSheet _ subject:="This is a test macro", _ mailbody:=strbody, _ toaddress:="ron@debruin.nl", _ ccaddress:="", _ bccaddress:="", _ displaymail:=True, _ accounttype:="", _ accountname:="", _ attachment:=TempFileName, _ FileFormat:=Sourcewb.FileFormat With Application .ScreenUpdating = True .EnableEvents = True End With End Sub

Anzeige
;

Forumthreads zu verwandten Themen

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Infobox / Tutorial

Excel-Tabellenblatt per E-Mail versenden mit VBA


Schritt-für-Schritt-Anleitung

Um ein Excel-Tabellenblatt per E-Mail zu versenden, benötigst Du den folgenden VBA-Code. Dieser Code funktioniert sowohl auf Windows als auch auf MacOS, wobei Du sicherstellen musst, dass Du die entsprechenden Anpassungen für die Outlook-Anwendung vornimmst.

  1. Öffne Excel und drücke ALT + F11, um den VBA-Editor zu öffnen.
  2. Füge ein neues Modul hinzu: Rechtsklick auf "VBAProject (DeineDatei.xlsx)" > Einfügen > Modul.
  3. Kopiere den folgenden Code in das Modul:
Sub einzelnes_Blatt_senden()
    ActiveSheet.Unprotect Password:="password"
    Dim strBlatt As String
    Dim strDatei As String
    Dim strPfad As String
    Dim outObj As Object
    Dim Mail As Object
    Dim strBodyText As String
    Set outObj = CreateObject("Outlook.Application")
    Set Mail = outObj.CreateItem(0)
    strPfad = "C:\Temp"
    strBlatt = ActiveSheet.Name
    Sheets(strBlatt).Copy
    ActiveWorkbook.SaveAs strPfad & "\" & ActiveSheet.Name
    strDatei = ActiveWorkbook.FullName
    With Mail
        .To = "spoc@empf.ch"
        '.CC = ""
        .Subject = "Artikelstammdaten"
        .BodyFormat = 2 '2 = HTML, 1 = Text
        .Attachments.Add strDatei
        .Body = strBodyText
    End With
    Workbooks(Dir(strDatei)).Close
    Kill (strDatei)
    Mail.Display
    ActiveSheet.Protect Password:="password"
End Sub
  1. Passe den Speicherpfad und die E-Mail-Adresse an Deine Bedürfnisse an.
  2. Schließe den VBA-Editor und führe das Makro aus.

Häufige Fehler und Lösungen

  • Fehler: "Set outObj = CreateObject("Outlook.Application")"

    • Dieser Fehler tritt möglicherweise auf, wenn Outlook nicht auf deinem Mac installiert ist. Stelle sicher, dass Du die richtige Version von Outlook verwendest.
  • Fehler: "Sub oder Function sind nicht definiert"

    • Überprüfe, ob Du den gesamten notwendigen Code kopiert hast, einschließlich aller erforderlichen Funktionen. Achte darauf, dass die Funktionen MailFromMacWithOutlook und KillFileOnMac im Modul vorhanden sind.

Alternative Methoden

Für Nutzer von Google Sheets gibt es die Möglichkeit, ein Makro zu erstellen, um eine E-Mail zu versenden. Hierbei kannst Du den google sheets macro button verwenden, um den Prozess zu automatisieren. Das HTML-Formular kann ebenfalls verwendet werden, um Daten per E-Mail zu senden.


Praktische Beispiele

Ein Beispiel für das Versenden des Inhalts bestimmter Zellen im Bodytext der E-Mail:

strBodyText = "Die Werte sind: " & Range("A1").Value & " und " & Range("B1").Value

Füge diesen Code in den With Mail Block ein, um die Werte von Zellen A1 und B1 in den E-Mail-Text einzufügen.


Tipps für Profis

  • Verwende vba mail versenden, um Massen-E-Mails zu erstellen, indem Du eine Schleife über eine Liste von E-Mail-Adressen implementierst.
  • Achte darauf, dass Du beim Arbeiten mit mac vba excel die spezifischen Funktionen für den Mac verwendest, um Komplikationen zu vermeiden.

FAQ: Häufige Fragen

1. Kann ich den Code in Excel für den Mac verwenden? Ja, der Code ist so strukturiert, dass er sowohl in Excel für Windows als auch für Mac funktioniert, solange Du die entsprechenden Anpassungen vornimmst.

2. Wie kann ich den E-Mail-Body formatieren? Du kannst HTML verwenden, um den E-Mail-Body zu formatieren, indem Du BodyFormat = 2 setzt und HTML-Tags in den strBodyText einfügst.

3. Was ist der Unterschied zwischen dem Versenden einer E-Mail auf Windows und Mac? Die Hauptunterschiede liegen in der verwendeten Outlook-Anwendung und der Art und Weise, wie bestimmte VBA-Objekte auf den jeweiligen Plattformen erstellt werden. Achte darauf, die korrekten Objekte für Deine Plattform zu verwenden.

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Entdecke mehr
Finde genau, was du suchst

Die erweiterte Suchfunktion hilft dir, gezielt die besten Antworten zu finden

Suche nach den besten Antworten
Unsere beliebtesten Threads

Entdecke unsere meistgeklickten Beiträge in der Google Suche

Top 100 Threads jetzt ansehen
Anzeige