LoginSignup
1
0

More than 5 years have passed since last update.

PowerPointの総ページ数をPowerShellで更新(課題残り)

Last updated at Posted at 2018-08-31

はじめに

弊社のPowerPointのフォーマットには総ページ数の項目があるのですが、
ここが更新されていないばっかりに資料が通らないことが何度もありました。
(中身を見てくれ…)
具体的には<#>/000というテキストボックスがスライドマスターに設定されています。
ここの000を総ページ数に手動で更新する必要があるのですが、手間です。
ポチッと更新せんと組み始めたのですが、引っかかっております。

参考資料

PowerPoint VBA リファレンス
前回のExcelと違って妙にしっくりこないことが散見される。

環境

  • Windows10 Pro Ver.1703
  • PowerPoint 2016
  • PowerShell

ソースコード

PageCount.ps1
#PowershellでPowerPointの総ページ数を更新
#未解決問題2点あり

#ダイアログを使えるようにする
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.Filter = "pptファイル(*.ppt;*.pptx)|*.PPT;*.PPTX"
$dialog.InitialDirectory = "C:\Users\1501275\Desktop\kojin"
$dialog.Title = "ファイルを選択"
if($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK){
    $filepath = $dialog.FileName
    #なにかと便利なのでつい用意してしまう
    $folder = Split-Path -parent $filepath
    $filename = [System.IO.Path]::GetFileNameWithoutExtension($filepath)
    $extension =[System.IO.Path]::GetExtension($filepath)
} else {
    Read-Host "キャンセルされました。ENTERキーを押してください。"
    return    
}

#PowerPoint側の作業
$pp = New-Object -ComObject PowerPoint.Application
$pp.Visible = "msoTrue" #可視化
$pp.DisplayAlerts = "ppAlertsAll" #アラート類も可視化
$slide = $pp.presentations.Open($filepath)

#変数定義
$variable = "‹#›"
$pagecount = $slide.Slides.Count

#実行部分
foreach ($shape in $slide.slidemaster.shapes){ #スライドマスター内の各オブジェクトについて
    if($shape.HasTextFrame){ #そのオブジェクトがテキストを持っていれば
        $text = $shape.TextFrame2.TextRange.Text #テキストの内容を取得し
        $pos_variable = $text.indexof($variable) #テキスト内のvariableの位置を取得し
        if($pos_variable -ge 0){ #variableが存在すれば対象の図形と判断し
            $shape.TextFrame2.textrange.replace("000",$pagecount)
        }
    }
}
$pp.ActivePresentation.SaveAs($folder + "\" + $filename +"_更新" +$extension)

#末端処理
$slide.Close()
$pp.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($pp)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($slide)

出力

Before
2-000.png
After
キャプチャ.PNG

やったぜ。

あとなぜかスライドは閉じることができるんだけどPowerPoint自体が終了できない。

目下調査中です。

1
0
2

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
1
0