Hallo zusammen,
Ich habe einen Code zum automatischen speichern von Mails auf Knopfdruck. Jedoch speichert er gleichnamige Mails nur 1 mal, und gibt dann die Fehlermeldung „bereits vorhanden“. Wie kann ich den Code umschreiben, um beim speichern immer „plus 1“ zu zählen. (zb: auto wird zu auto1 usw)
Ich habe keine Ahnung. Vielleicht nimmt sich jemand die Zeit mir zu helfen… wäre echt toll! Ganz herzlichen Dank schon jetzt!!
Liebe Grüsse
bo
VBA Code:
Attribute VB\_Name = "ExportEmail"
'==========================================================================
'Export Outlook e-mail to drive
'--------------------------------------------------------------------------
'Author: Michael Wöhrer
'Version: 0.2, 2009-01-20
'==========================================================================
'Terms and conditions
' You can use, redistribute and/or modify this code under the terms of
' the SOFTWARE GUIDE LICENSE. This code is distributed in the hope that it
' will be useful, but WITHOUT ANY WARRANTY; without even the implied
' warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the SOFTWARE GUIDE LICENSE for more details.
'==========================================================================
Option Explicit
'-------------------------------------------------------------
' OPTIONS
'-------------------------------------------------------------
'Email format:
' MSG = Outlook msg format (incl. attachments, embedded objects etc.)., TXT = plain text
Private Const EXM\_OPT\_MAILFORMAT As String = "MSG"
'Date format of filename
Private Const EXM\_OPT\_FILENAME\_DATEFORMAT As String = "yyyy-mm-dd\_hh-nn-ss"
'Build filename; placeholders: for date, for sender's name, for receiver, for subject
Private Const EXM\_OPT\_FILENAME\_BUILD As String = "\_"
'Use browse folder? Set to FALSE if you don't want to use browser for selecting target folder
Private Const EXM\_OPT\_USEBROWSER As Boolean = True
'Target folder (used if EXM\_OPT\_USEBROWSER is set to FALSE)
Private Const EXM\_OPT\_TARGETFOLDER As String = "D:\"
'Maximum number of emails to be selected & exported. Please don't use a huge number as this will cause
'performance and maybe other issues. Recommended is a value between 5 and 20.
Private Const EXM\_OPT\_MAX\_NO As Integer = 10
'Email subject prefixes (such us "RE:", "FW:" etc.) to be removed. Please note that this is a
'RegEx expression, google for "regex" for further information. For instance "\s" means blank " ".
Private Const EXM\_OPT\_CLEANSUBJECT\_REGEX As String = "RE:\s|Re:\s|AW:\s|FW:\s|WG:\s|SV:\s|Antwort:\s"
'-------------------------------------------------------------
'-------------------------------------------------------------
' TRANSLATIONS
'-------------------------------------------------------------
'-- English
'Const EXM\_007 = "Script terminated"
'Const EXM\_013 = "Selected Outlook item is not an e-mail"
'Const EXM\_014 = "File already exists"
'-- German
Private Const EXM\_001 As String = "Die E-Mail wurde erfolgreich abgelegt."
Private Const EXM\_002 As String = "Die E-Mail konnte nicht abgelegt werden, Grund:"
Private Const EXM\_003 As String = "Ausgewählter Pfad:"
Private Const EXM\_004 As String = "E-Mail(s) ausgewählt und erfolgreich abgelegt."
Private Const EXM\_005 As String = ""
Private Const EXM\_006 As String = ""
Private Const EXM\_007 As String = "Script abgebrochen"
Private Const EXM\_008 As String = "Fehler aufgetreten: Sie haben mehr als [LIMIT\_SELECTED\_ITEMS] E-Mails ausgewählt. Die Aktion wurde beendet."
Private Const EXM\_009 As String = "Es wurde keine E-Mail ausgewählt."
Private Const EXM\_010 As String = "Es ist ein Fehler aufgetreten: es war keine Email im Fokus, so dass die Ablage nicht erfolgen konnte."
Private Const EXM\_011 As String = "Es ist ein Fehler aufgetreten:"
Private Const EXM\_012 As String = "Die Aktion wurde beendet."
Private Const EXM\_013 As String = "Ausgewähltes Outlook-Dokument ist keine E-Mail"
Private Const EXM\_014 As String = "Datei existiert bereits"
Private Const EXM\_015 As String = ""
Private Const EXM\_016 As String = "Bitte wählen Sie den Ordner zum Exportieren:"
Private Const EXM\_017 As String = "Fehler beim Exportieren aufgetreten"
Private Const EXM\_018 As String = "Export erfolgreich"
Private Const EXM\_019 As String = "Bei [NO\_OF\_FAILURES] E-Mail(s) ist ein Fehler aufgetreten:"
Private Const EXM\_020 As String = "[NO\_OF\_SELECTED\_ITEMS] E-Mail(s) wurden ausgewählt und [NO\_OF\_SUCCESS\_ITEMS] E-Mail(s) erfolgreich abgelegt."
'-------------------------------------------------------------
'-------------------------------------
'For browse folder
'-------------------------------------
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Const BIF\_RETURNONLYFSDIRS = 1
Private Const MAX\_PATH = 260
Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Public Sub ExportEmailToDrive()
Const PROCNAME As String = "ExportEmailToDrive"
On Error GoTo ErrorHandler
Dim myExplorer As Outlook.Explorer
Dim myfolder As Outlook.MAPIFolder
Dim myItem As Object
Dim olSelection As Selection
Dim strBackupPath As String
Dim intCountAll As Integer
Dim intCountFailures As Integer
Dim strStatusMsg As String
Dim vSuccess As Variant
Dim strTemp1 As String
Dim strTemp2 As String
Dim strErrorMsg As String
'-------------------------------------
'Get target drive
'-------------------------------------
If (EXM\_OPT\_USEBROWSER = True) Then
strBackupPath = GetFileDir
If Left(strBackupPath, 15) = "ERROR\_OCCURRED:" Then
strErrorMsg = Mid(strBackupPath, 16, 9999)
Error 5004
End If
Else
strBackupPath = EXM\_OPT\_TARGETFOLDER
End If
If strBackupPath = "" Then GoTo ExitScript
If (Not Right(strBackupPath, 1) = "\") Then strBackupPath = strBackupPath & "\"
'-------------------------------------
'Process according to what is in the focus: an opened e-mail or a folder with selected e-mails.
'Case 2 would also work for opened e-mail, however it does not always work (for instance if
' an e-mail is saved on the file system and being opened from there).
'-------------------------------------
Set myExplorer = Application.ActiveExplorer
Set myfolder = myExplorer.CurrentFolder
If myfolder Is Nothing Then Error 5001
If Not myfolder.DefaultItemType = olMailItem Then GoTo ExitScript
'Stop if more than x emails selected
If myExplorer.Selection.Count \> EXM\_OPT\_MAX\_NO Then Error 5002
'No email selected at all?
If myExplorer.Selection.Count = 0 Then Error 5003
Set olSelection = myExplorer.Selection
intCountAll = 0
intCountFailures = 0
For Each myItem In olSelection
intCountAll = intCountAll + 1
vSuccess = ProcessEmail(myItem, strBackupPath)
If (Not vSuccess = True) Then
Select Case intCountFailures
Case 0: strStatusMsg = vSuccess
Case 1: strStatusMsg = "1x " & strStatusMsg & Chr(10) & "1x " & vSuccess
Case Else: strStatusMsg = strStatusMsg & Chr(10) & "1x " & vSuccess
End Select
intCountFailures = intCountFailures + 1
End If
Next
If intCountFailures = 0 Then
strStatusMsg = intCountAll & " " & EXM\_004
End If
'Final Message
If (intCountFailures = 0) Then 'No failure occurred
MsgBox strStatusMsg & Chr(10) & Chr(10) & EXM\_003 & " " & strBackupPath, 64, EXM\_018
ElseIf (intCountAll = 1) Then 'Only one email was selected and a failure occurred
MsgBox EXM\_002 & Chr(10) & vSuccess & Chr(10) & Chr(10) & EXM\_003 & " " & strBackupPath, 48, EXM\_017
Else 'More than one email was selected and at least one failure occurred
strTemp1 = Replace(EXM\_020, "[NO\_OF\_SELECTED\_ITEMS]", intCountAll)
strTemp1 = Replace(strTemp1, "[NO\_OF\_SUCCESS\_ITEMS]", intCountAll - intCountFailures)
strTemp2 = Replace(EXM\_019, "[NO\_OF\_FAILURES]", intCountFailures)
MsgBox strTemp1 & Chr(10) & Chr(10) & strTemp2 & Chr(10) & Chr(10) & strStatusMsg \_
& Chr(10) & Chr(10) & EXM\_003 & " " & strBackupPath, 48, EXM\_017
End If
ExitScript:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 5001: 'Not an email
MsgBox EXM\_010, 64, EXM\_007
Case 5002:
MsgBox Replace(EXM\_008, "[LIMIT\_SELECTED\_ITEMS]", EXM\_OPT\_MAX\_NO), 64, EXM\_007
Case 5003:
MsgBox EXM\_009, 64, EXM\_007
Case 5004:
MsgBox EXM\_011 & Chr(10) & Chr(10) & strErrorMsg, 48, EXM\_007
Case Else:
MsgBox EXM\_011 & Chr(10) & Chr(10) \_
& Err & " - " & Error$ & Chr(10) & Chr(10) & EXM\_012, 48, EXM\_007
End Select
Resume ExitScript
End Sub
Private Function ProcessEmail(myItem As Object, strBackupPath As String) As Variant
'Saves the e-mail on the drive by using the provided path.
'Returns TRUE if successful, and FALSE otherwise.
Const PROCNAME As String = "ProcessEmail"
On Error GoTo ErrorHandler
Dim myMailItem As MailItem
Dim strDate As String
Dim strSender As String
Dim strReceiver As String
Dim strSubject As String
Dim strFinalFileName As String
Dim strFullPath As String
Dim vExtConst As Variant
Dim vTemp As String
Dim strErrorMsg As String
If TypeOf myItem Is MailItem Then
Set myMailItem = myItem
Else
Error 1001
End If
'Set filename
strDate = Format(myMailItem.ReceivedTime, EXM\_OPT\_FILENAME\_DATEFORMAT)
strSender = myMailItem.SenderName
strReceiver = myMailItem.To 'All receiver, semikolon separated string
If InStr(strReceiver, ";") \> 0 Then strReceiver = Left(strReceiver, InStr(strReceiver, ";") - 1)
strSubject = myMailItem.Subject
strFinalFileName = EXM\_OPT\_FILENAME\_BUILD
strFinalFileName = Replace(strFinalFileName, "", strDate)
strFinalFileName = Replace(strFinalFileName, "", strSender)
strFinalFileName = Replace(strFinalFileName, "", strReceiver)
strFinalFileName = Replace(strFinalFileName, "", strSubject)
strFinalFileName = CleanString(strFinalFileName)
If Left(strFinalFileName, 15) = "ERROR\_OCCURRED:" Then
strErrorMsg = Mid(strFinalFileName, 16, 9999)
Error 1003
End If
strFinalFileName = IIf(Len(strFinalFileName) \> 251, Left(strFinalFileName, 251), strFinalFileName)
strFullPath = strBackupPath & strFinalFileName
'Save as msg or txt?
Select Case UCase(EXM\_OPT\_MAILFORMAT)
Case "MSG":
strFullPath = strFullPath & ".msg"
vExtConst = olMSG
Case Else:
strFullPath = strFullPath & ".txt"
vExtConst = olTXT
End Select
'File already exists?
If CreateObject("Scripting.FileSystemObject").FileExists(strFullPath) = True Then
Error 1002
End If
'Save file
myMailItem.SaveAs strFullPath, vExtConst
'Return true as everything was successful
ProcessEmail = True
ExitScript:
Exit Function
ErrorHandler:
Select Case Err.Number
Case 1001: 'Not an email
ProcessEmail = EXM\_013
Case 1002:
ProcessEmail = EXM\_014
Case 1003:
ProcessEmail = strErrorMsg
Case Else:
ProcessEmail = "Error #" & Err & ": " & Error$ & " (Procedure: " & PROCNAME & ")"
End Select
Resume ExitScript
End Function
Private Function CleanString(strData As String) As String
Const PROCNAME As String = "CleanString"
On Error GoTo ErrorHandler
'Instantiate RegEx
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Global = True
'Cut out strings we don't like
objRegExp.Pattern = EXM\_OPT\_CLEANSUBJECT\_REGEX
strData = objRegExp.Replace(strData, "")
'Replace and cut out invalid strings.
strData = Replace(strData, Chr(9), "\_")
strData = Replace(strData, Chr(10), "\_")
strData = Replace(strData, Chr(13), "\_")
objRegExp.Pattern = "[/\\*]"
strData = objRegExp.Replace(strData, "-")
objRegExp.Pattern = "[""]"
strData = objRegExp.Replace(strData, "'")
objRegExp.Pattern = "[:?\|]"
strData = objRegExp.Replace(strData, "")
'Replace multiple chars by 1 char
objRegExp.Pattern = "\s+"
strData = objRegExp.Replace(strData, " ")
objRegExp.Pattern = "\_+"
strData = objRegExp.Replace(strData, "\_")
objRegExp.Pattern = "-+"
strData = objRegExp.Replace(strData, "-")
objRegExp.Pattern = "'+"
strData = objRegExp.Replace(strData, "'")
'Trim
strData = Trim(strData)
'Return result
CleanString = strData
ExitScript:
Exit Function
ErrorHandler:
CleanString = "ERROR\_OCCURRED:" & "Error #" & Err & ": " & Error$ & " (Procedure: " & PROCNAME & ")"
Resume ExitScript
End Function
Private Function GetFileDir() As String
Const PROCNAME As String = "GetFileDir"
On Error GoTo ErrorHandler
Dim ret As String
Dim lpIDList As Long
Dim sPath As String
Dim udtBI As BrowseInfo
Dim RdStrings() As String
Dim nNewFiles As Long
'Show a browse-for-folder form:
With udtBI
.lpszTitle = lstrcat(EXM\_016, "")
.ulFlags = BIF\_RETURNONLYFSDIRS
End With
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList = 0 Then Exit Function
'Get the selected folder.
sPath = String$(MAX\_PATH, 0)
SHGetPathFromIDList lpIDList, sPath
CoTaskMemFree lpIDList
'Strip Nulls
If (InStr(sPath, Chr$(0)) \> 0) Then sPath = Left$(sPath, InStr(sPath, Chr(0)) - 1)
'Return Dir
GetFileDir = sPath
ExitScript:
Exit Function
ErrorHandler:
GetFileDir = "ERROR\_OCCURRED:" & "Error #" & Err & ": " & Error$ & " (Procedure: " & PROCNAME & ")"
Resume ExitScript
End Function