Office関連

ドラッグ&ドロップしたOfficeファイルをPDFに変換するVBScript

複数のExcelファイルをPDFに一括変換する必要があったので、簡単なスクリプトを書いてみました。

Option Explicit

Dim fp
Dim fso
Dim args
Dim i

Const msoFalse = 0
Const msoTrue = -1
Const xlTypePDF = 0
Const xlQualityStandard = 0
Const wdExportFormatPDF = 17
Const wdExportOptimizeForPrint = 0
Const wdExportAllDocument = 0
Const wdExportDocumentContent = 0
Const wdExportCreateWordBookmarks = 2
Const wdDoNotSaveChanges = 0
Const ppSaveAsPDF = 32

Set fso = CreateObject("Scripting.FileSystemObject")
Set args = WScript.Arguments

If args.Count < 1 Then
  MsgBox "当スクリプトにファイルをドラッグ&ドロップして処理を実行してください。", vbExclamation + vbSystemModal
  WScript.Quit
End If

For i = 0 To args.Count - 1
  fp = fso.GetParentFolderName(args(i)) & ChrW(92) & fso.GetBaseName(args(i)) & ".pdf"
  Select Case LCase(fso.GetExtensionName(args(i)))
    Case "doc", "docx", "dotm" 'Wordファイル処理
      With CreateObject("Word.Application")
        .Visible = True
        With .Documents.Open(args(i))
          .ExportAsFixedFormat fp, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument, , , _
                                   wdExportDocumentContent, False, False, wdExportCreateWordBookmarks, True, True, False
          .Close wdDoNotSaveChanges
        End With
        .Quit
      End With
    Case "xls", "xlsx", "xlsm" 'Excelファイル処理
      With CreateObject("Excel.Application")
        .Visible = True
        With .Workbooks.Open(args(i))
          .ExportAsFixedFormat xlTypePDF, fp, xlQualityStandard, False, False, , , False
          .Close False
        End With
        .Quit
      End With
    Case "ppt", "pptx", "pptm" 'PowerPointファイル処理
      With CreateObject("PowerPoint.Application")
        .Visible = True
        With .Presentations.Open(args(i))
          .SaveAs fp, ppSaveAsPDF, msoTrue 'ExportAsFixedFormatはエラーになったためSaveAs使用
          .Close
        End With
        .Quit
      End With
  End Select
Next

MsgBox "処理が終了しました。", vbInformation + vbSystemModal

ついでにWordやPowerPointにも対応させたのですが、PowerPointの場合は、ExportAsFixedFormatメソッドを使おうとすると「型が一致しません」エラーが発生したため、SaveAsメソッドを使うことにしました。

アイカツ!オフィシャルショップ浅草ROX店に行ってきました。前のページ

アイカツフレンズ!はじめました。次のページ

関連記事

  1. Office アドイン

    Office用アプリではalertやconfirmが使えない?

    JavaScriptでメッセージや確認ダイアログを表示する際には「al…

  2. Office関連

    [Excel]別インスタンスからの貼り付け時のダイアログを非表示にする方法

    Msdn フォーラムに「excel 2010 貼り付けで警告メッセージ…

  3. Office関連

    Word 2013では簡単にウィンドウ ハンドルを取得できるようになりました。

    ※ この情報はOffice 2013 カスタマー プレビュー版を元にし…

  4. Office関連

    Office 365 unified APIをJavaScriptだけで呼び出す

    Microsoftの松崎さんのブログに下記の記事がありました。…

  5. Office アドイン

    Office アドインの概要と開発方法を学ぶための自習書

    2018年10月27日(土)、品川の日本マイクロソフト本社で「2018…

  6. Office関連

    文書が互換モードかどうかを判定するWordマクロ

    古いバージョンのWordで作成された文書を開くと、タイトル バーに「互…

コメント

  • コメント (0)

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

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

Time limit is exhausted. Please reload CAPTCHA.

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

Translate

最近の記事

アーカイブ

PAGE TOP