Make Copy of File Before Extension Change

erock24

Well-known Member
Joined
Oct 26, 2006
Messages
1,163
I have code that I use to convert a file to a .xls file...It works great. I was wondering though if it were possible to add code to this so that it makes a copy of the file as is before it changes the extension..

Code:
TheFile = Application.GetOpenFilename("LOD Load Files (*.LOD), *.LOD", , "Open LOD File")
    If TheFile = "False" Then
      Exit Sub
    End If
    
      Oldfilename = TheFile
    ' Separate the file path and file name
      BS = InStrRev(Oldfilename, "\")
      Oldfilepath = Left(Oldfilename, BS)
      Newfilename = Right(Oldfilename, Len(Oldfilename) - BS)
    ' Add the XLS extension to the file name
      Dot = InStr(1, Newfilename, ".")
      Newfilename = Oldfilepath & Left(Newfilename, Dot) & "xls"
    ' Rename the file
      Name Oldfilename As Newfilename
    Workbooks.Open Filename:=Newfilename

Thanks,
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I've got it figured out.....found some code and was able to edit.

Code:
 TheFile = Application.GetOpenFilename("LOD Load Files (*.LOD), *.LOD", , "Open LOD File")
    If TheFile = "False" Then
      Exit Sub
    End If
    
'Create Copy of file
    Dim fso As Object
    Set fso = CreateObject("scripting.FileSystemObject")
     'Get a handle to the file
    Set f = fso.GetFile(TheFile)
    ' Copy the file
    f.Copy (TheFile & "Copy.LOD")
    
'Covert file from .LOD to .XLS
      Oldfilename = TheFile
    ' Separate the file path and file name
      BS = InStrRev(Oldfilename, "\")
      Oldfilepath = Left(Oldfilename, BS)
      Newfilename = Right(Oldfilename, Len(Oldfilename) - BS)
    ' Add the CSV extension to the file name
      Dot = InStr(1, Newfilename, ".")
      Newfilename = Oldfilepath & Left(Newfilename, Dot) & "xls"
    ' Rename the file
      Name Oldfilename As Newfilename
    Workbooks.Open Filename:=Newfilename
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top