问题
I am using the following code to archive my emails to a designated folder which works perfectly at the moment.... UNLESS the email subject contains an *... this then gives a debug message "run-time error '-2147286788 (800300fc)'
Is there anything I can add into the below code to make it ignore or replace the * to something else to allow it to automatically archive these emails?
Option Explicit
Public Sub Received2016()
Dim oMail As Outlook.MailItem
Dim objItem As Object
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
For Each objItem In ActiveExplorer.Selection
Set oMail = objItem
sName = oMail.Subject
ReplaceCharsForFileName sName, "_"
dtDate = oMail.ReceivedTime
sName = Format(dtDate, "yyyy-mm-dd - ", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(dtDate, "hh-nn-ss", _
vbUseSystemDayOfWeek, vbUseSystem) & " - " & sName & ".msg"
sPath = "H:\Email Archive\2016 Emails\Received\"
Debug.Print sPath & sName
oMail.SaveAs sPath & sName, olMSG
Next
End Sub
Private Sub ReplaceCharsForFileName(sName As String, _
sChr As String _
)
sName = Replace(sName, "/", sChr)
sName = Replace(sName, "\", sChr)
sName = Replace(sName, ":", sChr)
sName = Replace(sName, "?", sChr)
sName = Replace(sName, Chr(34), sChr)
sName = Replace(sName, "<", sChr)
sName = Replace(sName, ">", sChr)
sName = Replace(sName, "|", sChr)
End Sub
回答1:
Remove all the Replaces and add in this instead (changing characters as necessary) -
sName = RemoveSpecials(sName)
Function RemoveSpecials(strInput As String) As String
Dim strChars As String
strChars = "!£$%^&*()_+{}@~:<>?,./;'#[]-=`¬¦" & Chr(34)
Dim intIndex As Integer
For intIndex = 1 To Len(strChars)
strInput = Replace(strInput, Mid(strChars, intIndex, 1), "")
Next
RemoveSpecials = strInput
End Function
来源:https://stackoverflow.com/questions/36474383/outlook-email-archiving-macro-doesnt-work-if-subject-has-asterisk