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 アドイン

    【2019年6月版】Excel カスタム関数(Excel Custom functions)の紹介

    1年半ほど前、Excel カスタム関数について記事を書きました。…

  2. Office関連

    Excel 2013で追加された「UNICHAR」関数を使って特殊文字を表示する。

    「Excel 2013で追加された「WEBSERVICE」関数を使って…

  3. Office関連

    「DQNネーム辞書」を更新しました。

    前のブログで公開していたIME 2010用の「DQNネーム辞書」を更新…

  4. Office関連

    ヘッドレス ChromeをSeleniumBasicで動かしてみました。

    Chromeがヘッドレスモードに対応した頃、Seleniumで操作した…

  5. Office関連

    Excel REST APIをPowerShellから呼び出す方法

    以前Excel REST APIをVBAから呼び出す方法を紹介しました…

  6. PowerShell

    winmail.datから添付ファイルを取り出すPowerShellコード

    Outlook以外のメールクライアントを使用しているのであれば、一度は…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP