Excel 2007 saving file to folder (code included)

Slomaro2000

Board Regular
Joined
Jun 4, 2008
Messages
107
Hi all,
Im using the code below to open the .XLS files in a folder and save them as .XLSX files with the same name as they are in the same folder.

This is the macro I am using. Not sure why this is not working. If I try to put them on the C: in a folder I can and it works fine but when I try to save to the server its not working.



A little bit on why Im doing this. Business Objects sends a file (XLS) to a specified folder. I need these files to be (XLSX). So all I am trying to do is convert the XLS files in this folder to XLSX. If there is a easier way please let me know.

Code:
Public Function IsFileOpen(strFileName As String) As Boolean
 
    On Error Resume Next 'Ignore any errors (i.e. if workbook is not open)
 
        Set wrkFileName = Workbooks(strFileName)
 
            If wrkFileName Is Nothing Then
                IsFileOpen = False
            Else
                IsFileOpen = True
            End If
 
    On Error GoTo 0 'Nullify above error handler
 
End Function
Sub Macro1()
    Dim strDir As String, _
        strFileType As String
    Dim objFSO As Object, _
        objFolder As Object, _
        objFile As Object
    Dim intCounter As Integer
 
 
    strDir = "[URL="file://\\Mdnt15\maint_odms\Data\Reliability"]\\Mdnt15\maint_odms\Data\Reliability[/URL] Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final" 'Change to suit
    'strDir = "C:\Users\U369875\Desktop\jaysonTest" 'Change to suit
    strFileType = "XLS" 'Shouldn't need to, but change to suit if required
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(strDir)
 
    Application.ScreenUpdating = False
 
    For Each objFile In objFolder.Files
        'If the file in the 'strDir' directory is not this workbook, then...
        If objFile.Name <> ThisWorkbook.Name Then
            If objFile.Name Like "*." & strFileType Then
                '...check to see if it's open.  If it is...
                If IsFileOpen(objFile.Name) = True Then
                    '...run the 'MyMacro' passing the active workbook variable with it and _
                    increment the counter.
                     Call MyMacro(objFile.Name)
                     intCounter = intCounter + 1
                'Else, _
                1. Open the file, _
                2. Run the 'MyMacro' passing the active workbook variable with it, _
                3. Save the changes and close the file, and _
                4. Increment the counter.
                Else
                     Workbooks.Open (strDir & "\" & objFile.Name), UpdateLinks:=False
                     Call MyMacro(objFile.Name)
                     'Workbooks(objFile.Name).Close SaveChanges:=True
                     intCounter = intCounter + 1
                End If
            End If
        End If
        'Release memory
        Set objFSO = Nothing
        Set objFolder = Nothing
        Set objFile = Nothing
    Next objFile
 
    Application.ScreenUpdating = True
 
    Select Case intCounter
        Case Is = 0
            MsgBox "There were no """ & strFileType & """ file types in the """ & strDir & """ directory for the desired macro to be run on.", vbExclamation, "Data Execution Editor"
        Case Is = 1
            MsgBox "The desired macro has been run on the only """ & strFileType & """ file in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
        Case Is > 1
            MsgBox "The desired macro has now been run on the " & intCounter & " files in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
    End Select
 
End Sub
Sub MyMacro(strDesiredWkb As String)
'ActiveWorkbook.SaveAs , strDir, FileFormat:=51
 
Application.DisplayAlerts = False
 
ChDir "[URL="file://\\Mdnt15\maint_odms\Data\Reliability"]\\Mdnt15\maint_odms\Data\Reliability[/URL] Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final\testing_Jayson"
    ActiveWorkbook.SaveAs , FileFormat:=51
ActiveWorkbook.Close
 
Application.DisplayAlerts = True
 
End Sub


The problem must be here
Code:
Sub MyMacro(strDesiredWkb As String)
'ActiveWorkbook.SaveAs , strDir, FileFormat:=51
 
Application.DisplayAlerts = False
 
ChDir "[URL="file://mdnt15/maint_odms/Data/Reliability"]\\Mdnt15\maint_odms\Data\Reliability[/URL] Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final\testing_Jayson"
    ActiveWorkbook.SaveAs , FileFormat:=51
ActiveWorkbook.Close
 
Application.DisplayAlerts = True
 
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
ChDir doesn't change the drive path.

From VBA help on ChDir...
Remarks

The ChDir statement changes the default directory but not the default drive. For example, if the default drive is C, the following statement changes the default directory on drive D, but C remains the default drive:

ChDir "D:\TMP"

Try using the ChDrive function before ChDir.

Or just put the full path & filename in the .SaveAs
Code:
ActiveWorkbook.SaveAs "\\Mdnt15\maint_odms\Data\Reliability Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final\testing_Jayson\" & ActiveWorkbook.Name, FileFormat:=51
 
Last edited:
Upvote 0
AlfaFrog

I added
Code:
ActiveWorkbook.SaveAs "\\Mdnt15\maint_odms\Data\Reliability Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final\testing_Jayson\" & ActiveWorkbook.Name, FileFormat:=51


</PRE><!-- / message --><!-- sig -->

It did save in the correct place but did not save as .XLSX

When I go to open the file it gives the error
The file you are trying to open"xxx.xls", is in a different format than specified by the extension. Verify that the file is not corrupted and is from a trusted source before opening the file.
 
Upvote 0
Try this...

Code:
ActiveWorkbook.SaveAs "\\Mdnt15\maint_odms\Data\Reliability Tool\WO Detail Spending Report_Access\BO Reports that feed Reliability Final\testing_Jayson\" & ActiveWorkbook.Name [COLOR="Red"]& "x"[/COLOR], FileFormat:=51
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

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