Forum Discussion
Permission Denied error when moving a file
Hi all,
I'm using Excel 365 MSO (Version 2401 Build 16.0.17231.20236) 64-bit.
I have a spreadsheet (let's call it 'Main') that contains a bunch of VBA to do the following. It first imports data from several source spreadsheets (one at a time) into a new tab on Main. Once each spreadsheet's data is imported, the data is then extracted and pushed it into an Access database. The tab is then closed and the process repeats until there are no more source files to process. The code then grabs the data from Access, formats it all and pushes it into a text file for later upload by a third-party app. Once that's done, I have code to move the source spreadsheets into an archive folder.
The whole process works seamlessly, except that I get a 'Permission Denied' error when calling the fso.MoveFile() method on the very last source spreadsheet I imported. I can't think of why I'm getting said error, which is why I'm asking here.
Here is the code I use the import one of the files into Main. There are four different source spreadsheets, each formatted differently, but I use (essentially) the same code block to import each. Of course, the specific query differs from file to file.
ActiveWorkbook.Queries.Add Name:="PackingList", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Excel.Workbook(File.Contents(""" & fil.Path & """), null, true)," & Chr(13) & "" & Chr(10) & " PackingList1 = Source{[Name=""PackingList""]}[Data]," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(PackingList1, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(#""Promoted Headers"",{{""MY " & _
"COMPANY LLC #(lf)(CUSTOMER NAME) "", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type datetime}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", ty" & _
"pe text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=PackingList;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.Visible = False
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [PackingList]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "PackingList"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Name = "DataImport"
Sheets("DataImport").Delete
When all the source spreadsheets have been imported and closed, and the data have been exported to Access, I use the following code prior to creating the export text files and attempting to move the source files to the Archive folder:
Sub DeleteAllTablesNQueries()
Dim cn As Object
Dim qry As Object
On Error Resume Next
'Delete all connections
For Each cn In ThisWorkbook.Connections
cn.Delete: DoEvents
Next cn
'Delete all queries
For Each qry In ThisWorkbook.Queries
qry.Delete: DoEvents
Next qry
Set qry = Nothing
Set cn = Nothing
End Sub
I know Access VBA very well (I'm a former Access MVP), but I don't pretend to be an Excel wiz - far from it. So I'm hoping someone here can tell me what I'm doing wrong. The error occurs when I call the fso.MoveFile() method.
7 Replies
- JKPieterseSilver ContributorYou haven't posted the code containing the fso.MoveFile statement?
- gseachCopper ContributorHi Jan,
Thanks for responding.
fso is the Scripting.FileSystemObject, so it's pretty simple.
fso.MoveFile(strFilespec, strNewFilespec)
The source file has already been closed by the time the MoveFile() method is called. In fact, the procedure that opened and closed the source file has already gone out of scope.- JKPieterseSilver Contributor
gseach Of course, I know the fso library. But what other stuff is done before and after the statement might be important. Anyway, is there anything that might refer to the file that fails to move? Like a data connection?