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

Posted

技术标签:

【中文标题】在后台模式下从 Nodejs 执行 VBS(任务计划程序或 Windows 服务)【英文标题】:Execute VBS from Nodejs in background mode (Tasks Scheduler or Windows Service) 【发布时间】:2019-11-07 14:16:56 【问题描述】:

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

使用下面的脚本,我只能看到 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"', (err, stdout, 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,
            );

但是,它也不起作用。有人有想法吗?

编辑:这个问题似乎无法解决。很多年前有人问过同样的问题,至今没有答案……

Node.js as service, exec doesn't work

nodejs service -in windows system ,unable to execute jar files

【问题讨论】:

您的错误可能出现在objFile.Write Wscript.Arguments.Item(0) & " | " & WScript.Arguments.Item(1) & vbCrLf 行上,可能是命令行参数没有出现在脚本中。 Set oExcel = CreateObject("Excel.Application") 很好。 在 log.txt 中我写了第一行,我添加了这一行来调试我的脚本。它以前不起作用,这就是我添加这些行的原因。所以问题不在这里。 【参考方案1】:

我找到了答案(但这并不是真正的解决方案)。正如我所猜测的,问题与 Excel 有关,因此不是 VBScriptNodeJS。我使用 EdgeJsC# 中做同样的事情,但我遇到了同样的问题。

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

微软回答:

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

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

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

我也尝试过使用 winax(使用 node-ffi 的节点插件),结果相同。它在客户端模式下工作,但在服务器模式下不工作(DispInvoke:打开 Microsoft Excel 无法访问文件)。

【讨论】:

我可以确认这一点。我曾经在 Windows 7 中运行我的 excel VBA 自动化来发送由另一个 nodejs 脚本 VIA 任务计划程序调用的电子邮件和 VBS 自动化。但是在升级到 Windows 10 之后,现在一切都失败了,并出现了您分享的文章中提到的错误。如果我手动调用脚本或打开 excel 文件,一切正常。我将尝试制作一个永远运行的 nodejs 程序来为我触发这些脚本。让我知道您是否尝试过它以及它是否有效?它可能会为我节省一些精力。

以上是关于在后台模式下从 Nodejs 执行 VBS(任务计划程序或 Windows 服务)的主要内容,如果未能解决你的问题,请参考以下文章