Ich habe den folgenden Code, den ich in Outlook verwende, um einen Anhang aus einer Liste von E-Mails herunterzuladen.Objektvariable oder mit Blockvariable nicht gesetzt Fehler tritt in der zweiten Iteration der for-Schleife auf
Der Code funktioniert für die erste Iteration der Schleife in Ordnung, aber auf der zweite Iteration es Fehler mit Run-time error '91' Object variable or With block variable not set
bei dem Schritt, wo es versucht, wird die Datei in einen temporären Ordner auf dem Desktop zu speichern (das heißt die Linie wb.SaveAs FileFormat:=51, FileName:=xlNameAndPath
).
Aus der Dokumentation here Lesen und einige Tests, so scheint es, dass das Problem tatsächlich in der ersten Iteration der Schleife durch wb.close
wobei verursacht wird, setzt diese wb
zu nichts, die dann den Fehler in der zweiten Iteration verursacht.
Wenn ich richtig bin, dann ist meine Frage, wie man "eine Referenz für die Objektvariable" wiedergibt?
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
In Ihrem "Set wb = ActiveWorkbook" können Sie den Debugger verwenden, um zu sehen, ob ActiveWorkbook gültig ist? (Übrigens, wo ist ActiveWorkbook überhaupt definiert?) –
@SandyGettings Ich nehme an, dass Sam einen Verweis auf die Typbibliothek "Microsoft Excel Objektbibliothek" (VBA-Editor, Tools | Referenzen) hinzugefügt hat. Das macht "Arbeitsmappe" und ähnliche Typen in Dim-Anweisungen sichtbar. Es gibt auch das 'ActiveWorkbook' global, zusammen mit' ActiveSheet' und den 'xl *' Konstanten. –
cxw