ankonien
Goto Top

Outlook Anhang speichern und löschen

Liebe Community,

nutze Windows 10 und Office 2016. Folgendes Problem: Ich möchte in Outlook Anhänge auf der Festplatte speichern und in der Mail löschen. Um später noch zu erkennen, dass diese Mail einen Anhang hatte, soll in die Mail eine Notiz eingefügt werden "Entfernte Anhänge .....".
Verwende dazu das unten stehende Makro, was grundsätzlich auch funktioniert. Bin leider Makro-Laie und habe folgene Änderungswünsche:

- der Hinweistext "Entfernte Anhänge ..." erscheint am Ende der Mail, er soll aber am Beginn stehen
- der Text wird wie der Nachrichtentext formatiert - hätte ihn gern in kursiv und ein Schriftgrad kleiner
- das Makro speichert und entfernt alle Anhänge, also auch eingebettete Bilder, wie Logos etc. - wie kann man programmieren, dass nur "echte" Anhänge bearbeitet werden?

Schon mal vielen Dank für Eure Unterstützung!

Hier das Makro:

Sub AnhangSpeichern()
 
    'Declaration  
    Dim myItems, myItem, myAttachments, myAttachment As Object
    Dim myOrt As String
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
     
    'Ask for destination folder  
    myOrt = InputBox("Destination", "Save Attachments", "D:\")  
 
    On Error Resume Next
     
    'work on selected items  
    Set myOlExp = myOlApp.ActiveExplorer
    Set myOlSel = myOlExp.Selection
     
    'for all items do...  
    For Each myItem In myOlSel
     
        'point on attachments  
        Set myAttachments = myItem.Attachments
         
        'if there are some...  
        If myAttachments.Count > 0 Then
         
            'add remark to message text  
            myItem.Body = myItem.Body & vbCrLf & _
                "Entfernte Anhänge:" & vbCrLf  
                 
            'for all attachments do...  
            For i = 1 To myAttachments.Count
             
                'save them to destination  
                myAttachments(i).SaveAsFile myOrt & _
                    myAttachments(i).DisplayName
 
                'add name and destination to message text  
                myItem.Body = myItem.Body & _
                    "Datei: " & myOrt & _  
                    myAttachments(i).DisplayName & vbCrLf
                     
            Next i
             
            'for all attachments do...  
            While myAttachments.Count > 0
                              
                'remove it (use this method in Outlook 2000)  
                myAttachments(1).Delete
                 
            Wend
             
            'save item without attachments  
            myItem.Save
        End If
         
    Next
     
    'free variables  
    Set myItems = Nothing
    Set myItem = Nothing
    Set myAttachments = Nothing
    Set myAttachment = Nothing
    Set myOlApp = Nothing
    Set myOlExp = Nothing
    Set myOlSel = Nothing
   
End Sub

Content-Key: 464342

Url: https://administrator.de/contentid/464342

Printed on: April 18, 2024 at 17:04 o'clock

Member: Pjordorf
Pjordorf Jun 20, 2019 at 13:28:22 (UTC)
Goto Top
Hallo,

Zitat von @ankonien:
Verwende dazu das unten stehende Makro, was grundsätzlich auch funktioniert. Bin leider Makro-Laie und habe folgene Änderungswünsche:

Wieder ein Copy und Pastler ohne Verständniss...

- der Hinweistext "Entfernte Anhänge ..." erscheint am Ende der Mail, er soll aber am Beginn stehen
Schreibe es doch um
            'add remark to message text  
            myItem.Body = vbCrLf & _
                "Entfernte Anhänge:" & vbCrLf & myItem.Body   

Gruß,
Peter
Member: ankonien
ankonien Jun 20, 2019 at 16:12:19 (UTC)
Goto Top
Hallo Peter,
bin beeindruckt von Deiner umfassenden Problemanalyse und Deiner tiefgreifenden Lösungskompetenz. Hätte nicht erwartet, dass sich ein offensichtlicher IT-Halbgott wie Du sich herabläßt und mich eines Kommentars würdigst. Du zielst natürlich auf eine wichtige Fragestellung: Sollte jeder Sterbliche, der gerade mal einen Computer einschalten kann, hier eine Frage stellen dürfen.

Will Deine Kreise nicht weiter stören und verzichte daher zukünftig gerne auf derartige Mitleidsbekundungen.

Grüße in den IT-Olymp
ankonien
Member: Pjordorf
Pjordorf Jun 20, 2019 at 16:43:06 (UTC)
Goto Top
Hallo,

Zitat von @ankonien:
bin beeindruckt von Deiner umfassenden Problemanalyse
Fehleranalyse? Wo? Warum? Du hast nichts von Fehlern erzählt.

und Deiner tiefgreifenden Lösungskompetenz.
Hab ich nicht betrieben und auch nichts dazu gesagt, ausser das hier mal wieder ein Copy und Pastler rum macht der selbst sagt das er keine Ahnung hat face-smile

Hätte nicht erwartet, dass sich ein offensichtlicher IT-Halbgott wie Du sich herabläßt und mich eines Kommentars würdigst.
Dann hast du falsch gedacht

Du zielst natürlich auf eine wichtige Fragestellung
Nö.

Sollte jeder Sterbliche, der gerade mal einen Computer einschalten kann, hier eine Frage stellen dürfen.
Auch einer der einen Computer *'*nicht** einschalten kann, oder keinen Computer hat, darf hier fragen stellen. Die Antworten werden zeigen wie es weitergeht /weitergehen kann / weitergehen wird

Will Deine Kreise nicht weiter stören und verzichte daher zukünftig gerne auf derartige Mitleidsbekundungen.
Eigentlich bin ich nur neugierig ob die Stelle in dein Skript geändert wurde und o es dann passt (Den Text am Anfang und nicht am ende) wie ich an derm Schnipsel dir zeigte.

Grüße in den IT-Olymp
Ich wohne dort nicht - du?

Gruß,
Peter
Member: colinardo
Solution colinardo Jun 20, 2019, updated at Jun 21, 2019 at 11:45:15 (UTC)
Goto Top
Servus balkon... ähh @ankonien face-wink ,
hier was zum Spielen ... ich hätte zwar die Attachments nicht einfach aus der Nachricht gelöscht sondern gelöscht und stattdessen durch Link-Attachments (*.lnk) ersetzt (hätte Größe reduziert aber die Attachments weiterhin klickbar gelassen) aber vermutlich kennst du die Möglichkeit noch nicht, aber egal mach mal selbst weiter, sollst ja auch noch was aus der Sache lernen.

Sub AnhangSpeichern()
 
    'Declaration  
    Dim myItems, myItem As MailItem, myAttachments, att As Outlook.Attachment
    Dim myOrt As String, strBody As String, strPath As String
    Dim myOlApp As New Outlook.Application
    Dim myOlExp As Outlook.Explorer
    Dim myOlSel As Outlook.Selection
     
    'Ask for destination folder  
    myOrt = InputBox("Destination", "Save Attachments", "D:\")  
    
    ' FSO Object  
    Set fso = CreateObject("Scripting.FileSystemObject")  
 
    'On Error Resume Next  
     
    'work on selected items  
    Set myOlExp = myOlApp.ActiveExplorer
    Set myOlSel = myOlExp.Selection
     
    'for all items do...  
    For Each myItem In myOlSel
        Dim colRemove As New Collection
        With myItem
            'if there are some...  
            If .Attachments.Count > 0 Then
                
                strBody = IIf(.BodyFormat = olFormatHTML, .HTMLBody, .Body)
                
                'add remark to message text  
                strText = "<p style=""font-family:sans-serif;font-size:0.7em;font-style:italic"">Entfernte Anhänge:<br/>"  
                
                For Each att In .Attachments
                    ' If attachment is not inline  
                    If IsRealAttachment(att) Then
                        ' build save path  
                        strPath = fso.BuildPath(myOrt, att.FileName)
                        ' if file already exists build new name with unique id  
                        While fso.FileExists(strPath)
                            strPath = fso.BuildPath(myOrt, fso.GetBasename(att.FileName) & "_" & createUniqueID() & "." & fso.GetExtensionName(att.FileName))  
                        Wend
                        'save them to destination  
                        att.SaveAsFile strPath
                        ' add attachment to collection  
                        colRemove.Add att
                        'add name and destination to message text  
                        strText = strText & "Datei: <a href=""file:///" & strPath & """>" & strPath & "</a><br/>"  
                    End If
                Next
                ' if there are attachments to remove  
                If colRemove.Count > 0 Then
                    ' remove attachments  
                    For i = 1 To colRemove.Count
                        colRemove.Item(i).Delete
                    Next
                    ' set new mail body  
                    .HTMLBody = strText & "</p><hr/>" & strBody  
                    'save item  attachments  
                    .Save
                End If
            End If
        End With
    Next
     
    'free variables  
    Set myItems = Nothing
    Set myItem = Nothing
    Set myOlApp = Nothing
    Set myOlExp = Nothing
    Set myOlSel = Nothing
    Set fso = Nothing
End Sub

Function IsRealAttachment(ByVal att As Attachment) As Boolean
    IsRealAttachment = False
    If att.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E") = "" And att.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003") <> 4 Then  
        IsRealAttachment = True
    End If
End Function

Function createUniqueID()
    number = CLng((Now - #1/1/1970#) * 86400)
    Dim cList
    cList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"  
    Dim finalString
    While Not number = 0
        If CInt(number Mod 36) = 0 Then
            finalString = finalString & Mid(cList, 1, 1)
        Else
            finalString = finalString & Mid(cList, (number Mod 36), 1)
        End If
        number = Round(number / 36)
    Wend
    createUniqueID = finalString
End Function
Grüße Uwe
Member: ankonien
ankonien Jun 21, 2019 at 19:28:05 (UTC)
Goto Top
Hallo Uwe,

super - vielen Dank! Das Makro macht jetzt genau was es soll und wonach ich schon ziemlich lange gesucht habe (bevor ich überhaupt auf die Idee mit Makros kam).
Obwohl das Makro funktioniert, erhalte ich eine Fehlermeldung (siehe Anhänge). Das ".Save" in Zeile 60 ist wohl dafür verantwortlich. Ob ich Dich da nochmal bitten dürfte ....?!

Schon mal herzlichen Dank dafür und viele Grüße
ankonien
debugger
fehler makro
Member: colinardo
Solution colinardo Jun 21, 2019 updated at 20:00:45 (UTC)
Goto Top
Typisch IMAP Konto
https://support.microsoft.com/en-us/help/3064609/0x80040109-error-when-o ...
Outlook ist da bei IMAP zu träge, wenn es speichert löscht es die Mail und erstellt gleichzeitig eine neue.
Fix:
Collection erstellen, geänderte Mails hinzufügen, Save entfernen und am Ende über die Collection itterieren und gesammelt die Speichervorgänge ausführen.
Wie das geht kannst du oben im Code zu 99% abschauen.
Homework especially for you 😁
Member: ankonien
ankonien Jun 21, 2019 at 21:11:32 (UTC)
Goto Top
Lieber Uwe,
viiiiielen herzlichen Dank - echt mega, welche Arbeit Du Dir gemacht hast!!!

Weiß das sehr zu schätzen und werd mich da mal weiter reinfuchsen.

Beste Grüße und alles Gute
ankonien