LoginSignup
34
34

More than 5 years have passed since last update.

JScriptでOfficeドキュメントをPDFに

Posted at

はじめに

以前、JScriptでExcel操作(その2)で、ExcelファイルをPDFへ変換してみました。

WordとかPowerPointでも、いけるのではと思い作成してみました。

以外に複雑で難航しました。

Excel

「Workbook」クラスの「ExportAsFixedFormat」メソッドを利用します。

パラメータ

名前 説明 設定値
Type ファイルの種類
PDF(xlTypePDF:0)
XPS文章(xlTypeXPS:1)
0(PDF)
Filename 出力ファイルパス 元ファイルパス+「.pdf」
Quality 最適化
標準(xlQualityStandard:0)
最小サイズ(xlQualityMinimum:1)
0(標準)
IncludeDocProperties ドキュメントのプロパティを含める true
IgnorePrintAreas 印刷範囲を無視 false

Word

「Document」クラスの「ExportAsFixedFormat」メソッドを利用します。

パラメータ

名前 説明 設定値
OutputFileName 出力ファイルパス 元ファイルパス+「.pdf」
ExportFormat ファイルの種類
PDF(wdExportFormatPDF:17)
XPS文章(wdExportFormatXPS:18)
17(PDF)
OpenAfterExport 発行後にファイルを開く false(開かない)

PowerPoint

「Presentation」クラスの「SaveAs」メソッドを利用します。
(「ExportAsFixedFormat」メソッドがあるのですが、エラーが出てしまいます。
(Office2013には「ExportAsFixedFormat2」なんてメソッドもあります。)

パラメータ

名前 説明 設定値
FileName 出力ファイルパス 元ファイルパス+「.pdf」
FileFormat ファイルの種類
PDF(ppSaveAsPDF:32)
その他多数
32(PDF)

サンプルコード

office2pdf.js
// パラメータ取得
var args = WScript.arguments;
// 「FileSystemObject」オブジェクト生成
var fso = new ActiveXObject("Scripting.FileSystemObject");

// パラメータ存在チェック
if (args.length > 0) {
    // パラメータ数分ループ
    for (var i = 0; i < args.length; i++) {
        // パラメータ抽出(ドラッグ&ドロップしたファイルパス)
        var file = args.item(i);
        // ファイル存在チェック
        if (fso.FileExists(file)) {
            // 拡張子取得
            var ext = fso.GetExtensionName(file).toLowerCase();
            // 拡張子チェック
            if (ext.indexOf("xls") == 0) {
                // ExcelをPDFへ変換
                xls2pdf(file, file + ".pdf");
            } else if (ext.indexOf("doc") == 0) {
                // WordをPDF変換へ
                doc2pdf(file, file + ".pdf");
            } else if (ext.indexOf("ppt") == 0) {
                // PowerPointをPDFへ
                ppt2pdf(file, file + ".pdf");
            } else {
                // 拡張子が「xls*」でない場合
                WScript.Echo("" + file + "」の拡張子は対応していません。");
            }
        } else {
            // ファイルが存在しない場合
            //WScript.Echo("「" + file + "」は存在しません。");
        }
    }
} else {
    // パラメータが無い場合
    WScript.Echo("Excel、Word、PowerPointファイルをドラッグ&ドロップしてください。");
}

// 終了
WScript.Quit(0);

// ExcelをPDF出力
function xls2pdf(infile, outfile) {
    var excel = null;
    try {
        // Excelオブジェクト生成
        excel = new ActiveXObject("Excel.Application");
        var book = null;
        try {
            // 読み取り専用で開く
            book = excel.Workbooks.Open(infile, 0, true);
            // PDF出力
            book.ExportAsFixedFormat(0, outfile, 0, true, false);
        } catch (e) {
            // エラーの場合
            WScript.Echo("IN:" + infile + "\nOUT:" + outfile
                 + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
        } finally {
            // Workbookオブジェクト存在チェック
            if (book != null) {
                // Workbookを閉じる
                book.Close(false);
                book = null;
            }
        }
    } catch (e) {
        // エラーの場合
        WScript.Echo("IN:" + infile + "\nOUT:" + outfile
             + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
    } finally {
        if (excel != null) {
            excel.Quit();
            excel = null;
        }
    }
}

// WordをPDF出力
function doc2pdf(infile, outfile) {
    var word = null;
    try {
        // Wordオブジェクト生成
        word = new ActiveXObject("Word.Application");
        var doc = null;
        try {
            // 読み取り専用で開く
            doc = word.Documents.Open(infile, false, true);
            // PDF出力
            doc.ExportAsFixedFormat(outfile, 17, false);
        } catch (e) {
            // エラーの場合
            WScript.Echo("IN:" + infile + "\nOUT:" + outfile
                 + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
        } finally {
            // Workbookオブジェクト存在チェック
            if (doc != null) {
                // Workbookを閉じる
                doc.Close();
                doc = null;
            }
        }
    } catch (e) {
        // エラーの場合
        WScript.Echo("IN:" + infile + "\nOUT:" + outfile
             + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
    } finally {
        if (word != null) {
            word.Quit();
            word = null;
        }
    }
}

// PowerPointをPDF出力
function ppt2pdf(infile, outfile) {
    var ppt = null;
    try {
        // PowerPointオブジェクト生成
        ppt = new ActiveXObject("PowerPoint.Application");
        var pt = null;
        try {
            // 読み取り専用で開く
            pt = ppt.Presentations.Open(infile, -1);
            // PDF出力
            pt.SaveAs(outfile, 32);
        } catch (e) {
            // エラーの場合
            WScript.Echo("IN:" + infile + "\nOUT:" + outfile
                 + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
        } finally {
            // Presentationオブジェクト存在チェック
            if (pt != null) {
                // Presentationを閉じる
                pt.Close();
                pt = null;
            }
        }
    } catch (e) {
        // エラーの場合
        WScript.Echo("IN:" + infile + "\nOUT:" + outfile
             + "\nError(" + (e.number & 0xFFFF) + "):" + e.message);
    } finally {
        if (ppt != null) {
            ppt.Quit();
            ppt = null;
        }
    }
}
34
34
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
34
34