Outlook Email Archiving Macro Doesnt work if subject has asterisk

心已入冬 提交于 2021-01-28 02:12:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!