2014-10-01 12 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

спасибо, но делать вы знаете, где в моем коде я бы поставил это, так как я продолжаю получать ошибку компиляции, следующей за –

+0

. Вы должны придерживаться этого внутри вас r 'Для каждого objMsg ...' loop – JNevill

+0

im, который делает это, но stile появляется с ошибкой компиляции –

Смежные вопросы