Run Node.js using VBA

荒凉一梦 提交于 2020-07-11 06:40:58

问题


The overall goal is to run Node.js on a JavaScript file and scrape the output using MS Access VBA. Here's the JavaScript file I'm using to test:

// C:\Users\micha\OneDrive\AppAcademy\Interview Prep\tst.js

var fs = require("fs");
var outputFile = "testLog.txt";

fs.writeFile(outputFile, new Date());

When I run this from the command prompt as follows, it generates a testLog.txt file with a timestamp in the Interview Prep folder.

C:\Users\micha\OneDrive\AppAcademy\Interview Prep>node tst.js

Now I want to trigger the same thing using VBA from within an Access app. I've tried it two ways. The first is quick and dirty, but my understanding is that I can flesh it out with StdOut for the scraping bit. It uses WScript.Shell:

Private Sub runShellTest_Click()
    Dim objShell As Object, objExec As Object
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec("C:\Program Files\nodejs\node.exe C:\Users\micha\OneDrive\AppAcademy\Interview Prep\tst.js")
End Sub

When I run it, a command prompt briefly flashes onscreen, but I don't get a new testLog.txt.

The other way I tried is even quicker and dirtier:

Private Sub runShellTest_Click()
    MsgBox (Shell("C:\Program Files\nodejs\node.exe C:\Users\micha\OneDrive\AppAcademy\Interview Prep\tst.js", vbNormalFocus))
End Sub

Again, the command prompt flashes up briefly, and there's no new testLog.txt. However, the message box shows a task ID as expected, so I guess... something happened?

Any help is appreciated!


回答1:


When you run it like this, the file will get generated at the Access executable's location. Please check where your Access is installed location, you should find the file there. In node code you will have to also pass the path where the file needs to be generated.




回答2:


I'm been playing with that successfully sending xmlhttp request. You will need converting json to vba dictionary. There is a vba class you can use for the conversion.



来源:https://stackoverflow.com/questions/44428828/run-node-js-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!