「excel vba powerpoint 閉じる」
といった検索キーワードでアクセスがありました。
Excel VBAからPowerPointファイルを閉じるには、どのようなコードを書けばいいのかを調べていらしたのでしょうか。
パワポファイルを閉じるサンプルマクロ
何らかのPowerPointファイルが1つ開かれている状態で、以下のマクロを実行してください。
On Error GoTo ErrHandl
With GetObject(Class:="PowerPoint.Application")
.ActivePresentation.Close
End With
Exit Sub
ErrHandl:
Select Case Err.Number
Case 429
MsgBox "PowerPointが起動していないようです。"
Case -2147188160
MsgBox "PowerPointファイルが開かれていないようです。"
Case Else
MsgBox Err.Description & vbCrLf & Err.Number
End Select
Err.Clear
End Sub
開かれていたパワポのファイルが閉じられます。
サンプルマクロで行っている処理
PowerPointファイルを閉じているのは、以下の部分です。
With GetObject(Class:="PowerPoint.Application") .ActivePresentation.Close
まず、VBAのGetObject関数の引数Classに文字列「PowerPoint.Application」を指定することで、
起動済のPowerPoint.Applicationオブジェクトへの参照を取得しています。
With GetObject(Class:="PowerPoint.Application")
上記のコードでPowerPoint.Applicationオブジェクトへの参照を取得した後は、実質的にPowerPoint VBAです。
拙著『いちばんやさしいPowerPoint VBAの教本』の、「Lesson 15 Presentationが持つメソッドについて学習しましょう」で、PowerPoint VBAのコード「ActivePresentation.Close」をご紹介しています。
PowerPoint.Presentation.Closeメソッドを、
Excel VBAから呼んでいるのが、上記マクロの
With GetObject(Class:="PowerPoint.Application") .ActivePresentation.Close
です。
拙著でも解説しているとおり、PowerPoint.Presentation.Closeメソッドはいきなりプレゼンテーションファイルと閉じますから、そのファイルを残す必要があるのであれば事前にSaveメソッドやSaveAsメソッドで保存してください。
最終更新日時:2025-04-27 10:14
Home » エクセルマクロ・Excel VBAの使い方 » Office連携 » Excel VBAでパワポファイルを閉じる