netrixuser
Board Regular
- Joined
- Jan 21, 2019
- Messages
- 77
- Office Version
- 365
- Platform
- Windows
Good evening,
I have some code given to me to save a zipped email attachment to disk, unzip the file and then open the csv file. The code worked once but now throws an error 1004 at the .refresh command.
I could probably use my limited knowledge to use the Get Data approach and then manipulate the data but I would like to try and understand the method below a little more.
The code to get the email attachment saved as a .csv file onto a 0365 Share Point location works fine. The next lines of code are:
For the first run of the code, the data from the csv file was populated in the Scratch Sheet tab. Before running the code again (with the same attachment) I deleted the csv file from the attachment destination folder and deleted the data from the Scratch Sheet tab - I did get a pop-up warning when deleting the data - but didn't take note as I just expected the data to be re entered when the code ran for the second time.
Any help very much appreciated.
Kind regards
Netrix
I have some code given to me to save a zipped email attachment to disk, unzip the file and then open the csv file. The code worked once but now throws an error 1004 at the .refresh command.
I could probably use my limited knowledge to use the Get Data approach and then manipulate the data but I would like to try and understand the method below a little more.
The code to get the email attachment saved as a .csv file onto a 0365 Share Point location works fine. The next lines of code are:
VBA Code:
'Open file into workbook
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(AttachmentDestinationFolder) 'this line resolves to the location of the csv file (I have the SP location synced to my OneDrive)
Dim FileName As String
For Each oFile In oFolder.Files
FileName = oFile.Path
Exit For
Next oFile
Dim Ws As Worksheet
Set Ws = ActiveWorkbook.Sheets("Scratch Sheet") 'I am running the code from within the active workbook
With Ws.QueryTables.Add(Connection:="TEXT;" & FileName, _
Destination:=Ws.Range("A1"))
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh 'The code throws an error 1004 here - see uploaded screenshot
End With
For the first run of the code, the data from the csv file was populated in the Scratch Sheet tab. Before running the code again (with the same attachment) I deleted the csv file from the attachment destination folder and deleted the data from the Scratch Sheet tab - I did get a pop-up warning when deleting the data - but didn't take note as I just expected the data to be re entered when the code ran for the second time.
Any help very much appreciated.
Kind regards
Netrix