在后台模式下从Nodejs执行VBS(任务计划程序或Windows服务) [英] Execute VBS from Nodejs in background mode (Tasks Scheduler or Windows Service)

查看:475
本文介绍了在后台模式下从Nodejs执行VBS(任务计划程序或Windows服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to convert an Excel File to PDF from a NodeJS App. It works when I start Node in command line, but when I start my app.bat as Windows Service through NSSM or directly with the Tasks Scheduler, it doesn't work anymore.

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

With the script bellow, I can only see the first row in the log.txt which is the program arguments. If everything was going well, I should see 0, then 1, and 2. Sadly, when Node runs in background, I don't have any number, so i'm guessing the problem is CreateObject("Excel.Application") but i don't know why and how to resolve it.

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");
        }
      }
    });

我也尝试过这样的事情:

I also tried things like this :

    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,
            });

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

However, it's not working either. Does someone have an idea ?

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

This problem seems unresolvable. Some people have asked the same question many years ago, and there is still no answer ...

nodejs服务-在Windows系统中,无法执行jar文件

推荐答案

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

I found the answer (but it's not really a solution). As i guessed, the problem is related to Excel, so it's not VBScript or NodeJS. I used EdgeJs to do the same thing in C# and i have the same problem.

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

You can edit/create Open XML files with their library, but you can't save in PDF or use any macro in server mode.

Microsoft回答:

Microsoft answer :


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

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

If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully.

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

The CreateObject function and the CoCreateInstance function return one of the following run-time error messages and cannot be started for Automation... ... ... ...

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

I also tried with winax (node addon using node-ffi), same result. It works in client mode, but not in server mode (DispInvoke: Open Microsoft Excel cannot access the file).

这篇关于在后台模式下从Nodejs执行VBS(任务计划程序或Windows服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