0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerPoint】pdfとして保存する【VBA】

Last updated at Posted at 2022-02-25

これのppt版

Call ppShape非表示にする("shp余白", False)

これは↓のものがある前提なので、ない時はこの行をコメントアウトして下さい。

Option Explicit

Sub PDF()
  
  On Error GoTo Err
  With ActivePresentation
    Dim ppPath: ppPath = .Path
    Dim ppName: ppName = .Name
    
    
    Dim pdfPath
    pdfPath = ppPath & "\" & 拡張子変換(ppName, "pdf")
    
    Do Until ファイル存在確認(pdfPath) = False
      pdfPath = 追番付与(pdfPath)
    Loop
    
    'pdfタブに表示されるppt詳細プロパティの値をファイル名に変更する
    .BuiltInDocumentProperties.Item(1).Value = 拡張子変換(ppName, "pdf")
    
    Call ppShape非表示にする("shp余白", False)
    
    .SaveAs _
      FileName:=pdfPath, _
      FileFormat:=ppSaveAsPDF
      
'      https://docs.microsoft.com/ja-jp/office/vba/api/powerpoint.presentation.exportasfixedformat
'    こっちの方法でやると詳細プロパティ問題は起こらないが画像がぼやけることがある
'    .ExportAsFixedFormat _
'      Path:=pdfPath, _
'      FixedFormatType:=ppFixedFormatTypePDF, _
'      PrintHiddenSlides:=msoFalse

  End With
  
  Call ppShape非表示にする("shp余白", True)
  
  ' pdf保存する時に付随して開くオプションがわからないのでWSで開く
  Call FileOpen_WScriptShell(pdfPath)
Err:
  Select Case Err.Number
    Case Is = 1004: MsgBox "プレゼンテーションを保存後実行してください。"
  End Select
End Sub

Private Function 追番付与(TargetPath)
  Dim buf1, ub, buf2
  buf1 = Split(TargetPath, ".")
  ub = UBound(buf1)
  
  buf2 = buf1(ub - 1)
  If InStr(buf2, "▲") = 0 Then   'すでに▲があるか否かで分岐
    buf2 = buf2 & "▲1"           'なければ▲1を付与
  Else
    buf2 = Split(buf2, "▲")      'あるなら▲までで区切ってその後をインクリメントする
    buf2(1) = buf2(1) + 1
    buf2 = Join(buf2, "▲")       '分割したものを戻す
  End If
  
  buf1(ub - 1) = buf2             '拡張子以前を元の形にまとめ直す
  追番付与 = Join(buf1, ".")      '分割したものを戻す
End Function

Private Function 拡張子変換(Target, 指定拡張子)
  Dim buf, ub
  buf = Split(Target, ".")
  ub = UBound(buf)
  buf(ub) = 指定拡張子
  拡張子変換 = Join(buf, ".")
End Function

Private Function ファイル存在確認(TargetPath) As Boolean
  Dim flg As Boolean
  If Dir(TargetPath) <> "" Then
    flg = True
  Else
    flg = False
  End If
  ファイル存在確認 = flg
End Function

Sub FileOpen_WScriptShell(ByVal strFile As String)
    Dim wsh As Object
    Set wsh = CreateObject("Wscript.Shell")
    strFile = """" & strFile & """"
    wsh.Run strFile, 1
    Set wsh = Nothing
End Sub



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?