将工作簿保存为* .xml到子文件夹时出现Dir问题

我有一个小脚本允许我遍历当前文件夹中的所有xslx文件,并将它们全部保存为xml工作表。

这工作正常,但我想保存在一个子文件夹,这就是事情出错的地方,因为我总是再次保存相同的文件 。 我不太熟悉Dir语法,所以如果有人能帮我一下,我会很感激。

这部分按预期工作:

 Sub XLS2XML() Application.DisplayAlerts = False Dim folderPath As String Dim Report As String Dim ReportName As String Dim XMLLocation As String Dim XMLReport As String Dim WB As Workbook 'set path to current location folderPath = ThisWorkbook.Path If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 'loop through all xlsx files Report = Dir(folderPath & "*.xlsx") Do While Report <> "" Set WB = Workbooks.Open(folderPath & Report) 'get the file name without path ReportName = Split(Report, ".")(0) XMLLocation = folderPath XMLReport = XMLLocation & ReportName & ".xml" 'save the file as xml workbook ActiveWorkbook.SaveAs filename:=XMLReport, _ FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 'close and next WB.Close False Report = Dir Loop MsgBox "All XML files have been created" Application.DisplayAlerts = True End Sub 

而这一个失败了我:

 Sub XLS2XML() Application.DisplayAlerts = False Dim folderPath As String Dim Report As String Dim ReportName As String Dim XMLLocation As String Dim XMLReport As String Dim WB As Workbook 'set path to current location folderPath = ThisWorkbook.Path If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 'loop through all xlsx files Report = Dir(folderPath & "*.xlsx") Do While Report <> "" Set WB = Workbooks.Open(folderPath & Report) 'get the file name without path and save it in xml folder ReportName = Split(Report, ".")(0) XMLLocation = folderPath & "xml" XMLReport = XMLLocation & "\" & ReportName & ".xml" 'create xml folder if it doesn't exist yet If Len(Dir(XMLLocation, vbDirectory)) = 0 Then MkDir XMLLocation End If 'save the file as xml workbook ActiveWorkbook.SaveAs filename:=XMLReport, _ FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 'close and next WB.Close False Report = Dir Loop 

任何想法,我的语法出错? 另外,是否可以在静音模式下做同样的事情? 所以不打开工作簿?

谢谢 !

你的问题是你正在使用你的初始Dir循环内的第二个Dir来testing和创buildxml子目录。

你可以 – 而且应该把它移到循环之外 – 尤其是因为它是一次性testing,不应该循环开始。 下面是这样的

(你用Dir很好,根据我的简单的通配符代码示例循环使用VBA文件夹中的文件? )

 Sub XLS2XML() Application.DisplayAlerts = False Dim folderPath As String Dim Report As String Dim ReportName As String Dim XMLlocation As String Dim XMLReport As String Dim WB As Workbook 'set path to current location folderPath = ThisWorkbook.Path XMLlocation = folderPath & "xml" If Len(Dir(XMLlocation, vbDirectory)) = 0 Then MkDir XMLlocation If Right$(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 'loop through all xlsx files Report = Dir(folderPath & "*.xlsx") Do While Len(Report) > 0 Set WB = Workbooks.Open(folderPath & Report) 'get the file name without path and save it in xml folder ReportName = Split(Report, ".")(0) XMLReport = XMLlocation & "\" & ReportName & ".xml" 'save the file as xml workbook WB.SaveAs Filename:=XMLReport, _ FileFormat:=xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False 'close and next WB.Close False Report = Dir Loop End Sub