2017-01-29 32 views
2

我已經使用了下面的代碼來從outlook下載電子郵件列表中的附件。對象變量或塊變量未設置錯誤發生在for循環的第二個迭代中

代碼工作精細的循環的第一次迭代,但在第二次迭代它錯誤與Run-time error '91' Object variable or With block variable not set在它試圖將文件保存到桌面上的一個臨時文件夾(即,線wb.SaveAs FileFormat:=51, FileName:=xlNameAndPath)的步驟。

從閱讀文檔here和一些測試,似乎這個問題實際上是被在循環的第一次迭代由wb.close造成的,這臺wb不了了之,然後導致錯誤在第二次迭代。

如果我是對的,那麼我的問題是如何「重新指定對象變量的引用」?

Sub SaveExcels() 

Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI") 
Dim olFolder As Outlook.MAPIFolder 
Set olFolder = objNS.GetDefaultFolder(olFolderInbox) 
Dim Item As Object 
Dim objAttachments As Outlook.Attachments 

For Each Item In olFolder.Items 

    If TypeOf Item Is Outlook.MailItem Then 

     Dim oMail As Outlook.MailItem: Set oMail = Item 

     ' Check it contains an attachment 
     Set objAttachments = oMail.Attachments 
     lngCount = objAttachments.Count 

     ' Check its from the right company 
     senderCheck = InStr(oMail.SenderEmailAddress, "company.com") 

     ' Check that it is the right email type 
     subjectCheck = InStr(oMail.Subject, "TYPE") 

     ' Check whether its the latest weeks data 
     receivedDate = DateValue(oMail.ReceivedTime) 
     todaysDate = DateValue(Now()) 
     dateDifference = todaysDate - receivedDate 

     If lngCount > 0 And senderCheck > 0 And subjectCheck > 0 And dateDifference <= 7 Then 

      ' Get the file name 
      strFile = objAttachments.Item(1).FileName 
      ' Debug.Print strFile 

      strFolderpath = "D:\Users\" & Environ("Username") & "\Desktop\temp\" 

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

      ' Save the attachment as a file. 
      objAttachments.Item(1).SaveAsFile strFileIncPath 

      ' Extract the files into the newly created folder 
      Set oApp = CreateObject("Shell.Application") 
      oApp.NameSpace(strFolderpath).CopyHere oApp.NameSpace(strFileIncPath).Items 

      ' Delete the zip file 
      Kill strFileIncPath 

      ' Open the excel file 
      Dim xlApp As Object 
      Set xlApp = CreateObject("Excel.Application") 

      xlApp.Application.Visible = True 
      xlName = Replace(strFile, ".ZIP", "") 
      xlNameTemp = xlName & "_00000.xls" 
      xlNameAndPath = strFolderpath & xlName 
      Debug.Print xlNameAndPath 

      xlApp.Workbooks.Open strFolderpath & xlNameTemp 

      Dim wb As Workbook 
      Set wb = ActiveWorkbook 

      ' Save as unique name and close 
      wb.SaveAs FileFormat:=51, FileName:=xlNameAndPath << ERROR 

      ' Get rid of the old excel 
      Kill strFolderpath & xlNameTemp 

      ' Close the workbook 
      wb.Close 

     End If 

    End If 

Next 

End Sub 
+0

在您的「設置wb = ActiveWorkbook」中,是否可以使用調試器查看ActiveWorkbook是否有效? (順便說一下,ActiveWorkbook是在哪裏定義的?) –

+0

@SandyGettings我假設Sam添加了對類型庫「Microsoft Excel Object Library」(VBA Editor,Tools | References)的引用。這使'Workbook'和類似的類型在'Dim'語句中可見。它還爲您提供「ActiveWorkbook」全局,以及「ActiveSheet」和「xl *」常量。 – cxw

回答

3

我相信

Dim wb As Workbook 
Set wb = xlApp.Workbooks.Open(strFolderpath & xlNameTemp) 

將做的工作,每the docs。 (未測試-YMMV!)

相關問題