【发布时间】:2020-06-23 08:26:29
【问题描述】:
我已将 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
【问题讨论】:
-
据我所知,以这种方式从下载的邮件项创建对象并不容易。
MailItem对象适用于应用程序中存在的邮件。如果您进一步解释您要测试的内容,或许还有其他解决方案。 -
我正在尝试测试一个下载 Outlook 项目附件的 vba 脚本。通常它工作正常,但这次它存储了一个损坏的文件。所以我想调试一下代码看看为什么会这样。
-
也许如果你分享你的 vba...
-
添加了调试存储文件时损坏文件的部分的代码。但是,它并不总是损坏存储它,只是偶尔。因此,为什么我想了解它何时发生,因此我需要知道如何调试它
-
...大概只有某些邮件? (如,可重现?)