Consulting

Results 1 to 5 of 5

Thread: Solved: xls to xlsm files.

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    55
    Location

    Solved: xls to xlsm files.

    This macro change xls-files to xlsm-files in one folder:

    Dim result As String
    Dim objFSO As Object
    
    Sub Start()
        result = BrowseForFolder
        Select Case result
            Case Is = False
                result = "an invalid folder!"
            Case Else
        End Select
        TrandformAllXLSFilesToXLSM
    End Sub
    
    Public Sub TrandformAllXLSFilesToXLSM()
    Dim myPath As String
    
    myPath = result & "\"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    ShowSubFolders objFSO.GetFolder(myPath)
    
    
    WorkFile = Dir(myPath & "*.xls")
    
    Do While WorkFile <> ""
    If Right(WorkFile, 4) <> "xlsm" Then
                Workbooks.Open filename:=myPath & WorkFile
                ActiveWorkbook.SaveAs filename:= _
                myPath & WorkFile & "m", FileFormat:= _
                xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
                ActiveWorkbook.Close
                Kill myPath & WorkFile
            End If
    WorkFile = Dir()
    Loop
    End Sub
    
    Sub ShowSubFolders(Folder)
    For Each SubFolder In Folder.SubFolders
        Set objFolder = objFSO.GetFolder(SubFolder.path)
        Set colFiles = objFolder.Files
        For Each objFile In colFiles
            If Right(objFile.Name, 4) <> "xlsm" Then
            Dim path As String
            Dim filename As String
            filename = objFile.Name
            path = SubFolder.path & "\" & objFile.Name
                Workbooks.Open filename:=path
                path = SubFolder.path & "\" & "test.xlsm"
                
                ActiveWorkbook.SaveAs filename:= _
                path, FileFormat:= _
                xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
                ActiveWorkbook.Close
                
                Kill SubFolder.path & "\" & objFile.Name
                'path is nog temp file
                Workbooks.Open filename:=path
                'path veranderen naar originele filename met extensie "xlsm"
                path = SubFolder.path & "\" & filename & "m"
                
                  ActiveWorkbook.SaveAs filename:= _
                path, FileFormat:=52, CreateBackup:=False
                'xlOpenXMLWorkbookMacroEnabled
                ActiveWorkbook.Close
            End If
        Next
        ShowSubFolders SubFolder
    Next
    End Sub
    
    Function BrowseForFolder(Optional OpenAt As Variant) As Variant
        Dim ShellApp As Object
        Set ShellApp = CreateObject("Shell.Application"). _
            BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
        On Error Resume Next
            BrowseForFolder = ShellApp.self.path
        On Error GoTo 0
        Set ShellApp = Nothing
        Select Case Mid(BrowseForFolder, 2, 1)
            Case Is = ":"
                If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
            Case Is = "\"
                If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
            Case Else
                GoTo Invalid
        End Select
        Exit Function
    Invalid:
        BrowseForFolder = False
    End Function
    Is it posible to also change in SUBFolders?
    Or where can I find it on the internet?

    Thanks in advance.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim result As String
    Dim objFSO As Object

    Sub Start()

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    result = BrowseForFolder

    Select Case result
    Case Is = False
    result = "an invalid folder!"
    Case Else
    End Select
    TrandformAllXLSFilesToXLSM (result)
    End Sub

    Public Sub TrandformAllXLSFilesToXLSM(FilePath As String)
    Dim mFile As Object
    Dim fldr As Object
    Dim sfldr As Object

    Set fldr = objFSO.getFolder(FilePath & "\")
    For Each mFile In fldr.Files

    If mFile.Type Like "*Excel*" And Right$(mFile.Name, 4) <> "xlsm" Then

    Workbooks.Open Filename:=mFile.Path
    ActiveWorkbook.SaveAs Filename:= _
    mFile.Path & "m", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

    ActiveWorkbook.Close
    Kill FilePath & mFile.Name
    End If
    Next mFile

    For Each sfldr In fldr.subfolders

    Call TrandformAllXLSFilesToXLSM(FilePath & "\" & sfldr.Name)
    Next sfldr

    End Sub

    Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
    Set ShellApp = Nothing
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
    If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
    If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
    GoTo Invalid
    End Select
    Exit Function
    Invalid:
    BrowseForFolder = False
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2006
    Posts
    55
    Location
    Thans for reply,

    Gives an Error 53 (can't find the file) - Kill FilePath & mFile.Name

    Work with xl2007.
    Last edited by Pinokkio; 02-07-2010 at 02:55 PM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The one bit I didn't test

    Change that line to

    [vba]

    Kill FilePath & Application.PathSeparator & mFile.Name
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Aug 2006
    Posts
    55
    Location
    Works great now, many thanks.

    P.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •