2

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

私の構文が間違っている場所はありますか?また、サイレントモードで同じことをすることは可能ですか? ワークブックを開かずに?

ありがとう !

4

1 に答える 1