Consulting

Results 1 to 3 of 3

Thread: macro loop to save .xml extension to .xls.

  1. #1

    macro loop to save .xml extension to .xls.

    Hello, I have the macro below to loop through files in a directory. I am trying to update it to save each file in the directory from .xml to .xls. however I hit a run time 1004 error "Method 'SaveAs' of object '_workbook' failed. I am on 2013.

    Sub savexmltoexcel()
    
    
    
    
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    
    
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    
    
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
    
      myExtension = "*.xml"
    
    
      myFile = Dir(myPath & myExtension)
    
    
      Do While myFile <> ""
        'Set variable equal to opened workbook
          Set wb = Workbooks.Open(Filename:=myPath & myFile)
          myNewExtension = "*.xls"
          myNewFile = Dir(myPath & myNewExtension)
          wb.SaveAs Filename:=myPath & myNewFile
          
          wb.Close SaveChanges:=True
    
    
          myFile = Dir
      Loop
    
    
    ResetSettings:
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    You need to add the fileformat.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    When I get stuck, I find it usually helps if I record a small macro with the record and see what Excel gives me. Usually it's enough to point me in the right (or at least better) direction

    Sub Macro1()
        ChDir "C:\Users\Daddy\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\Daddy\Desktop\Book1.xlsx", _
            FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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