The basic idea is to build a DOS .BAT file to do the copying for you, then execute that file. Here's the code to execute the .BAT file. You should only use this AFTER you have verified that the program is building the file correctly.
Code:
'return code from shell commands below
Dim ShellRC As Integer
' strBatFile is a string containing the full path and file name of the bat file you created above
'execute the DelCopy.bat file to delete old spreadsheets
ShellRC = Shell(strBatFile)
'Delay to allow shell to finish
Sleep 20000 'Implements a 20 second delay - vary this as needed
Here's sample code to build the BAT file. This code hasn't been tested, since I had to strip out a lot of proprietary stuff and unnecessary complications that wouldn't have been useful for showing the technique.
Code:
‘ Here’s a simple example. I’ve hard-coded everything
‘ Except for the loop
‘batch file name
Dim strBatFile As String
strBatFile = "C:\fullpath\batfolder\namehere\DelCopy.bat"
' Open bat file for output
Open strBatFile For Output As #1
‘ tell DOS what Drive should be current
Print #1, Tab(1); "C: "
‘ set the current folder on the drive
Print #1, Tab(1); "CD C:\fullpath\copyfolder\namehere\"
‘*****************************************
‘ Here is where you do the actual copy work
‘ you can adapt this to build your correct
‘ source and destination files
‘*****************************************
‘ In this example, aFileName is an array of
‘ strings loaded somewhere above, and
‘ each string in that array is one file to be
‘ newly created from a fresh copy of Template.XLS
‘
‘ limFN is the number of file names in aFileName
‘
‘ Dim aFileName(100) As String
‘ Dim limFN As Integer
‘
‘*****************************************
‘ delete the old backups from the folder
Print #1, Tab(1); "del *.prev"
‘ for each file in the array,
‘ rename the old .XLS to .PREV
‘ copy the template to .XLS
Dim xFN As Integer
Dim strOutline As String
Dim DQ As String
DQ = Chr(34) 'string variable representing a double quote
For xFN = 1 To limFN Step 1
‘copy .XLS to .PREV
strOutline = "ren " & DQ & aFileName(xFN) & ".xls" & DQ & " " & DQ & aFileName(xFN) & ".prev" & DQ
Print #1, Tab(1); strOutline
‘copy template.XLS to .XLS
strOutline = "copy Template.xls " & DQ & aFileName(xFN) & ".xls" & DQ
Print #1, Tab(1); strOutline
Next
Print #1, Tab(1); "exit"
Close #1 ' Close file.