用于将邮件移动到HDD的Outlook宏工作但打开邮件会出错

时间:2018-04-18 07:18:47

标签: vba outlook outlook-vba

我有一个Outlook宏来通过Outlook规则将传入的消息移动到HDD,它工作正常。

但问题是当我在通过宏复制的HDD上打开消息时,我收到此错误消息

  

"我们无法打开xxxxx。文件已经打开,或者您无权打开文件。要检查权限,请右键单击文件文件夹,然后单击“属性”。"

我检查了我的权限,发现没问题。我也关闭了Outlook并尝试打开保存的消息但没有任何改变,我仍然得到相同的错误消息。

我们如何解决这个错误?

Public Sub AutoSave(oMail As Outlook.MailItem)
    Dim dtDate As Date
    Dim sName As String
    Dim sFile As String
    Dim sExt As String

    sPath = "C:\Users\xxxuser\Desktop\"
    sExt = ".msg"
    sName = oMail.Subject
    ReplaceCharsForFileName sName, "_"
    dtDate = oMail.ReceivedTime
    sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & sExt

    oMail.SaveAs sPath & sName, olSaveAsMsg

End Sub

Public Sub ReplaceCharsForFileName(sName As String, _
    sChr As String _
    )
    sName = Replace(sName, "/", sChr)
    sName = Replace(sName, "", sChr)
    sName = Replace(sName, "FW:", sChr)
    sName = Replace(sName, "RE:", 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 个答案:

答案 0 :(得分:2)

我认为<div class="listingItem"> <input type="hidden" class="listingItemData" value="32142|32142|t|f"> <div class="listingItemData"> <div class="listingDisplayLeft"><a href="/listings/lost-pets/view/32142/test-tiger-demo/" title="View full details"><img src="http://assets.worldwildlife.org/photos/907/images/story_full_width/sumatran-tiger-hero_92514619.jpg?1345581518" style="width:100%;max-width:1024px" alt=""></a></div> <div class="listingDisplayRight"> <div class="listingItemText"> <h3> <a href="/listings/lost-pets/view/32142/test-tiger-demo/" title="View full details">test tiger demo</a> </h3> <div class="listingItemTextDetails"> i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. <br> <br>i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. i lost my pet tiger. </div> <a class="listingItemTextButton" href="/listings/lost-pets/view/32142/test-tiger-demo/" title="View full details">More information</a> </div> </div> </div> </div>是错误的,因为您没有使用olSaveAsMsg,所以您没有看到。所以我建议在模块顶部写Option Explicit。然后您将看到Option Explicit被视为变量而未被声明 它现在应该抛出错误“变量未定义”

因为根据OlSaveAsType Enumeration,空变量可能会转换为olSaveAsMsg,这意味着0,您的邮件很可能以文本格式保存,因此Outlook无法打开它

因此将其保存为Outlook消息格式

  • olTXT “Outlook邮件格式”
  • olMSG用于“Outlook Unicode邮件格式”