PDA

View Full Version : macro loop to save .xml extension to .xls.



datadc
11-05-2015, 10:52 AM
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

snb
11-05-2015, 02:02 PM
You need to add the fileformat.

Paul_Hossler
11-06-2015, 06:56 AM
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