2014-10-01 99 views
0

我正在使用以下代码将附件从电子邮件保存到文件夹中,现在我想添加一个if条款或条件,该条款只保存具有.pdf扩展名的附件。VBA使用pdf扩展名保存电子邮件附件到文件夹

是否有人可以告诉我怎么可以改变我的代码来获得这种情况发生,在此先感谢

Public Sub SaveAttachments() 
Dim objOL As Outlook.Application 
Dim objMsg As Outlook.MailItem 'Object 
Dim objAttachments As Outlook.Attachments 
Dim objSelection As Outlook.Selection 
Dim i As Long 
Dim lngCount As Long 
Dim strFile As String 
Dim strFolderpath As String 
Dim strDeletedFiles As String 

    ' Get the path to your My Documents folder 
    On Error Resume Next 

    ' Instantiate an Outlook Application object. 
    Set objOL = CreateObject("Outlook.Application") 

    ' Get the collection of selected objects. 
    Set objSelection = objOL.ActiveExplorer.Selection 

' The attachment folder needs to exist 
' You can change this to another folder name of your choice 

    ' Set the Attachment folder. 
    strFolderpath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\TEST\" 

    ' Check each selected item for attachments. 
    For Each objMsg In objSelection 

    Set objAttachments = objMsg.Attachments 
    lngCount = objAttachments.Count 

    If lngCount > 0 Then 

    ' Use a count down loop for removing items 
    ' from a collection. Otherwise, the loop counter gets 
    ' confused and only every other item is removed. 

    For i = lngCount To 1 Step -1 

    ' Get the file name. 
    strFile = objAttachments.Item(i).FileName 

    ' Combine with the path to the Temp folder. 
    strFile = strFolderpath & strFile 

    ' Save the attachment as a file. 
    objAttachments.Item(i).SaveAsFile strFile 

    Next i 
    End If 

    Next 

ExitSub: 

Set objAttachments = Nothing 
Set objMsg = Nothing 
Set objSelection = Nothing 
Set objOL = Nothing 
End Sub 

回答

1

你会希望通过attachments收集,反覆您objMsg找到PDF。

这将是这样的:

For each objAttachment in objMsg.Attachments 
    if Right(objAttachment.FileName, 3) = "pdf" then 
      objAttachment.SaveAsFile strFolderPath & strFile 
    end if 
Next objAttachment 

只要确保你decalre objAttachment顶部有: Dim objAttachment as Attachment

从你的例子完整的代码更新时间:

Public Sub SaveAttachments() 
    Dim objOL As Outlook.Application 
    Dim objMsg As Outlook.MailItem 'Object   
    Dim strFile As String 
    Dim strFolderpath As String 
    Dim strDeletedFiles As String 

    ' Get the path to your My Documents folder 
    On Error Resume Next 

    ' Instantiate an Outlook Application object. 
    Set objOL = CreateObject("Outlook.Application") 

    ' Get the collection of selected objects. 
    Set objSelection = objOL.ActiveExplorer.Selection 

    ' The attachment folder needs to exist 
    ' You can change this to another folder name of your choice 
    ' Set the Attachment folder. 
    strFolderpath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\TEST\" 

    ' Check each selected item for attachments. 
    For Each objMsg In objSelection 
     For each objAttachment in objMsg.Attachments 
      if Right(objAttachment.FileName, 3) = "pdf" then     

        ' Append the file name to the folder. 
        strFile = strFolderpath & objAttachment.FileName 

        ' Save it 
        objAttachments.Item(i).SaveAsFile strFile     
      end if 
     Next objAttachment 
    Next objMsg 

ExitSub: 
    Set objAttachments = Nothing 
    Set objMsg = Nothing 
    Set objSelection = Nothing 
    Set objOL = Nothing 
End Sub 
+0

感谢,但这样做你知道我的代码在哪里,我会把这个,因为我继续得到一个编译错误,而不是 – 2014-10-01 12:44:02

+0

你应该坚持这个你r'For Each objMsg ...'循环 – JNevill 2014-10-01 12:48:07

+0

即时通讯但是stile出现编译错误 – 2014-10-01 12:50:46

相关问题