Private Sub CommandButton3_Click() 'PPT削除
Const CELL_FILE_PATH As String = "B3"
' ファイルパス取得
Dim st As String '変数を宣言する
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = True Then
Range(CELL_FILE_PATH).Value = .SelectedItems(1)
PPT削除して保存
Else
MsgBox "キャンセル実行"
End If
End With
End Sub
Sub PPT削除して保存()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim strArray() As String
Dim file_count As Long
Dim path As String
Dim PPT_app As Object
Dim pptx_file As String
Dim i As Long
path = Sheets("Excel").Range("B3").Value
file_count = 0
Call get_files(path, FSO, file_count, strArray)
If file_count = 0 Then
MsgBox "pptファイルはありません"
Exit Sub
End If
Set PPT_app = CreateObject("PowerPoint.Application")
PPT_app.Visible = True
For i = 0 To UBound(strArray)
With PPT_app.Presentations.Open(strArray(i))
pptx_file = strArray(i) & "x"
.SaveAs Filename:=pptx_file
.Close
End With
Kill strArray(i) '元ファイル削除
Next
MsgBox "PPT変換し元ファイルは削除しています"
PPT_app.Quit
End Sub
'拡張子が.pptのファイル名をサブフォルダも含めて取得する
Private Sub get_files(ByVal path As String, FSO As Object, file_count As Long, strArray() As String)
Dim trgfolder As Object
Dim myfiles As Object
Dim myfile As Object
Dim subfolders As Object
Dim subfolder As Object
Set trgfolder = FSO.getfolder(path)
Set myfiles = trgfolder.files 'ファイル一覧取得
For Each myfile In myfiles '各ファイルを処理
'拡張子が.pptのファイルを取得
If LCase(Right(myfile.Name, 4)) = ".ppt" Then
ReDim Preserve strArray(file_count)
strArray(file_count) = path & "\" & myfile.Name
file_count = file_count + 1
End If
Next
Set subfolders = trgfolder.subfolders 'フォルダ一覧取得
For Each subfolder In subfolders '各サブフォルダを処理
'サブフォルダ下のファイル名を取得
Call get_files(path & "\" & subfolder.Name, FSO, file_count, strArray)
Next
End Sub