PDA

View Full Version : Trying to save on computers with different FileFormats



JeffL
04-05-2013, 03:44 PM
One last (I hope) problem.
I want to be able to run my Excel app on an XP laptop using Office 2000, and my Win 8 desktop using Office 2013.

I have code in the macro to open a .CSV file, and close it as a .XLS file. The formats are different, which I thought I took care of with this code:

' Get the correct file format the version of Excel running
If Application.OperatingSystem = "Windows (32-bit) NT 5.01" Then
' For old Excel
ActiveWorkbook.SaveAs Filename:="C:\Jeff\Temp\StreetSmart.xls", FileFormat _
:=xlExcel9795, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False
Else
' For new Excel
ActiveWorkbook.SaveAs Filename:="C:\Jeff\Temp\StreetSmart.xls", FileFormat _
:=xlExcel8, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
False, CreateBackup:=False
End If

It works fine on the Win 8 computer, but the XP computer with the older Office 2000 doesn't like the variable xlExcel8. Even though it's not used on that computer, I get an error because it's not defined. It will run if I define it as a string, but then the Win 8 computer running Office 2013 gives me an error. I was hoping there was a way to set up my own variable for FileFormat:= but I haven't been able to come up with anything.
How can I make this work in both environments?

JeffL
04-05-2013, 06:36 PM
Update: I figured out a solution.
I put the SaveAs macro code for each version of Excel in its own subroutine. Then I just call the appropriate routine. It never looks at the one not being used, and it all works. :beerchug:

snb
04-06-2013, 04:01 AM
Sub M_snb()
if Val(application.version)<10 then
ActiveWorkbook.SaveAs "C:\Jeff\Temp\StreetSmart.xls"
else
ActiveWorkbook.SaveAs "C:\Jeff\Temp\StreetSmart.xls",43
end if
end sub

JeffL
04-06-2013, 09:42 AM
Thanks snb.

Even though my solution works, I gave your code a try since it's so much cleaner (I generated my code by recording the keystroke macro). I'm on the desktop using 2013, so it followed the "Else" path.
It didn't work.

Run-time error 1004
Method 'SaveAs' of object'_Workbook' failed

snb
04-06-2013, 12:07 PM
Sub M_snb()
If Val(application.version)<10 Then
ActiveWorkbook.SaveAs "C:\Jeff\Temp\StreetSmart.xls"
Else
ActiveWorkbook.SaveAs "C:\Jeff\Temp\StreetSmart.xls",56
End If
End Sub

JeffL
04-06-2013, 03:45 PM
:thumb That's better! I like it. I'll try it on my note book later.
Thanks.

snb
04-07-2013, 03:44 AM
You could have figured out yourself.....; using F2 (object browser) in the VBEditor.