This microservice takes the following approach to move an Excel file:
- Opens an Excel file (the “source file”).
- Saves it as a new file (the “destination file”).
- Deletes the source file.
This is handy if you download a report as a CSV but want to save it as an XLSX or vice versa.
MoveExcelFile
Sub MoveExcelFile(PathToSource As String, PathToDestination As String, Optional FileType As Long = 51)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim SourceWorkbook As Workbook: Set SourceWorkbook = Workbooks.Open(PathToSource, False, True)
SourceWorkbook.SaveAs PathToDestination, FileType
SourceWorkbook.Close SaveChanges:=False
Kill PathToSource
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Parameter | Description |
---|---|
PathToSource | This is the existing Excel file (the “source file”) you would like to move. DO include the file extension, e.g.: “C:\Users\AHavas\Downloads\My Draft Report.csv” |
PathToDestination | This is where you would like to move the source file to. DO NOT include the file extension, e.g.: “C:\Users\AHavas\Downloads\My Final Report” |
FIleType | An optional file type. Valid options include: • xlOpenXMLWorkbook – .xlsx – Excel Workbook – (DEFAULT – you can leave this parameter blank and destination file will be saved as an .xlsx) • xlOpenXMLWorkbookMacroEnabled – .xlsm – Excel Macro-Enabled Workbook • xlExcel12 – .xlsb – Excel Binary Workbook • xlCSV – .csv – CSV (Comma delimited) |
Examples
MoveExcelFile "C:\Users\AHavas\Downloads\Report Alpha.xlsx", "C:\Users\AHavas\Downloads\Report Alpha", xlCSV
This will do the following:
- Opens the Excel file named Report Alpha.xlsx at C:\Users\AHavas\Downloads\
- Saves it as Report Alpha in the CSV format at C:\Users\AHavas\Downloads\
- Deletes C:\Users\AHavas\Downloads\Report Alpha.xlsx