Skip to main content
Participating Frequently
June 29, 2021
Answered

Cfexecute problem

  • June 29, 2021
  • 1 reply
  • 1224 views

I have  batch file which run script to convert word to pdf .When directly run batch file conversion done perfectly.

When I  try to use coldfusion cfexecute to run the batchfile  it take continuously  execute until browser close  no error and no output .

My  cfm file and bat file location  is D drive

<cfexecute name="C:\Windows\System32\cmd.exe" timeout="2000">

cd /D D:\home\docxtopdf

bulk-convert-Word2PDF.bat

</cfexecute>

 

My batch file :

echo off
for %%X in (*.docx) do cscript.exe //nologo SaveAsPDF.js "%%X"
for %%X in (*.doc) do cscript.exe //nologo SaveAsPDF.js "%%X"

 

My SaveAsPDF.js

ar obj = new ActiveXObject("Scripting.FileSystemObject");
var docPath = WScript.Arguments(0);
docPath = obj.GetAbsolutePathName(docPath);

var pdfPath = docPath.replace(/\.doc[^.]*$/, ".pdf");
var objWord = null;
var wdAlignPageNumberCenter = 1;
var wdPageNumberStyleLowercaseRoman=46
try
{
objWord = new ActiveXObject("Word.Application");
objWord.Visible = false;
var objDoc = objWord.Documents.Open(docPath);
objDoc.Sections(1).Footers(1).PageNumbers.Add(wdAlignPageNumberCenter);
objDoc.Sections(1).Footers(1).PageNumbers.NumberStyle = wdPageNumberStyleLowercaseRoman;

var format = 17;
objDoc.SaveAs(pdfPath, format);
objDoc.Close();

WScript.Echo("Saving '" + docPath + "' as '" + pdfPath + "'...");
}
finally
{
if (objWord != null)
{
objWord.Quit();
}
}

Help me with some solution to work with it

 

 

 

 

    This topic has been closed for replies.
    Correct answer BKBK

    I try to use com object Word Application for alternate method but it also throw class not registered,

    I am using Coldfsuion2016 64bit and Ms office standard 2013


    @defaultne3mldqroexz , I modified your code just a little bit, and it works! 🙂

    The steps:

    1. Change the contents of the batch file bulk-convert-Word2PDF.bat to:

    @echo off
    cd /D D:\home
    for %%X in (*.docx) do cscript.exe //nologo SaveAsPDF.js "%%X"
    for %%X in (*.doc) do cscript.exe //nologo SaveAsPDF.js "%%X"

     

    2. Ensure that the file paths are as follows (where testDoc1.docx and testDoc2.doc are the files to be converted to PDF):

        D:\home\SaveAsPDF.js 

        D:\home\bulk-convert-Word2PDF.bat

        D:\home\testDoc1.docx

        D:\home\testDoc2.doc

     

    3) Launch a CFM page containing the code:

    <cfexecute name="D:\home\bulk-convert-Word2PDF.bat" timeout="120" />

     

     

     

     

    1 reply

    BKBK
    Community Expert
    Community Expert
    June 30, 2021

    I think you should let ColdFusion execute the batch directly. Assuming that the batch file is in D:\home\, I would try something like:

    <cfexecute name="D:\home\bulk-convert-Word2PDF.bat" arguments="D:\home\docxtopdf" timeout="2000">

     

    Participating Frequently
    July 1, 2021

    Hi  I tried already to exceute it, no error throw , in browser it shows C:\ColdFusion2016\cfusion\bin>echo off

    conversion never done.

     

    BKBK
    Community Expert
    Community Expert
    July 1, 2021

    Hmm, you're right. I, too, had the same experience with a batch conversion in C:\temp. The ColdFusion path "C:\ColdFusion2021\cfusion\bin" was thrown into the mix.

     

    In spite of this, I did a trick and it got the conversions with cfexecute to work. I am sure yours will, too. But there is still a problem we have to solve.

     

    The procedure to try is as follows:

     

    1. Taking the hint from ColdFusion, copy the files bulk-convert-Word2PDF.bat and SaveAsPDF.js to C:\ColdFusion2016\cfusion\bin.
    2. Copy some *.docx and/or *.doc files to C:\ColdFusion2016\cfusion\bin.
    3. Execute the batch file from any location where it is present, for example, from D:\home\bulk-convert-Word2PDF.bat:
    <!--- Timeout of 120 seconds should be sufficient --->
    <cfexecute name="D:\home\bulk-convert-Word2PDF.bat" timeout="120" />

     

    Did that work for you?

    If so, then the problem that remains to be solved is how to do a DOS cd to avoid having to use C:\ColdFusion2016\cfusion\bin.