File Saveas 2003 error in XL2007

ctild

New Member
Joined
Jan 26, 2008
Messages
44
I've inherited an old Excel 2003 file that saves each worksheet in the Masterfile as a separate file. It works great in XL2003 but falls over each time when using XL2007, on the highlighted line that sets the File name.

Code:
Public Sub SaveWorksheet_as_Workbook(SheetName As String, SavePath As String, SaveName As String)
' Create seperate file for each w/s present
Dim NewWorkbookName As String
Dim MasterWorkbook As String
Dim P1Name As String
WorkbookName = ActiveWorkbook.Name
MasterWorkbook = WorkbookName
Application.ScreenUpdating = False
 
P1Name = SaveName & "_" & SheetName ' SheetName passed from calling procedure
Workbooks.Add xlWorksheet
Sheets("Sheet1").Name = P1Name
ActiveWindow.DisplayGridlines = False
 
ActiveWorkbook.SaveAs FileName:=SavePath & "\" & P1Name
SaveAs2003_Workbook (PathAndName)
Application.ScreenUpdating = False
Workbooks.Open FileName:=SavePath & "\" & P1Name & ".xls", UpdateLinks:=0 ' ##### This line is highlighted!
Windows(WorkbookName).Activate
 
Sheets(SheetName).Select
Cells.Select
Selection.Copy
Windows(P1Name & ".xls").Activate
Cells.Select
ActiveSheet.Paste
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveWindow.Zoom = 50
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.BreakLink Name:=SavePath & "\" & P1Name & ".xls", Type:= _
xlExcelLinks
ActiveWorkbook.Close
Range("C3").Select
End Sub
[code]
 
Unfortunately I'm a complete novice with VB and was hoping someone might be able to amend my code so that not only does it save as an XL2003 (.xls) verion as the default, but works in both XL2003 & 2007 versions.
 
Any help appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
In your code, where exactly did you define the SavePath variable as the path of the workbook you are trying to open?
 
Upvote 0
Hi Tom
If you see this twice - Sorry but I had to make an alteration. (Ignore this line if not)

Unfortunately the guy who created this file has now retired and contains a lot of subs that I don't understand. So I've shown it all below.

He did make comments on some lines which may help you to understand what's happening at the time.

Basically it uses an "Order No" entered on the front "Entry" sheet in Range ("G19") for most of it. Its task is to create a New Folder, based on ("G19") and then go on to create individual files inside it, again using ("G19") + the name of each Sheet name, that apart from a couple, exists in the Master workbook.

Code:
Sub Sub JobPack_Creation()
    Dim JType As String
    Dim FPath As String
    Dim Order_No As String
    Dim WorkbookName As String
    Dim NewDir As String
    CurrentPath = ActiveWorkbook.Path
    WorkbookName = ActiveWorkbook.Name
    Call A3PageTest
'Get file name and path details (estimate number and duct or cable)
    With ActiveWorkbook.Worksheets("Entry")
        
    Order_No = Range("G19").Value
    If Len(Order_No) < 4 Then
        Response = MsgBox("An 8 figure Order No is required ?" & Chr(13) & Chr(10) & _
            "Please enter an Order No and try again", vbCritical, _
            "               Order No required")
        Exit Sub
    Else
    End If
    Range("G4").Select
    Application.ScreenUpdating = True
    
    End With
    
    NewDir = CurrentPath & "\" & FPath & Range("G19").Value
    Call Check_Create_Dir(NewDir, Exchange & "_" & Order_No & "_" & Survey_no)
    Application.ScreenUpdating = False
    
    Call SaveEachTabs_as_a_SeperateWorksheet(NewDir, Order_No)
    Sheets("Entry").Select
    With ActiveSheet.PageSetup
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    Application.ScreenUpdating = True
    MsgBox "'Job Pack' files created successfully.", vbInformation + vbOKOnly, "All done."
    Application.ScreenUpdating = True
End Sub
Sub Check_Create_Dir(NewDir As String, NewFolder As String)
    ' If a Folder with the same name exists, create a No. 2
    If Not PathExists(NewDir) Then
    If Dir(NewDir, vbDirectory) <> NewFolder Then
    MkDir (NewDir)
    End If
    Else
    If Dir(NewDir, vbDirectory) <> NewFolder Then
    NewDir = NewDir & " - 2"
    
    On Error GoTo NotAllowed:
    MkDir (NewDir)
    On Error GoTo 0
    End If
    End If
    Exit Sub
    
NotAllowed:
    msg = "2 Folders with the same Order Number " & vbCrLf & _
    vbCrLf & "    already exist in this location ?"
    result = MsgBox(msg, vbCritical, "                 2 Folders Exist")
    If result = vbOK Then
End
    End If
End Sub
#If VBA6 Then
#Else
Function Replace(ByVal sExpression As String, _
        ByVal sFind As String, ByVal sReplace As String)
        Replace = Application.WorksheetFunction.Substitute(sExpression, sFind, sReplace)
End Function
#End If
Public Sub SaveEachTabs_as_a_SeperateWorksheet(SavePath As String, SaveName As String)
' Get Savepath & SaveName details
    Dim MasterWorkbook As String
    Dim WorksheetName As String
    
    MasterWorkbook = ActiveWorkbook.Name
    For Each ws In Worksheets ' Loop through every worksheet in the workbook
    ' Create the seperate files
    Select Case (ws.Name)
                                
        Case "Entry"
        Case "Data"
        Case Else
            'Pass the work sheet name, file path, and file name to the "SaveWorksheet_as_Workbook" procedure
        Call SaveWorksheet_as_Workbook(ws.Name, SavePath, SaveName)
        
    End Select
    Next ws
    
