Opening and saving Excel from Access

dark11984

Registered User.
Local time
Today, 09:46
Joined
Mar 3, 2008
Messages
129
I'm after an access code to open an excel spreadsheet and save as another name than close the spreadsheet. My spreadsheet is located in I:\Databases\StockDB\SarkoezyTemp\NswStoresOM2.xls

Can anyone help please?
 
Will this work if I have to change the file type as well when I save as?
 
Hmmmm. The way you say that scares me a little.

Neither FileCopy() nor Kill() care what the file name is. You'll need to supply the logic to use them, including any logic for giving the file names to copy and delete.

You could potentially use Dir() in conjunction with them to get closer to using a file type, but you need to be careful.
 
I'm guessing (just guessing) that you might be wanting to change it to a CSV file, perhaps. If so, this would do it for you:
Code:
Function RenameExcelFile(strFileName As String, strNewName As String, strNewFileType As String)
    ' xlCSV for CSV
    'xlNormal for normal Excel for the current version
    Const xlCSV As Integer = 6
    Const xlNormal As Integer = -4143
    Dim objXL As Object
    Dim xlWB As Object
    
    Set objXL = CreateObject("Excel.Application")
    
    Set xlWB = objXL.Workbooks.Open(strFileName)
    
    xlWB.SaveAs strNewName, strNewFileType

    
    xlWB.Close False
    objXL.Application.Quit
    
    Set xlWB = Nothing
    Set objXL = Nothing
    
    
End Function

And the usage would be like this:
Code:
RenameExcelFile "C:\Test\Text.xls","C:\Test\Text.csv", xlCSV
 
Bob,

This answers my problem as well only I want to save a backup of the file with a mm-yy added to the end of the file name and save as an .xls. Can I just & the date some way?
 
To insert the date into the file name use

Dim strDate as String
strDate = Format(Date(),"yyyymmdd")

OldFileName = "C:\Test\Test.xls"
NewFileName = Replace(OldFileName,".xls",strDate & ".xls")

FileCopy OldFileName,NewFileName
 
To open an Excel work book
Code:
Dim DataDate As String, wksBook As Excel.Workbook, srcBook As Excel.Workbook, wksSheet As Excel.Worksheet,
    objExcel As Excel.Application
       srcFile = "H:\directory\filename.xls"
       Workbooks.Open (srcFile)
To close with a new name held in srcFile
Code:
ActiveWorkbook.SaveAs FileName:=srcFile, FileFormat:= _
        xlNormal
 
 ActiveWorkbook.Close
 

Users who are viewing this thread

Back
Top Bottom