Setting Directories and the If Len(Dir(… statement in VBA

烂漫一生 提交于 2019-12-01 10:43:17

问题


I have a file exists under this path:

//path/folder1/folder2/datafile.gdp

It is an input to an external program being called from vba in this manner:

Sub RunProgram()
Dim wsh As Object
SetCurrentDirectory "\\path\"
ChDir "\\path\folder1\folder2\" 'Set Directory
Set wsh = VBA.CreateObject("WScript.Shell")
check = CurDir
Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

But on the final line, including \path\folder1\folder2\ before the input file name appears to cause the external program to want to write some files into a duplicated directory that doesn't exist, causing an error. It used to work in this format before the .exe was updated by an external company. It now wants to write some files here, all prefixed with the name of my input file:

\\path\folder1\folder2\PATH\FOLDER1\FOLDER2\

Hoping to fix this, I changed the final line of the code to this, following some comments on a previous post here on SO:

Statusnum = wsh.Run(Command:="program.exe ""datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

Since the directory is set correctly prior to calling the .exe, I thought removing the path for the input file would solve the issue.

The program now launches, but doesn't appear to load the input file with it and no longer runs calculations automatically in the background as it should. Instead, it launches and the main .exe window pops up to the user as if it had just been launched for setting up a new project, calculations don't occur automatically.

To check which directory the VBA code was try to pull my datafile.gdp from, I created these loops directly before calling the .exe:

If Len(Dir("\\path\folder1\folder2\datafile.gdp")) = 0 Then
    FileIsMissing1 = True 'I use Excel-VBA watches to break if true
End If

If Len(Dir("datafile.gdp")) = 0 Then
    FileIsMissing2 = True 
End If

Bizarrely, neither of these loops causes a break. The file only exists in

\\path\folder1\folder2\datafile.gdp

Not in the duplicated directory... so why are both of these statements satisfied? Does entering the directory make no difference even when the current directory has been set? The current directory seems to be impacting the line:

Statusnum = wsh.Run(Command:="program.exe ""\\path\folder1\folder2\datafile.gdp""", WindowStyle:=1, waitonreturn:=True)

But not these if loops, and I'm not sure why.

来源:https://stackoverflow.com/questions/47751374/setting-directories-and-the-if-lendir-statement-in-vba

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