繁体   English   中英

使用单元格值中的文件名保存 outlook 附件

[英]Save outlook attachment using the filename from a cell value

我实际上试图将 outlook 附件从特定子文件夹保存到本地路径。 我能够将文件按原样保存到本地路径。 但是,要求是使用 excel 文件中给出的文件名(即单元格值)保存 xl 附件。 我希望代码使用 ThisWorkbook 的单元格值中给出的文件名。

让我知道是否有办法。 感谢对此的任何支持。

Sub ManualPunchAttachmentsExtract()
    Dim OlFolder As Outlook.MAPIFolder
    Dim OlMail As Object
    Dim OlApp As Outlook.Application
    Dim OlItems As Outlook.Items
    Dim Get_namespace As Outlook.Namespace
    Dim strFolder As String
    Dim i As Integer
    
    ThisWorkbook.Activate
    Sheets("MP File Save").Activate
    Range("H3").Activate
    
    Set OlApp = GetObject(, "Outlook.Application")
    
    If err.Number = 429 Then
        Set OlApp = CreateObject("Outlook.Application")
    End If
    
    strFolder = InputBox("Please Enter the Folder Path alongwith ' \ ' at the end", Path)
    
    'Set Get_namespace = OlApp.GetNamespace("MAPI")
    Set OlFolder = OlApp.GetNamespace("MAPI").Folders("shaikajaz.k@flex.com").Folders("Archive").Folders("Juarez").Folders("Manual Punch")
    Set OlItems = OlFolder.Items
    '.Restrict("[Unread]=true")
    
    For Each OlMail In OlItems
    
         If OlMail.UnRead = False Then
             
         Else
            ThisWorkbook.Activate
            Sheets("MP File Save").Activate
            ActiveCell.Value = OlMail.Subject
            ActiveCell.Offset(0, 1).Value = OlMail.ReceivedTime
            
            If OlMail.attachments.Count > 0 Then
                For i = 1 To OlMail.attachments.Count
                    OlMail.attachments.Item(i).SaveAsFile strFolder & "\" & OlMail.attachments.Item(i).FileName
                    OlMail.UnRead = False
                    ThisWorkbook.Activate
                    ActiveCell.Offset(1, 0).Select
                Next i
            
            Else
            
            End If
         
         End If
                
    Next
MsgBox ("Done")
End Sub

首先,遍历 Outlook 文件夹中的所有项目并不是一个好主意。 请改用 Items class 的Find / FindNextRestrict方法。 所以,而不是下面的代码:

For Each OlMail In OlItems

     If OlMail.UnRead = False Then

用这个:

Private Sub FindAllUnreadEmails(folder As Outlook.MAPIFolder)

    Dim searchCriteria As String = "[UnRead] = true"
    Dim counter As Integer = 0
    Dim mail As Outlook._MailItem = Nothing
    Dim folderItems As Outlook.Items = Nothing
    Dim resultItem As Object = Nothing
   
        If (folder.UnReadItemCount > 0) Then
            
            folderItems = folder.Items
            resultItem = folderItems.Find(searchCriteria)
            While Not IsNothing(resultItem)
                If (TypeOf (resultItem) Is Outlook._MailItem) Then
                    counter += 1
                    mail = resultItem
                    Debug.Print("#" + counter.ToString() + _
                                          " - Subject: " + mail.Subject)
                End If
                resultItem = folderItems.FindNext()
            End While
            
        Else
            Debug.Print("There is no match in the " + _
                                   folder.Name + " folder.")
        End If
End Sub

请注意,附加文件可以具有相同的文件名。 因此,为了唯一标识文件,我建议在将附件保存到磁盘时在文件名中引入任何 ID。

最后,要使用工作簿的内容名称保存附件,您需要将单元格值传递给SaveAsFile方法:

OlMail.attachments.Item(i).SaveAsFile strFolder & "\" & yourWorksheet.Range("B2").Value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM