エクセルマクロ
エクセルマクロ
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
' 出力先フォルダの作成
outputPath = folderPath & "\text_extract_result"
If Not FileSystem.Dir(outputPath, vbDirectory) <> "" Then
MkDir outputPath
End If
' 指定されたパス内のファイルを検索し、変換
Call ProcessFilesInFolder(folderPath, "", outputPath)
End Sub
Sub ProcessFilesInFolder(folderPath As String, prefix As String, outputPath As String)
Dim fileName As String
fileName = Dir(folderPath & "*.*")
Do While fileName <> ""
If Right(fileName, 5) = ".xlsm" Or Right(fileName, 4) = ".xls" Or Right(fileName, 4) = ".log" Or Right(fileName, 4) = ".xml" Then
Call ConvertFileToText(folderPath, fileName, prefix, outputPath)
End If
fileName = Dir()
Loop
Dim subFolderName As String
subFolderName = Dir(folderPath & "\", vbDirectory)
Do While subFolderName <> ""
If subFolderName <> "." And subFolderName <> ".." Then
If (GetAttr(folderPath & "\" & subFolderName) And vbDirectory) = vbDirectory Then
Call ProcessFilesInFolder(folderPath & "\" & subFolderName, prefix & subFolderName & "_", outputPath)
End If
End If
subFolderName = Dir()
Loop
End Sub
Sub ConvertFileToText(folderPath As String, fileName As String, prefix As String, outputPath As String)
If Right(fileName, 4) = ".xls" Or Right(fileName, 5) = ".xlsm" Then
Call ConvertExcelToText(folderPath & "" & fileName, prefix, outputPath)
Else
FileCopy folderPath & "" & fileName, outputPath & "" & prefix & fileName & ".txt"
End If
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet
Set wb = Workbooks.Open(filePath)
For Each ws In wb.Sheets
ws.SaveAs Filename:=outputPath & "\" & prefix & Replace(Replace(filePath, wb.Path & "\", ""), ".xls", "_" & ws.Name & ".txt"), FileFormat:=xlText
Next ws
wb.Close False
End Sub
Discussion
Sub ProcessFilesInFolder(folderPath As String, prefix As String, outputPath As String)
Dim fso As Object, folder As Object, subfolder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
'''
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, outputPath As String)
Dim fso As Object, folder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet
Set wb = Workbooks.Open(filePath)
End Sub
'''
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, prefix As String, outputPath As String)
Dim fso As Object, folder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, prefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertExcelToText(filePath As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, outputPath As String)
Dim fso As Object, folder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, folderPrefix As String, outputPath As String, rootPath As String)
Dim fso As Object, folder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, folderPrefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, folderPrefix As String, outputPath As String)
Dim fso As Object, folder As Object, file As Object, subfolder As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, folderPrefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください (/target)"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub ProcessFiles(folderPath As String, folderPrefix As String, outputPath As String, rootPath As String)
Dim fso As Object, folder As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
End Sub
Sub ConvertExcelToText(filePath As String, folderPrefix As String, outputPath As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String, baseFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub WriteFileListToText()
Dim folderPath As String, outputPath As String, txtFilePath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub WriteFilesToFile(folderPath As String, txtFilePath As String)
Dim fso As Object, folder As Object, file As Object, subfolder As Object
Dim txtStream As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
Set txtStream = fso.CreateTextFile(txtFilePath, True)
End Sub
Sub WriteFilesToSubFolder(subfolder As Object, txtStream As Object)
Dim file As Object
For Each file In subfolder.Files
txtStream.WriteLine file.Path
Next file
For Each subfolder In subfolder.SubFolders
WriteFilesToSubFolder subfolder, txtStream
Next subfolder
End Sub
Sub WriteFileListToText()
Dim folderPath As String, txtFilePath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub WriteFilesToFile(folderPath As String, txtFilePath As String, rootPath As String)
Dim fso As Object, folder As Object, txtStream As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
Set txtStream = fso.CreateTextFile(txtFilePath, True)
End Sub
Sub WriteFilesToSubFolder(folder As Object, txtStream As Object, rootPath As String)
Dim file As Object, subfolder As Object
For Each file In folder.Files
txtStream.WriteLine Replace(file.Path, rootPath & "", "")
Next file
For Each subfolder In folder.SubFolders
WriteFilesToSubFolder subfolder, txtStream, rootPath
Next subfolder
End Sub
Sub ConvertFilesToText()
Dim folderPath As String, outputPath As String, txtFilePath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "フォルダを選択してください"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
End Sub
Sub WriteFilesToFile(folderPath As String, txtFilePath As String, rootPath As String)
Dim fso As Object, folder As Object, txtStream As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folderPath)
Set txtStream = fso.CreateTextFile(txtFilePath, True)
End Sub
Sub WriteFilesToSubFolder(folder As Object, txtStream As Object, rootPath As String)
Dim file As Object, subfolder As Object
For Each file In folder.Files
txtStream.WriteLine Replace(file.Path, rootPath & "", "")
Next file
For Each subfolder In folder.SubFolders
WriteFilesToSubFolder subfolder, txtStream, rootPath
Next subfolder
End Sub
Sub ProcessFilesFromList(txtFilePath As String, rootPath As String, outputPath As String)
Dim fso As Object, txtStream As Object, line As String, fullFilePath As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtStream = fso.OpenTextFile(txtFilePath, ForReading)
End Sub
Sub ConvertExcelToText(filePath As String, outputPath As String, baseFileName As String)
Dim wb As Workbook, ws As Worksheet, txtFileName As String
Set wb = Workbooks.Open(filePath)
End Sub
Sub ProcessFilesFromList(txtFilePath As String, rootPath As String, outputPath As String)
Dim fso As Object, txtStream As Object, line As String, fullFilePath As String
Dim baseFileName As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtStream = fso.OpenTextFile(txtFilePath, ForReading)
End Sub
Sub ProcessFilesFromList(txtFilePath As String, rootPath As String, outputPath As String)
Const ForReading = 1
Dim fso As Object, txtStream As Object, line As String, fullFilePath As String
Dim baseFileName As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtStream = fso.OpenTextFile(txtFilePath, ForReading)
End Sub