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-11-25

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' || ext == '.rtf') {
            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 selected 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-11-25</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; || ext == &apos;.rtf&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>

7 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>
3 Likes

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}
1 Like

Hi, any chance the script can convert Word documents with the bookmarks embedded as the current saveas2 command does not retain the bookmarks.

Word VBA can do this with the ExportAsFixedFormat command, e.g.,

However, when I replace your saveas2 command with

wordItem.ExportAsFixedFormat outputfilename:=itemPdf,ExportFormat:=17, CreateBookmarks:=1

dopus will not work :frowning:
Parse error - script aborted
Error at line 35, position 60 (raw line 39)
...rtAsFixedFormat(outputfilename:=itemPdf,ExportFormat:=17, Cr...
^
Expected ')' (0x800a03ee)
Parse error - script aborted

Any idea on how to get this to work?

The script is JScript but the code you're adding is using VBScript/VBA syntax.

Yeah, I am not familiar with JScript. Thought I give this VBA command a chance but didn't work. Thus, wondering whether there is a way to save the word document with the bookmarks enabled

Try

wordItem.ExportAsFixedFormat(itemPdf, 17, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 1); // ExportFormat:=17, CreateBookmarks:=1

Excellent, that worked :wink: Thanks

Hi, does this script still work with Dopus 13.10?

I've installed it but it fails to load from the Script Management modal with this message: "Status: Failed (methods)".

I've also installed the XML button but when clicking on it while a docx document is selected, I get "Error at line 34, position 17 (raw line 38).

I followed the instructions in the "How To" post about buttons and scripts, though it's still possible that I committed some operator error somewhere.

Any help appreciated!

1 Like

It's a script that goes in a toolbar button or menu item. See the Script Buttons section for details on how to use it.

If you're seeing the "methods" error it sounds like the script was saved into the script add-ins folder, which is for more complex scripts that handle events and things like that.