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('Set UTILITY=otherlog');</instruction>
<instruction />
<instruction> DOpus.ClearOutput();</instruction>
<instruction />
<instruction> DOpus.Output('Launching apps...\n');</instruction>
<instruction> var wordApp = new ActiveXObject('Word.Application');</instruction>
<instruction> var powerPointApp = new ActiveXObject('PowerPoint.Application');</instruction>
<instruction />
<instruction> DOpus.Output('Enumerating...\n');</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 + '\\' + item.name_stem + '.pdf';</instruction>
<instruction> if (fsu.Exists(itemPdf)) {</instruction>
<instruction> DOpus.Output('... skipped. Target exists!\n');</instruction>
<instruction> continue;</instruction>
<instruction> }</instruction>
<instruction />
<instruction> var ext = item.ext.substring(0, 4);</instruction>
<instruction />
<instruction> if (ext == '.doc' || ext == '.rtf') {</instruction>
<instruction> if (wordApp == null) {</instruction>
<instruction> DOpus.Output('... skipped. Word could not be launched!\n');</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 + '\n');</instruction>
<instruction> }</instruction>
<instruction> } else if (ext == '.ppt') {</instruction>
<instruction> if (powerPointApp == null) {</instruction>
<instruction> DOpus.Output('... skipped. PowerPoint could not be launched!\n');</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 + '\n');</instruction>
<instruction> }</instruction>
<instruction> } else {</instruction>
<instruction> DOpus.Output('... skipped. Filetype not supported!\n');</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction />
<instruction> DOpus.Output('\n... done.');</instruction>
<instruction />
<instruction> cmd.RunCommand('Select NONE');</instruction>
<instruction> cmd.RunCommand('Select FROMSCRIPT SETFOCUS');</instruction>
<instruction> cmd.RunCommand('Select SHOWFOCUS');</instruction>
<instruction>}</instruction>
</function>
</button>
dopus will not work
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
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
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.