【Excelマクロ】フォルダの画像ファイルをパワーポイントに順番に貼り付ける【chatGPTメモ】

フォルダにある画像ファイルを片っ端からパワポに貼り付けたいと思ったので、chatGPTに聞いてみた。今度試してみます。

 

Excelマクロを使用してPowerPointに画像を貼り付けるには、以下の手順に従ってください。

  1. Excelブックを開いて、PowerPointプレゼンテーションに画像を貼り付けるための新しいマクロを記述する新しいモジュールを作成します。

  2. VBAコード内で、PowerPointオブジェクトライブラリを参照する必要があります。参照を追加するには、VBAエディターで「ツール」> 「参照」をクリックして、Microsoft PowerPoint x.x Object Libraryを選択します(x.xはインストールされているPowerPointのバージョンに対応します)。

  3. 次に、指定されたフォルダからすべての画像ファイルを取得するための関数を作成します。上記の方法と同じ関数を使用できます。

  4. 次に、PowerPointプレゼンテーションを開き、新しいスライドを作成します。

' PowerPointプレゼンテーションを開く
Dim pptApp As PowerPoint.Application
Dim pptPresentation As PowerPoint.Presentation
Set pptApp = New PowerPoint.Application
Set pptPresentation = pptApp.Presentations.Open("C:\presentation.pptx")

' 新しいスライドを作成する
Dim pptSlide As PowerPoint.Slide
Set pptSlide = pptPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank)

' フォルダ内のすべての画像をスライドに貼り付ける
Sub InsertImages()
    ' 画像が含まれるフォルダのパス
    Dim folderPath As String
    folderPath = "C:\images"
    
    ' 画像ファイルのコレクションを取得する
    Dim files As Collection
    Set files = GetImageFiles(folderPath)
    
    ' 画像をスライドに挿入する
    Dim file As Variant
    For Each file In files
        Dim pptShape As PowerPoint.Shape
        Set pptShape = pptSlide.Shapes.AddPicture(file, msoFalse, msoTrue, 0, 0)
    Next
End Sub