Vba Outlook宏仅保存了一个附加文件并停止
我编译了一个运行良好的宏,但后来我进一步“改进”了它,由于某种原因,它在第一次成功循环后停止。宏将电子邮件中的附件保存到NAS上的文件夹中。第二次运行时,它会在SaveAsFile行上停止。谢谢你的评论Vba Outlook宏仅保存了一个附加文件并停止,vba,outlook,Vba,Outlook,我编译了一个运行良好的宏,但后来我进一步“改进”了它,由于某种原因,它在第一次成功循环后停止。宏将电子邮件中的附件保存到NAS上的文件夹中。第二次运行时,它会在SaveAsFile行上停止。谢谢你的评论 Public Sub SaveAttsToNAS(MItem As Outlook.MailItem) Dim oAttachment As Outlook.Attachment ' A document or link to a document contained in an
Public Sub SaveAttsToNAS(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment ' A document or link to a document contained in an Outlook item.
Dim sSaveFolder As String
Dim sPath As String
Dim regDate As Date
Dim strDate As String
Dim objFSO, strFolder
Dim strAtmtName(1) As String ' strAtmtName(0): to save the name; strAtmtName(1): to save the file extension. They are separated by dot of an attachment file name.
Dim intDotPosition As Integer ' The dot position in an attachment name.
Dim strAtmtFullName As String ' The full name of an attachment.
sSaveFolder = "P:\"
regDate = MItem.ReceivedTime
strDate = Format(regDate, "yyyymmdd")
sPath = sSaveFolder & strDate
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(sPath) Then
objFSO.CreateFolder (sPath)
End If
For Each oAttachment In MItem.Attachments
' Get the full name of the current attachment.
strAtmtFullName = oAttachment.FileName
intDotPosition = InStrRev(strAtmtFullName, ".")
' Get the file extension.
strAtmtName(1) = Right$(strAtmtFullName, Len(strAtmtFullName) - intDotPosition)
' Exclude Gifs and save the current attachment.
If strAtmtName(1) <> "gif" Then
oAttachment.SaveAsFile sPath & "\" & strAtmtFullName
End If
Next
End Sub
Public子SaveAttsToNAS(以Outlook.MailItem格式显示)
Dim Otachment作为Outlook。附件“Outlook项目中包含的文档或指向文档的链接。
将文件夹设置为字符串
像细绳一样暗淡
Dim regDate作为日期
作为字符串的Dim strDate
变光objFSO,strFolder
Dim strAtmtName(1)作为字符串'strAtmtName(0):保存名称;strAtmtName(1):保存文件扩展名。它们由附件文件名的点分隔。
Dim intDotPosition As Integer'附件名称中的点位置。
Dim StratmFullName As String'附件的全名。
sSaveFolder=“P:\”
regDate=MItem.ReceivedTime
strDate=格式(regDate,“yyyyymmdd”)
sPath=sSaveFolder&strDate
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
如果不是objFSO.FolderExists(sPath),则
objFSO.CreateFolder(sPath)
如果结束
对于斜接附件中的每个附件
'获取当前附件的全名。
strAtmtFullName=oAttachment.FileName
intDotPosition=InStrRev(strAtmtFullName,“.”)
'获取文件扩展名。
strAtmtName(1)=右$(strAtmtFullName,Len(strAtmtFullName)-intDotPosition)
'排除GIF并保存当前附件。
如果strAtmtName(1)“gif”,则
oAttachment.SaveAsFile sPath&“\”&strAtmtFullName
如果结束
下一个
端接头
I如果错误不一致,您可以等待解决
Public Sub SaveAttsToNAS(MItem As Outlook.MailItem)
....
Dim i As Long
Dim numTries As Long
....
' Exclude Gifs and save the current attachment.
If strAtmtName(1) <> "gif" Then
i = 0
numTries = 100
retryNAS:
On Error GoTo slowNAS
oAttachment.SaveAsFile sPath & "\" & strAtmtFullName
Debug.Print "Attachment path and name: " & sPath & "\" & strAtmtFullName
On Error GoTo 0
End If
Next
Exiting:
Exit Sub
slowNAS:
i = i + 1
Debug.Print i & " Err.number: " & Err.number & " - Err.Description: " & Err.Description
If i >= numTries Then
If MsgBox("Tried " & numTries & " times. Again?", vbYesNo) = vbYes Then
i = 0
Resume retryNAS
Else
Resume Exiting
End If
Else
Resume retryNAS
End If
End Sub
Public子SaveAttsToNAS(以Outlook.MailItem格式显示)
....
我想我会坚持多久
暗淡的珠子一样长
....
'排除GIF并保存当前附件。
如果strAtmtName(1)“gif”,则
i=0
numTries=100
雷特里纳斯:
关于错误GoTo slowNAS
oAttachment.SaveAsFile sPath&“\”&strAtmtFullName
调试。打印“附件路径和名称:”&sPath&“\”&strAtmtFullName
错误转到0
如果结束
下一个
退出:
出口接头
斯洛纳斯:
i=i+1
Debug.Print i&“Err.number:&Err.number&”-Err.Description:&Err.Description
如果i>=numTries,那么
如果MsgBox(“已尝试”&numTries&“再次尝试”?,vbYesNo)=vbYes,则
i=0
复述
其他的
继续退出
如果结束
其他的
复述
如果结束
端接头
编辑问题以包括错误消息的文本和附件的类型。我认为如果LCase(Right$(StratmFullName,3))“gif”,那么将比创建StratmName(1)
所需的代码更容易。您每天有一个输出文件夹。如果在同一天发送的两个附件具有相同的名称,则此代码将失败。感谢您的评论!谢谢你的评论!昨天,在调试过程中,在一个成功保存的文件没有任何错误后,它就停止了。流程就像,首先找到了被跳过的GIF,然后第一个成功保存的PDF,最后完成。没有尝试保存另一个文件,并且有三个PDF文件以不同的名称附加。现在,您可以看到上面的清除代码返回错误消息:“运行时错误-2147024894(80070002)”无法保存附件。找不到此文件。验证路径和文件名是否正确“