Hi Everyone,
Here is a quick review of what is going on. Everyday I get an email with a file attached, and I have outlook rules set up to have this email sent to a specific folder each day. In an excel file I have code that checks that folder each day for a) an unread email and b) if there is an attachment in the file. If both conditions are true then the code should download and save the attachment in xlsb format. To give credit where it is due, most of this code was sourced from items I found on Ron de Bruin website, along with other forums with a few small tweaks.
The code I have above works to save the file in the right place, but when I try to access it I get the following error - "Excel cannot open the file 'Daily Reporting File 01_19_17'.xlsb' because the file format or file extensions is not valid. Verify that the file has not been corrupted and that the file extensions matched the format of the file."
In the past I used used a line of code like
to save another excel workbook in xlsb format, but when I try to input into the code above in the place of
it errors out on me. I am assuming that it has something to do with the PaceFile being declared as an attachment but really I am not sure. I have searched for answers but have been unable to find anything.
Can anyone please help me to save this file in the right format and be able to access it? Thanks for the help.
Here is a quick review of what is going on. Everyday I get an email with a file attached, and I have outlook rules set up to have this email sent to a specific folder each day. In an excel file I have code that checks that folder each day for a) an unread email and b) if there is an attachment in the file. If both conditions are true then the code should download and save the attachment in xlsb format. To give credit where it is due, most of this code was sourced from items I found on Ron de Bruin website, along with other forums with a few small tweaks.
Code:
Option Explicit
Sub do_something_cool()
'Note be sure to early bind with outlook
'Create the file path where I want the file saved
Dim AttachementPath As String: Dim NewFileName As String
AttachementPath = "C:\Daily File Download\"
NewFileName = AttachementPath & "Daily Reporting File " & Format(Date, "MM-DD_YY")
Dim olApp As Outlook.Application
Dim ns As Outlook.Namespace
Dim Inbox_Prog_Pace As MAPIFolder
Dim Item As Object
Dim PaceFile As Attachment
Set ns = GetNamespace("MAPI")
Set Inbox_Prog_Pace = ns.GetDefaultFolder(olFolderInbox).Folders("Special Location").Folders("Pacing File")
'Check the pacing file folder for an unread email
'If there are no unread emails then display a message
If Inbox_Prog_Pace.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "The pacing file has already been read and will not be downloaded"
Exit Sub
End If
'Get the attachment from the 1st unread email
For Each Item In Inbox_Prog_Pace.Items.Restrict("[Unread] = True")
'checking if there is actually an attachment
If Item.Attachments.Count <> 0 Then
For Each PaceFile In Item.Attachments
PaceFile.SaveAsFile NewFileName
Exit For
Next
Else
MsgBox "There is not an attachment in the email, please QA"
End If
Item.UnRead = False
DoEvents
Item.Save
Exit For
Next
End Sub
The code I have above works to save the file in the right place, but when I try to access it I get the following error - "Excel cannot open the file 'Daily Reporting File 01_19_17'.xlsb' because the file format or file extensions is not valid. Verify that the file has not been corrupted and that the file extensions matched the format of the file."
In the past I used used a line of code like
Code:
PaceFile.SaveAs FileName:= NewFileName & ".xlsb", FileFormat:=50
Code:
PaceFile.SaveAsFile NewFileName
Can anyone please help me to save this file in the right format and be able to access it? Thanks for the help.