Office関連

PowerShellからNetOfficeを使ってWordやPowerPointを操作する方法

前回の記事関連で、

なんてことをツイートしたので、NetOfficeでWordやPowerPointを操作するコードも載せておきます。

NetOfficeでWordを操作するコード

Wordで新規文書を作成して、文頭に日付と時刻を挿入するコードです。

※ 要NetOffice.Word

[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\NetOffice.dll")
[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\OfficeApi.dll")
[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\WordApi.dll")
[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\VBIDEApi.dll")
$app = New-Object NetOffice.WordApi.Application
$app.Visible = $true
$doc = $app.Documents.Add()
$rng = $doc.Range(0,0)
$rng.InsertAfter((Get-Date).ToString("yyyy/MM/dd HH:mm:ss"))
$rng.Bold = $true
$rng.Font.ColorIndex = 5
$doc.SaveAs("C:\Test\Sample.docx")
$doc.Close()
$app.Quit()
$app.Dispose()

NetOfficeでPowerPointを操作するコード

PowerPointで新規プレゼンテーションを作成して、スライドを追加、追加したスライドにテキストボックスを挿入するコードです。

※ 要NetOffice.PowerPoint

[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\NetOffice.dll")
[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\OfficeApi.dll")
[void][Reflection.Assembly]::LoadFile("C:\System\NetOffice\PowerPointApi.dll")
$app = New-Object NetOffice.PowerPointApi.Application
$app.Visible = [NetOffice.OfficeApi.Enums.MsoTriState]::msoTrue
$pres = $app.Presentations.Add()
$sld = $pres.Slides.Add(1, [NetOffice.PowerPointApi.Enums.PpSlideLayout]::ppLayoutBlank)
$tbox = $sld.Shapes.AddTextbox([NetOffice.OfficeApi.Enums.MsoTextOrientation]::msoTextOrientationHorizontal, 100, 100, 300, 200)
$tbox.TextFrame2.TextRange.Text = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
$tbox.TextFrame2.TextRange.Font.Size = 24
$pres.SaveAs("C:\Test\Sample.pptx")
$app.Quit()
$app.Dispose()

PowerShellからNetOfficeを使ってExcelを操作する方法前のページ

PowerShellからリボンUIを呼ぶ方法次のページ

関連記事

  1. Office アドイン

    [Officeアドイン]ワークシートで選択範囲を変更したときに発生するイベント

    ワークシート上で選択範囲の変更を検知する際、VBAでは通常「Works…

  2. Office関連

    「カレンダーから日付入力」をUserFormに移植してみました。

    前回の記事では、Office 用アプリ「カレンダーから日付入力」と同様…

  3. Office関連

    「ちゃうちゃう!」で2つの文書を比較するWordマクロ

    2014/08/10 追記:ちゃうちゃう!がバージョンアップされま…

  4. Office関連

    「図のリセット」を実行するExcelマクロ

    Msdn フォーラムに「Excel2010-VBA 画像「図の書式設定…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP