2017-10-04 178 views
0

我從ERP系統接收系統生成的電子郵件,其中包含.zip文件,zip文件內部是一個包含要導入儀表板的數據的excel文件我擁有製作。Outlook VBA解壓縮附件並保存到文件夾

目前,我有這樣的代碼,其成功保存的.zip附件到該文件夾​​時,我收到的電子郵件:

Sub saveAttachment2(Item As Outlook.MailItem) 
Dim selItems   As Selection 
Dim objItem    As Object 
Dim iCount    As Integer 
Dim atmts     As Attachments 
Dim oAttachment As Attachment 
Dim sSaveFolder As String 

Set selItems = ActiveExplorer.Selection 
sSaveFolder = "C:\Users\212357980\Documents\Accounts Coordination\Oracle Exports\" 
For Each objItem In selItems 
    Set atmts = objItem.Attachments 
    For Each oAttachment In atmts 
     oAttachment.SaveAsFile sSaveFolder & "\Service Requests.zip" 
    Next 
Next 
End Sub 

現在我想它解​​壓縮文件時,它的位置保存之前,這是我到目前爲止有:

Sub Unzip2() 

    Dim ns As NameSpace    'variables for the main functionality 
    Dim Inbox As MAPIFolder 
    Dim SubFolder As MAPIFolder 
    Dim Atchmt As Attachment 
    Dim FileName As String 
    Dim msg As Outlook.MailItem 

    Dim FSO As Object    'variables for unzipping 
    Dim oApp As Object 
    Dim FileNameFolder As Variant 
    Set ns = GetNamespace("MAPI") 
    Set Inbox = ns.GetDefaultFolder(olFolderInbox) 
    Set SubFolder = Inbox.Folders("Sales Orders") 

    For Each msg In SubFolder.Items 
      For Each Atchmt In msg.Attachments 
        If (Right(Atchmt.FileName, 3) = "zip") Then 

            FileNameFolder = "C:\Users\212357980\Documents\Accounts Coordination\Oracle Exports\" 
            Set oApp = CreateObject("Shell.Application") 
            With oApp 
             .NameSpace(FileNameFolder).CopyHere 
             .NameSpace(Atchmt.FileName).Items 
            End With 

            On Error Resume Next 
            Set FSO = CreateObject("scripting.filesystemobject") 
           FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True 
        End If 
      Next 
    Next 
End Sub 

我得到錯誤的兩線「的參數或無效的屬性賦值錯號」的後帶。

任何幫助你可以提供將是偉大的!

+0

您需要將壓縮保存到臨時文件夾和從那裏提取它:非常確定,如果不先保存它,你就無法做到這一點。 –

+0

「With oApp」塊中的這兩行應該在一行中。 –

回答

0

外觀: NameSpace(Atchmt.FileName).Items 建議有幾個項目。您正嘗試將其全部收集在一個項目中。

嘗試 對於n = 1(數字附件) 命名空間(Atchmt.FileName).Items(N) ..等等

+0

PeakPeak,我得到上面的線上的錯誤也,我試圖昏暗n爲整數,然後設置n = 1,並添加一個(n)後像你建議的項目,但這並沒有工作,你有任何其他建議? – Harwood95

+0

你是你的案例的專家,你知道它的一切。請不要以爲其他人也完全瞭解它。 請更具體和詳細:你想達到什麼目標?什麼工作已經?什麼是不正常的?什麼是錯誤信息? – peakpeak

+0

嗨高峯期,我相信如果你閱讀原文,你會看到我已經回答了所有這些 – Harwood95

相關問題