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.

テスト用 Word, Excel, PowerPoint ファイルをまとめて作成

Posted at

動機

とあるテスト用に Word, Excel, PowerPoint のさまざまの拡張子のファイルをまとめて作成することが必要になりました。

コード

いろいろ調べたのが手間だっただけで、大したことはしていません。 (要は自分用のメモです、すみません。)

PowerShell
$dir = "$env:USERPROFILE\Documents\TestFiles"
if (!(Test-Path -Path $dir)) {
    New-Item -Path $dir -ItemType Directory
}
$basename = "Test$(Get-Date -Format 'yyyyMMdd')"
$text = 'Test'

Add-Type -AssemblyName Microsoft.Office.Interop.Excel

# https://docs.microsoft.com/ja-jp/office/vba/api/excel.xlfileformat
[Microsoft.Office.Interop.Excel.XlFileFormat[]] $xlFormats = @(
    'xlWorkbookNormal'               # .xls
    'xlExcel12'                      # .xlsb
    'xlTemplate'                     # .xlt
    'xlOpenXMLWorkbookMacroEnabled'  # .xlsm
    'xlWorkbookDefault'              # .xlsx
    'xlOpenXMLTemplateMacroEnabled'  # .xltm
    'xlOpenXMLTemplate'              # .xltx
)

$exel = New-Object -ComObject Excel.Application
$book = $exel.Workbooks.Add()
$sheet = $book.sheets(1)
$sheet.Cells.Item(1, 1).Value = $text
foreach ($f in $xlFormats) {
    $book.SaveAs("$dir\$basename", $f)
}
$book.Close()
$book = $null
$exel.Quit()
$exel = $null

Add-Type -AssemblyName Microsoft.Office.Interop.Word

# https://docs.microsoft.com/ja-jp/office/vba/api/word.wdsaveformat
[Microsoft.Office.Interop.Word.WdSaveFormat[]] $wdFormats = @(
    'wdFormatDocument'                 # .doc
    'wdFormatXMLDocumentMacroEnabled'  # .docm
    'wdFormatDocumentDefault'          # .docx
    'wdFormatTemplate'                 # .dot
    'wdFormatXMLTemplateMacroEnabled'  # .dotm
    'wdFormatXMLTemplate'              # .dotx
)

$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Add()
$doc.Content.Text = $text
foreach ($f in $wdFormats) {
    $doc.SaveAs("$dir\$basename", $f)
}
$doc.Close()
$doc = $null
$word.Quit()
$word = $null

Add-Type -AssemblyName Microsoft.Office.Interop.PowerPoint

# https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ff760025(v=office.14)?redirectedfrom=MSDN
$ppFormats = [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType[]] @(
    'ppSaveAsOpenXMLTemplateMacroEnabled'      # .potm
    'ppSaveAsOpenXMLTemplate'                  # .potx
    'ppSaveAsShow'                             # .pps
    'ppSaveAsOpenXMLShowMacroEnabled'          # .ppsm
    'ppSaveAsOpenXMLShow'                      # .ppsx
    'ppSaveAsPresentation'                     # .ppt
    'ppSaveAsOpenXMLPresentationMacroEnabled'  # .pptm
    'ppSaveAsOpenXMLPresentation'              # .pptx
)

$powerPoint = New-Object -ComObject PowerPoint.Application
# $powerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::MsoTrue
$presentation = $powerPoint.Presentations.Add()
$powerPoint.WindowState = [Microsoft.Office.Interop.PowerPoint.PpWindowState]::ppWindowMinimized
$slide = $presentation.Slides.Add(1, [Microsoft.Office.Interop.PowerPoint.PpSlideLayout]::ppLayoutTitle)
$slide.Shapes.Title.TextFrame.TextRange.Text = $text
foreach ($f in $ppFormats) {
    $presentation.SaveAs("$dir\$basename", $f)
}
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($slide)
$presentation.Close()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($presentation)
$powerPoint.Quit()
$powerPoint = $null
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?