在后台模式(任务计划程序或Windows服务)中从Nodejs执行VBS

我正在尝试从NodeJS应用程序将Excel文件转换为PDF。当我在命令行中启动Node时,它可以工作,但是当我通过NSSM或直接使用Tasks Scheduler作为Windows Service来启动app.bat时,它将不再起作用。

使用以下脚本,我只能在log.txt中看到第一行,即程序参数。如果一切正常,我应该看到0,然后是1,然后是2。可悲的是,当Node在后台运行时,我没有任何数字,所以我猜问题出在CreateObject("Excel.Application"),但我没有不知道为什么以及如何解决它。

VBScript:

    Set objFSO=CreateObject("Scripting.FileSystemObject")
    outFile="C:\Users\admin\Documents\Projects\tools\log.txt"
    Set objFile = objFSO.CreateTextFile(outFile,True)
    objFile.Write Wscript.Arguments.Item(0) & " | " & WScript.Arguments.Item(1) & vbCrLf

    Dim oExcel
    Set oExcel = CreateObject("Excel.Application")
    Dim oBook

    objFile.Write "0" & vbCrLf
    Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))

    objFile.Write "1" & vbCrLf
    oBook.ExportAsFixedFormat xlTypePDF,WScript.Arguments.Item(1)

    objFile.Write "2" & vbCrLf
    oBook.Close True

NodeJS:

    const {exec} = require('child_process');
    exec('"' + Meteor.settings.directories.tools + 'convertToPDF.vbs" "' + outputServerFilePathChild + '.xlsx" "' + outputServerFilePathChild + '.pdf"',(err,stdout,stderr) => {
      if (err) {
        console.log(stderr);
        reject(new Error(stderr));
      } else {
        if (fs.existsSync(outputServerFilePathChild.replace(/ /g,"%20") + ".pdf")) {
          console.log('rename "' + outputServerFilePathChild.replace(/ /g,"%20") + '.pdf" "' + outputServerFilePathChild + '.pdf"');
          exec('rename "' + outputServerFilePathChild.replace(/ /g,"%20") + '.pdf" "' + outputServerFilePathChild + '.pdf"',stderr) => {
            console.log("File generated: " + outputServerFilePathChild + ".pdf");
                    resolve(outputClientFilePathChild + ".pdf");
                  });
        } else {
          console.log("File generated: " + outputServerFilePathChild + ".pdf");
          resolve(outputClientFilePathChild + ".pdf");
        }
      }
    });

我也尝试过这样的事情:

    const child2 = spawn('start',[
                '"PDFConverter"','cscript','"C:\\Users\\admin\\Documents\\Projects\\tools\\convertToPDF.vbs"','"C:\\Users\\admin\\Documents\\Projects\\reports\\admin\\rebates\\customer1 2019-09-30.xlsx"','"C:\\Users\\admin\\Documents\\Projects\\reports\\admin\\rebates\\customer1 2019-09-30.pdf"'
            ],{
                shell: true,windowsHide: true,});

但是,它也不起作用。有人有主意吗?

编辑:此问题似乎无法解决。许多年前,有人问过同样的问题,但仍然没有答案...

A008888 回答:在后台模式(任务计划程序或Windows服务)中从Nodejs执行VBS

我找到了答案(但这并不是真正的解决方案)。如我所料,问题与 Excel 有关,因此不是 VBScript NodeJS 。我使用 EdgeJs C#中执行相同的操作,并且遇到了相同的问题。

您可以使用其库来编辑/创建Open XML文件,但不能将其保存为PDF或在服务器模式下使用任何宏。

Microsoft答案:

  

https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office?wa=wsignin1.0%3Fwa%3Dwsignin1.0

     

如果您使用服务器端解决方案中的Office应用程序,则   应用程序将缺少许多运行所需的功能   成功。

     

CreateObject函数和CoCreateInstance函数返回一个   以下运行时错误消息,并且无法针对   自动化...   ... ... ...

本文链接:https://www.f2er.com/3144146.html

大家都在问