Sub ExtractDescriptionsFromYXMCFiles()
Dim folderPath As String
Dim xmlDoc As Object
Dim xmlFile As Object
Dim batchMacroNodes As Object
Dim controlParamsNode As Object
Dim controlParamNodes As Object
Dim controlParamNode As Object
Dim descriptionNode As Object
Dim fileName As String
Dim description As String
Dim i As Long
Dim j As Long
Dim outputRow As Long
Dim ws As Worksheet
' 出力先ワークシートを設定(新しいシートを作成)
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "Description抽出結果"
' ヘッダーを設定
ws.Cells(1, 1).Value = "ファイル名"
ws.Cells(1, 2).Value = "Description"
outputRow = 2
' フォルダパスを指定(ユーザーに選択させる)
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "YXMCファイルが格納されているフォルダを選択してください"
If .Show = -1 Then
folderPath = .SelectedItems(1)
Else
MsgBox "フォルダが選択されませんでした。処理を終了します。"
Exit Sub
End If
End With
' フォルダパスの最後に\を追加(必要に応じて)
If Right(folderPath, 1) <> "\" Then
folderPath = folderPath & "\"
End If
' XMLDOMオブジェクトを作成
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.async = False
' フォルダ内のYXMCファイルを処理
fileName = Dir(folderPath & "*.yxmc")
Do While fileName <> ""
' YXMCファイルを読み込み(XMLとして処理)
If xmlDoc.Load(folderPath & fileName) Then
' BatchMacroノードを取得
Set batchMacroNodes = xmlDoc.SelectNodes("//BatchMacro")
' BatchMacroノードが存在する場合
For i = 0 To batchMacroNodes.Length - 1
' ControlParamsノードを取得
Set controlParamsNode = batchMacroNodes(i).SelectSingleNode("ControlParams")
If Not controlParamsNode Is Nothing Then
' ControlParamノードを全て取得
Set controlParamNodes = controlParamsNode.SelectNodes("ControlParam")
' 各ControlParamノードをループ
For j = 0 To controlParamNodes.Length - 1
Set controlParamNode = controlParamNodes(j)
' Descriptionノードを取得
Set descriptionNode = controlParamNode.SelectSingleNode("Description")
If Not descriptionNode Is Nothing Then
description = descriptionNode.Text
Else
description = "(Descriptionが見つかりません)"
End If
' 結果をワークシートに出力
ws.Cells(outputRow, 1).Value = fileName
ws.Cells(outputRow, 2).Value = description
outputRow = outputRow + 1
Next j
End If
Next i
Else
' YXMCの読み込みに失敗した場合
ws.Cells(outputRow, 1).Value = fileName
ws.Cells(outputRow, 2).Value = "(YXMCの読み込みに失敗しました)"
outputRow = outputRow + 1
End If
' 次のYXMCファイルを取得
fileName = Dir()
Loop
' 列幅を自動調整
ws.Columns("A:B").AutoFit
' 完了メッセージ
If outputRow = 2 Then
MsgBox "指定フォルダにYXMCファイルが見つからないか、BatchMacro/ControlParams/ControlParam/Descriptionの構造を持つファイルがありませんでした。"
Else
MsgBox "処理が完了しました。" & (outputRow - 2) & "件のDescriptionを抽出しました。"
End If
' オブジェクトの解放
Set xmlDoc = Nothing
Set batchMacroNodes = Nothing
Set controlParamsNode = Nothing
Set controlParamNodes = Nothing
Set controlParamNode = Nothing
Set descriptionNode = Nothing
Set ws = Nothing
End Sub
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme