チャットGPTが示したコード(2025/12/25)
次の行以降をパワポのモジュールへコピーする。
コードを貼り付けると、コピペでコード不良? ドキュメントを埋め込みました。(2026/1/19)
Option Explicit
'==================================================
' ノートの文章を読み上げて WAV を作成し
' 各スライドに音声として埋め込む
'==================================================
Sub NotesToSpeech_EmbedAudio()
Const SSFMCreateForWrite As Long = 3 ' SAPI 定数
Dim sld As Slide
Dim notesText As String
Dim voice As Object
Dim stream As Object
Dim wavPath As String
Dim wavName As String
Dim i As Long
' プレゼンが未保存だと Path が空になるためチェック
If ActivePresentation.Path = "" Then
MsgBox "先にこの PowerPoint を保存してください。", vbExclamation
Exit Sub
End If
wavPath = ActivePresentation.Path & "\"
Set voice = CreateObject("SAPI.SpVoice")
voice.Rate = 0 ' 話速(-10 ~ +10)
voice.Volume = 100 ' 音量(0 ~ 100)
i = 1
For Each sld In ActivePresentation.Slides
' ノートのテキスト取得
notesText = ""
On Error Resume Next
notesText = sld.NotesPage.Shapes.Placeholders(2) _
.TextFrame.TextRange.Text
On Error GoTo 0
If Trim(notesText) <> "" Then
wavName = wavPath & "Slide_" & Format(i, "00") & ".wav"
Set stream = CreateObject("SAPI.SpFileStream")
stream.Open wavName, SSFMCreateForWrite, False
Set voice.AudioOutputStream = stream
voice.Speak notesText
stream.Close
Set voice.AudioOutputStream = Nothing
Set stream = Nothing
' スライドに音声を埋め込み
With sld.Shapes.AddMediaObject2( _
FileName:=wavName, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue)
' 自動再生・非表示
.AnimationSettings.PlaySettings.PlayOnEntry = True
.AnimationSettings.PlaySettings.HideWhileNotPlaying = True
End With
End If
i = i + 1
Next sld
MsgBox "ノートの読み上げ音声を埋め込みました。", vbInformation
End Sub
これでマクロが登録されるので、ポイントして実行すれば、各スライドにノートを読み上げた音声ファイルが埋め込まれる。
同時にパワポ.pptmを保存したフォルダにも音声ファイルが保存される。