我已将 Outlook.MailItem 下载到我的Y:
文件夹中,位置为:
Y:email.msg
在Outlook VBA中,我想测试此项目的脚本。但是,我不确定如何定义它。
我有以下几点:
Dim testMail As MailItem
Set testMail = Application.CreateItem(olMailItem)
但是,如何创建指向我存储的确切项目的链接?
之后,我想使用代码测试将附件存储在此文件中(有时并不总是产生损坏的文件(:
Public Sub Save_File(MItem As Outlook.MailItem)
On Error Resume Next
' init
Dim oAttachment As Outlook.Attachment
Dim folderSave As String
Dim yyyymmdd As String
Dim fileName As String
Dim fileNameFull As String
' date @T (midnight 00:00)
Dim mydate As Date
mydate = MItem.ReceivedTime
' filename and path
yyyymmdd = get_yyyymmdd_prevday(mydate)
folderSave = "V:Operations"
fileName = yyyymmdd & "-FileToStore.csv"
fileNameFull = folderSave & fileName
For Each oAttachment In MItem.Attachments
If fileExist(fileNameFull) = False Then
' if file does not exist
oAttachment.SaveAsFile fileNameFull
End If
Next
End Sub
和帮助功能:
Public Function get_yyyymmdd_prevday(mydate As Date) As String
Dim yyyymmddstr As String
'Previous Business Date
Dim yyyy As String
Dim mm As String
Dim dd As String
If Weekday(mydate) = 2 Then
mydate = mydate - 3
Else
mydate = mydate - 1
End If
yyyy = Year(mydate)
mm = Month(mydate)
dd = Day(mydate)
If Month(mydate) < 10 Then
mm = "0" & mm
End If
If Day(mydate) < 10 Then
dd = "0" & dd
End If
' -->
yyyymmddstr = yyyy & "_" & mm & "_" & dd
get_yyyymmdd_prevday = yyyymmddstr
End Function
要引用.msg文件,有OpenSharedItem
.
Option Explicit
Private Sub Reference_msg_file()
Dim testMailPathFile As String
Dim testMail As MailItem
testMailPathFile = "Y:email.msg"
Set testMail = Session.OpenSharedItem(testMailPathFile)
'testMail.Display
Save_File testMail
ExitRoutine:
Set testMail = Nothing
End Sub
您已禁用使用On Error Resume Next
进行调试。删除此行并研究如何使用它,然后再将其应用于任何将来的代码。
您可以将"出错时恢复下一个"更改为"出错时GoTo处理程序"。
您可以使用它来调试 Outlook.MailItem
有关更多信息,您可以参考此链接:
错误语句 (Visual Basic(