Convert Office docs to PDF

This script converts Word and PowerPoint files to PDF-Documents. It uses Office's built-in SaveAs functionality and therefore requires an Office installation. The new files will sit next to the originals with the new .pdf extension. Existing files will not be overwritten. The applications may remain open after the script has finished and need then to be closed manually.

// https://resource.dopus.com/t/convert-office-docs-to-pdf/41531

// 2022-06-20

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    cmd.deselect = false;

    cmd.RunCommand('Set UTILITY=otherlog');

    DOpus.ClearOutput();

    DOpus.Output('Launching apps...\n');
    var wordApp = new ActiveXObject('Word.Application');
    var powerPointApp = new ActiveXObject('PowerPoint.Application');

    DOpus.Output('Enumerating...\n');

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        DOpus.Output(item);

        var itemPdf = item.path + '\\' + item.name_stem + '.pdf';
        if (fsu.Exists(itemPdf)) {
            DOpus.Output('... skipped. Target exists!\n');
            continue;
        }

        var ext = item.ext.substring(0, 4);

        if (ext == '.doc') {
            if (wordApp == null) {
                DOpus.Output('... skipped. Word could not be launched!\n');
            } else {
                var wordItem = wordApp.Documents.Open(String(item.realpath));
                wordItem.SaveAs2(itemPdf, 17); // wdFormatPDF
                wordItem.Close();
                DOpus.Output(itemPdf + '\n');
            }
        } else if (ext == '.ppt') {
            if (powerPointApp == null) {
                DOpus.Output('... skipped. PowerPoint could not be launched!\n');
            } else {
                var powerPointItem = powerPointApp.Presentations.Open(String(item.realpath));
                powerPointItem.SaveAs(itemPdf, 32); // ppSaveAsPDF
                powerPointItem.Close();
                DOpus.Output(itemPdf + '\n');
            }
        } else {
            DOpus.Output('... skipped. Filetype not supported!\n');
        }
    }

    DOpus.Output('\n... done.');

    cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Convert to .pdf</label>
	<tip>Convert all selected .doc and .ppt files to .pdf format</tip>
	<icon1>#office</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/convert-office-docs-to-pdf/41531</instruction>
		<instruction />
		<instruction>// 2022-06-20</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;Launching apps...\n&apos;);</instruction>
		<instruction>    var wordApp = new ActiveXObject(&apos;Word.Application&apos;);</instruction>
		<instruction>    var powerPointApp = new ActiveXObject(&apos;PowerPoint.Application&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;Enumerating...\n&apos;);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        DOpus.Output(item);</instruction>
		<instruction />
		<instruction>        var itemPdf = item.path + &apos;\\&apos; + item.name_stem + &apos;.pdf&apos;;</instruction>
		<instruction>        if (fsu.Exists(itemPdf)) {</instruction>
		<instruction>            DOpus.Output(&apos;... skipped. Target exists!\n&apos;);</instruction>
		<instruction>            continue;</instruction>
		<instruction>        }</instruction>
		<instruction />
		<instruction>        var ext = item.ext.substring(0, 4);</instruction>
		<instruction />
		<instruction>        if (ext == &apos;.doc&apos;) {</instruction>
		<instruction>            if (wordApp == null) {</instruction>
		<instruction>                DOpus.Output(&apos;... skipped. Word could not be launched!\n&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                var wordItem = wordApp.Documents.Open(String(item.realpath));</instruction>
		<instruction>                wordItem.SaveAs2(itemPdf, 17); // wdFormatPDF</instruction>
		<instruction>                wordItem.Close();</instruction>
		<instruction>                DOpus.Output(itemPdf + &apos;\n&apos;);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else if (ext == &apos;.ppt&apos;) {</instruction>
		<instruction>            if (powerPointApp == null) {</instruction>
		<instruction>                DOpus.Output(&apos;... skipped. PowerPoint could not be launched!\n&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                var powerPointItem = powerPointApp.Presentations.Open(String(item.realpath));</instruction>
		<instruction>                powerPointItem.SaveAs(itemPdf, 32); // ppSaveAsPDF</instruction>
		<instruction>                powerPointItem.Close();</instruction>
		<instruction>                DOpus.Output(itemPdf + &apos;\n&apos;);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else {</instruction>
		<instruction>            DOpus.Output(&apos;... skipped. Filetype not supported!\n&apos;);</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n... done.&apos;);</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Select NONE&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT SETFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

6 Likes

I've tinkered a bit and added support for Excel and the new Open-XML file types: docx, xlsx and pptx. Here we go:

// https://resource.dopus.com/t/convert-office-docs-to-pdf/41531

// 2022-06-20

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    cmd.deselect = false;

    cmd.RunCommand('Set UTILITY=otherlog');

    DOpus.ClearOutput();

    DOpus.Output('Launching apps...\n');
    var wordApp = new ActiveXObject('Word.Application');
    var powerPointApp = new ActiveXObject('PowerPoint.Application');
	var excelApp = new ActiveXObject('Excel.Application');

    DOpus.Output('Enumerating...\n');

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        DOpus.Output(item);

        var itemPdf = item.path + '\\' + item.name_stem + '.pdf';
        if (fsu.Exists(itemPdf)) {
            DOpus.Output('... skipped. Target exists!\n');
            continue;
        }

        var ext = item.ext.substring(0, 4);

        if (ext == '.doc' || ext == '.docx' ) {
            if (wordApp == null) {
                DOpus.Output('... skipped. Word could not be launched!\n');
            } else {
                var wordItem = wordApp.Documents.Open(String(item.realpath));
                wordItem.SaveAs2(itemPdf, 17); // wdFormatPDF
                wordItem.Close();
                DOpus.Output(itemPdf + '\n');
            }
        } else if (ext == '.ppt' || ext == '.pptx' ) {
            if (powerPointApp == null) {
                DOpus.Output('... skipped. PowerPoint could not be launched!\n');
            } else {
                var powerPointItem = powerPointApp.Presentations.Open(String(item.realpath));
                powerPointItem.SaveAs(itemPdf, 32); // ppSaveAsPDF
                powerPointItem.Close();
                DOpus.Output(itemPdf + '\n');
            }
        } else if (ext == '.xls' || ext == '.xlsx' ) {
            if (excelApp == null) {
                DOpus.Output('... skipped. Excel could not be launched!\n');
            } else {
                var excelItem = excelApp.Workbooks.Open(String(item.realpath));
                excelItem.SaveAs(itemPdf, 57); // exSaveAsPDF
                excelItem.Close();
                DOpus.Output(itemPdf + '\n');
            } 
		 } else {
            DOpus.Output('... skipped. Filetype not supported!\n');
        }
    }

    DOpus.Output('\n... done.');

    cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Convert to .pdf</label>
	<tip>Convert all selected .doc and .ppt files to .pdf format</tip>
	<icon1>#office</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/convert-office-docs-to-pdf/41531</instruction>
		<instruction />
		<instruction>// 2022-06-20</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;Launching apps...\n&apos;);</instruction>
		<instruction>    var wordApp = new ActiveXObject(&apos;Word.Application&apos;);</instruction>
		<instruction>    var powerPointApp = new ActiveXObject(&apos;PowerPoint.Application&apos;);</instruction>
		<instruction>	var excelApp = new ActiveXObject(&apos;Excel.Application&apos;);</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;Enumerating...\n&apos;);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        DOpus.Output(item);</instruction>
		<instruction />
		<instruction>        var itemPdf = item.path + &apos;\\&apos; + item.name_stem + &apos;.pdf&apos;;</instruction>
		<instruction>        if (fsu.Exists(itemPdf)) {</instruction>
		<instruction>            DOpus.Output(&apos;... skipped. Target exists!\n&apos;);</instruction>
		<instruction>            continue;</instruction>
		<instruction>        }</instruction>
		<instruction />
		<instruction>        var ext = item.ext.substring(0, 4);</instruction>
		<instruction />
		<instruction>        if (ext == &apos;.doc&apos; || ext == &apos;.docx&apos; ) {</instruction>
		<instruction>            if (wordApp == null) {</instruction>
		<instruction>                DOpus.Output(&apos;... skipped. Word could not be launched!\n&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                var wordItem = wordApp.Documents.Open(String(item.realpath));</instruction>
		<instruction>                wordItem.SaveAs2(itemPdf, 17); // wdFormatPDF</instruction>
		<instruction>                wordItem.Close();</instruction>
		<instruction>                DOpus.Output(itemPdf + &apos;\n&apos;);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else if (ext == &apos;.ppt&apos; || ext == &apos;.pptx&apos; ) {</instruction>
		<instruction>            if (powerPointApp == null) {</instruction>
		<instruction>                DOpus.Output(&apos;... skipped. PowerPoint could not be launched!\n&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                var powerPointItem = powerPointApp.Presentations.Open(String(item.realpath));</instruction>
		<instruction>                powerPointItem.SaveAs(itemPdf, 32); // ppSaveAsPDF</instruction>
		<instruction>                powerPointItem.Close();</instruction>
		<instruction>                DOpus.Output(itemPdf + &apos;\n&apos;);</instruction>
		<instruction>            }</instruction>
		<instruction>        } else if (ext == &apos;.xls&apos; || ext == &apos;.xlsx&apos; ) {</instruction>
		<instruction>            if (excelApp == null) {</instruction>
		<instruction>                DOpus.Output(&apos;... skipped. Excel could not be launched!\n&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                var excelItem = excelApp.Workbooks.Open(String(item.realpath));</instruction>
		<instruction>                excelItem.SaveAs(itemPdf, 57); // exSaveAsPDF</instruction>
		<instruction>                excelItem.Close();</instruction>
		<instruction>                DOpus.Output(itemPdf + &apos;\n&apos;);</instruction>
		<instruction>            } </instruction>
		<instruction>		 } else {</instruction>
		<instruction>            DOpus.Output(&apos;... skipped. Filetype not supported!\n&apos;);</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    DOpus.Output(&apos;\n... done.&apos;);</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Select NONE&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT SETFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

This is very useful as it doesn't require opening up separate programs.

Any way to add conversion for image to pdf? (jpg/png to pdf)

Would Office be the right tool for the job? Which Office program would you recommend?

ImageMagick would be my first choice:

magick.exe convert {filepath} {filepath|ext=pdf}