End Sub
Public Sub SaveWorksheet_as_Workbook(SheetName As String, SavePath As String, SaveName As String)
' Create seperate file for each w/s present - "SetPrint" instructs how to format Page Setup
    Dim NewWorkbookName As String
    Dim MasterWorkbook As String
    Dim P1Name As String
    WorkbookName = ActiveWorkbook.Name
    MasterWorkbook = WorkbookName
    Application.ScreenUpdating = False
    
    P1Name = SaveName & "_" & SheetName ' SheetName passed from calling procedure
    Workbooks.Add xlWorksheet
    Sheets("Sheet1").Name = P1Name
    ActiveWindow.DisplayGridlines = False
    Call SetPrint
    
    ActiveWorkbook.SaveAs FileName:=SavePath & "\" & P1Name
    SaveAs2003_Workbook (PathAndName)
    Application.ScreenUpdating = False
    Workbooks.Open FileName:=SavePath & "\" & P1Name & ".xls", UpdateLinks:=0
    Windows(WorkbookName).Activate
    
    Sheets(SheetName).Select
    Cells.Select
    Selection.Copy
    Windows(P1Name & ".xls").Activate
    Cells.Select
    ActiveSheet.Paste
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    ActiveWindow.Zoom = 50
    Range("A1").Select
    ActiveWorkbook.Save
    ActiveWorkbook.BreakLink Name:=SavePath & "\" & P1Name & ".xls", Type:= _
        xlExcelLinks
    ActiveWorkbook.Close
    Range("C3").Select
End Sub
Sub SetPrint()
        ActiveSheet.PageSetup.PrintArea = "$C$3:$AH$77"
        
    With ActiveSheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.2)
        .RightMargin = Application.InchesToPoints(0.2)
        .TopMargin = Application.InchesToPoints(0.2)
        .BottomMargin = Application.InchesToPoints(0.2)
        .PrintGridlines = False
        .CenterHorizontally = True
        .CenterVertically = True
        .Orientation = xlLandscape
        .PaperSize = xlPaperA4
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PrintErrors = xlPrintErrorsDisplayed
    End With
    
    ActiveWindow.DisplayHeadings = False
End Sub
Private Function PathExists(pname) As Boolean
    
    Dim x As String
    On Error Resume Next
    x = GetAttr(pname) And 0
    If Err = 0 Then PathExists = True _
    Else PathExists = False
End Function
Sub SaveAs2003_Workbook(PathAndName)
    'Adapted from code by Ron Debruin
    'All saves are sent here.
    'Avoid CheckCompatibility dialog when you copy a WorkSheet
    'from a 2007-2010 file with things that are new in 2007-2010
    'to a new workbook and save this workbook as a 97-2003 workbook
    Dim SaveFormat As Long
    'Remember the users setting
    SaveFormat = Application.DefaultSaveFormat
    'Set it to the 97-2003 file format
    Application.DefaultSaveFormat = -4143
    
    On Error Resume Next 'added by NJI as Ex2003 doesn't know the 'CheckCompatibility' command
    ActiveWorkbook.CheckCompatibility = False
    
    Application.DisplayAlerts = False
    
    
    PathAndName = Replace(PathAndName, Chr(10), "")
    PathAndName = Replace(PathAndName, Chr(13), "")
    PathAndName = Replace(PathAndName, "\\", "\", 1)
    PathAndName = Replace(PathAndName, ".xls", "", 1) 'Remove ".xls"
    
    'MsgBox PathAndName
    ActiveWorkbook.SaveAs PathAndName, FileFormat:=-4143
  
    'Set DefaultSaveFormat back to the users setting
    Application.DefaultSaveFormat = SaveFormat
    
    Application.DisplayAlerts = True
    
    MyChanges = False
    Exit Sub
Getout:
    
End Sub

Hope you can help as I'm just starting out in VB
 
Last edited:
Upvote 0
Yeesh. A lot going on there to sift through, so seeing as you say it works in 2003 and not 2007, try saving the file as a .xlsm extension without specifying that, but with the fileformat constant 52 instead.

Example:

Instead of this:
ActiveWorkbook.SaveAs PathAndName, FileFormat:=-4143


Try this:
ActiveWorkbook.SaveAs PathAndName, FileFormat:=52


Then try opening it with the .xlsm extension in the Open method, example:
Workbooks.Open FileName:=SavePath & "\" & P1Name & ".xlsm", UpdateLinks:=0
 
Upvote 0
Hi Tom,
Sorry it was a bit long, with so many different up-front options going on before the main event.

I don't know if this helps, but if I 'Rem the call to the "SaveAs2003_Workbook (PathAndName)" sub completely, and use XL2003 to create the new files, it still creates all the new worksheets without errors and opens OK in both XL2003 & 2007.

So it seems that the "SaveAs2003_Workbook (PathAndName)" sub is where the error is and where things need to change.

Is there anything you can insert that will change the newly created Sheets to a compatible ".xls" format?
i.e. Something that will work in both XL2003 or 2007, especiallly if the user is using XL2007 or above.

Perhaps using a catch - e.g. If Val(Application.Version) >= 12 Then .............. ' You are using a version of Excel above 97-2003

Thanks for your persistance.
 
Upvote 0
Remove all the unneeded code and give only the relevant code. It's too much to sift through. I can't see why your saveas 2003 wouldn't work but there's way too much going on - targeting multiple versions of Excel with conditional compilation options to boot just sounds like a headache. Perhaps consider keeping the original workbook in xls file format so you don't need all these conversions. And don't run this on 64 bit office systems. In other words, simplify so you don't have to keep track of so much.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,862
Members
452,948
Latest member
UsmanAli786

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