1
votes

Short Background

I've got an application that I built in Excel, and I'm attempting to add functionality where Excel acts as a controller and runs different VBScript files (I know this sounds like an odd case, but bear with me). To test the concept, I wrote a short VBScript that simply Echos the path to the folder that the script is in.

VBScript (run-from-excel.vbs)

Set fso = CreateObject("Scripting.FileSystemObject")

scriptPath = fso.GetAbsolutePathName(".")
WScript.Echo(scriptPath)

When run with cscript run-from-excel.vbs from a console in the proper directory, the output is: C:\Users\cmfonvi\dev\vbscript, which is as expected.

Now, for the Excel integration. I wrote a simple Sub that runs a VBScript file through WScript on the console.

VBA - Excel

Sub runVBScript(filePath As String)
    Shell "wscript " & filePath, vbNormalFocus
End Sub

I now run this Sub, with the command

runVBScript (ActiveWorkbook.Path & "\run-from-excel.vbs")

(the workbook and VBScript are currently in the same directory, so this works). However, the output is not as I expected. The window it opens outputs C:\Users\cmfonvi\Documents, which means that somehow the wscript process running the VBScript is initialized in the %userprofile%\Documents directory.

My question is two-fold:

  1. Why is this the case?
  2. How can I make the console initialize in the proper directory (say, for example, in %userprofile%\Desktop)?
1

1 Answers

1
votes

. (in your VBScript code) is the current working directory. ActiveWorkbook.Path (in your VBA code) is the path of the directory containing the active workbook. Those two paths are not necessarily the same. If you want the VBScript code to output the directory where the script is located you need to use something like this:

fso.GetParentFolderName(WScript.ScriptFullName)

If you want the VBScript to use a particular working directory change it like this:

Set sh = CreateObject("WScript.Shell")
sh.CurrentDirectory = "C:\some\folder"