Outlook下载附件宏跳过奇数文件

时间:2013-11-08 10:23:17

标签: vba download outlook attachment

我继承了一段从Outlook 2010中的用户窗体运行的代码。代码应该将选定电子邮件中的所有附加文件保存在公用文件夹中,并保存到用户C盘。

用户向我保证(过去3年)他们必须在早上第一件事“热身宏”。他们说,如果他们选择100封电子邮件,宏将忽略一些附件。但是,如果他们从选择的10封电子邮件开始,则宏将起作用。然后他们在下次运行中选择20,并继续增加。

我设法复制了一次,但只有一次,我看不出原因。

非常感谢任何建议或分享经验。

Sub DownloadFiles()
Dim objFS As Object
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim objFolder As Outlook.MAPIFolder
Dim iLoop As Long
Dim lAttCount As Long, lMessageCount As Long, lngCount As Long
Dim iNameCount As Integer, bContinue As Boolean, lSelCount As Long
Dim strFile As String, strFolderpath As String
Dim lVerCount As Long, bVerNew As Boolean, strVFile As String


'call FSO function to create the local folders if they do not exist
Call TallyFolders

lAttCount = 0
lMessageCount = 0
strFolderpath = "C:\MCSUploads\etally\"

Set objSelection = Application.ActiveExplorer.Selection
Set objFS = CreateObject("Scripting.FileSystemObject")

For lSelCount = 1 To objSelection.Count
    Set objAttachments = objSelection.Item(lSelCount).Attachments
    lngCount = objAttachments.Count

    If lngCount > 0 Then

        For iLoop = lngCount To 1 Step -1
            strFile = "No Attachment"
            strFile = objAttachments.Item(iLoop).FileName
            strFile = strFolderpath & strFile

            If objFS.FileExists(strFile) Then
                'append lSelCount to the filename (not extension) to ensure a unique name
                bContinue = True

                For iNameCount = Len(strFile) To 1 Step -1
                    If bContinue And (Mid(strFile, iNameCount, 1) = ".") Then

                       lVerCount = 1
                       bVerNew = False

                       Do Until bVerNew = True
                            strVFile = Left(strFile, iNameCount - 1) & CStr(lVerCount) & Right(strFile, Len(strFile) - iNameCount + 1)
                            If objFS.FileExists(strVFile) Then
                                lVerCount = lVerCount + 1
                            Else
                                bVerNew = True
                            End If
                        Loop

                        bContinue = False
                    End If
                Next iNameCount

                strFile = strVFile
            End If

            objAttachments.Item(iLoop).SaveAsFile strFile
        Next iLoop
    End If
Next lSelCount

FrmDownloadAttachments1.LblMsg.Visible = True

ExitSub:
Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
End Sub




Sub TallyFolders()
Dim oFileSystem As Object
Dim FolderRaw As String, FolderComplete As String, FolderProblem As String


Set oFileSystem = CreateObject("Scripting.FileSystemObject")
If Not oFileSystem.FolderExists("C:\MCSUploads") Then oFileSystem.CreateFolder ("C:\MCSUploads")


FolderRaw = "C:\MCSUploads\etally\"
FolderComplete = "C:\MCSUploads\etally\Completed\"
FolderProblem = "C:\MCSUploads\etally\Problems\"
If Not oFileSystem.FolderExists(FolderRaw) Then oFileSystem.CreateFolder (FolderRaw)
If Not oFileSystem.FolderExists(FolderComplete) Then oFileSystem.CreateFolder (FolderComplete)
If Not oFileSystem.FolderExists(FolderProblem) Then oFileSystem.CreateFolder (FolderProblem)



FolderRaw = "C:\MCSUploads\LAR\"
FolderComplete = "C:\MCSUploads\LAR\Completed\"
FolderProblem = "C:\MCSUploads\LAR\Problems\"
If Not oFileSystem.FolderExists(FolderRaw) Then oFileSystem.CreateFolder (FolderRaw)
If Not oFileSystem.FolderExists(FolderComplete) Then oFileSystem.CreateFolder (FolderComplete)
If Not oFileSystem.FolderExists(FolderProblem) Then oFileSystem.CreateFolder (FolderProblem)


FolderRaw = "C:\MCSUploads\MAR\"
FolderComplete = "C:\MCSUploads\MAR\Completed\"
FolderProblem = "C:\MCSUploads\MAR\Problems\"
If Not oFileSystem.FolderExists(FolderRaw) Then oFileSystem.CreateFolder (FolderRaw)
If Not oFileSystem.FolderExists(FolderComplete) Then oFileSystem.CreateFolder (FolderComplete)
If Not oFileSystem.FolderExists(FolderProblem) Then oFileSystem.CreateFolder (FolderProblem)
End Sub

1 个答案:

答案 0 :(得分:0)

如果您没有给代码足够的时间来保存附件,那么这是非常可能的。最简单的解决方法是在DoEvents之后添加objAttachments.Item(iLoop).SaveAsFile strFile

另一种方法是在该行之后使用DIR来检查文件是否实际保存。

Debug.Print DIR(strFile)

像这样的东西

Do While Dir(strFile) = ""
    DoEvents
Loop