Using strings for network paths with Shell in VBA
up vote
1
down vote
favorite
QUESTION UPDATED, PLEASE SEE BELOW
I am trying to use string variables (exedir
- the full network drive directory of an exe file, and inputdir
- the location of the input file argument) to launch an exe with its argument using shell in VBA.
The commented out line in the script below does not work, and I cannot figure out why. If I manually set the drive using a letter I can get it to work, as shown below, but the .exe and .lam input file used here are both on networked drives (the variable comp
is the unique name of the users PC, which sets the name of the .lam input file, so the input file name is different for every user).
I'm not too familiar with the syntax and borrowed it from elsewhere. Am I missing a quotation mark or maybe have too many in the commented out row?
Sub CallExeWithInput()
Set wsh = VBA.CreateObject("WScript.Shell")
'Statusnum = wsh.Run(Command:="""" & exedir & """""""" & inputdir & """""", windowstyle:=1, waitonreturn:=False)
SetCurrentDirectory "M:path"
Statusnum = wsh.Run(Command:="foo.exe ""M:path" & comp & ".lam""", windowstyle:=1, waitonreturn:=False)
End Sub
Example of exedir: \networkpathfoo.exe
Example of inputdir: \networkpathcompname.lam
Example of comp: compname
<< found using Environ$("computername")
I'm aware of some previous questions, having read many to get this far. I may be missing something, but I couldn't solve my issue using their contents. I'm sure it's something simple...
I don't want to provide the letter of the drive, and ideally want to launch the .exe with the input file using string variables to input everything. One major reason I want to only use strings, is that they can be controlled by a single variable, and when the directory changes (say with an upgrade of the .exe) it will be easy to update this script.
UPDATE
Following the comments kindly provided below I arrived here:
SetCurrentDirectory fldr
Statusnum = wsh.Run(Command:="foo.exe " & quote & inputdir & quote, windowstyle:=1, waitonreturn:=False)
Where quote = chr(34)
and fldr = \networkpath
Interestingly, if inputdir is defined as a path to a lettered drive (inputdir = M:etc
), this works. If it is to a network drive path (inputdir = \networketc
), it launches but the .exe immediately crashes with an invalid floating point operation.
How can I alter this so the input file can be provided as a network path?
If this is not possible I will presumably need a subroutine that locates which letter the user has mapped the network drive onto in order to build my string, such as this:
path = ":foobar"
dim x as integer
for x = 65 to 90
If CBool(Len(Dir(Chr(x) & path))) Then
msgbox "It's in drive " & Chr(x)
exit for
end if
next x
Thank you all for your help so far.
excel vba excel-vba shell
|
show 1 more comment
up vote
1
down vote
favorite
QUESTION UPDATED, PLEASE SEE BELOW
I am trying to use string variables (exedir
- the full network drive directory of an exe file, and inputdir
- the location of the input file argument) to launch an exe with its argument using shell in VBA.
The commented out line in the script below does not work, and I cannot figure out why. If I manually set the drive using a letter I can get it to work, as shown below, but the .exe and .lam input file used here are both on networked drives (the variable comp
is the unique name of the users PC, which sets the name of the .lam input file, so the input file name is different for every user).
I'm not too familiar with the syntax and borrowed it from elsewhere. Am I missing a quotation mark or maybe have too many in the commented out row?
Sub CallExeWithInput()
Set wsh = VBA.CreateObject("WScript.Shell")
'Statusnum = wsh.Run(Command:="""" & exedir & """""""" & inputdir & """""", windowstyle:=1, waitonreturn:=False)
SetCurrentDirectory "M:path"
Statusnum = wsh.Run(Command:="foo.exe ""M:path" & comp & ".lam""", windowstyle:=1, waitonreturn:=False)
End Sub
Example of exedir: \networkpathfoo.exe
Example of inputdir: \networkpathcompname.lam
Example of comp: compname
<< found using Environ$("computername")
I'm aware of some previous questions, having read many to get this far. I may be missing something, but I couldn't solve my issue using their contents. I'm sure it's something simple...
I don't want to provide the letter of the drive, and ideally want to launch the .exe with the input file using string variables to input everything. One major reason I want to only use strings, is that they can be controlled by a single variable, and when the directory changes (say with an upgrade of the .exe) it will be easy to update this script.
UPDATE
Following the comments kindly provided below I arrived here:
SetCurrentDirectory fldr
Statusnum = wsh.Run(Command:="foo.exe " & quote & inputdir & quote, windowstyle:=1, waitonreturn:=False)
Where quote = chr(34)
and fldr = \networkpath
Interestingly, if inputdir is defined as a path to a lettered drive (inputdir = M:etc
), this works. If it is to a network drive path (inputdir = \networketc
), it launches but the .exe immediately crashes with an invalid floating point operation.
How can I alter this so the input file can be provided as a network path?
If this is not possible I will presumably need a subroutine that locates which letter the user has mapped the network drive onto in order to build my string, such as this:
path = ":foobar"
dim x as integer
for x = 65 to 90
If CBool(Len(Dir(Chr(x) & path))) Then
msgbox "It's in drive " & Chr(x)
exit for
end if
next x
Thank you all for your help so far.
excel vba excel-vba shell
1
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
1
Dim Quote as String
,Quote = Chr(34)
, then replace the multitude of multiple quotes with& Quote &
to make it reasonably readable. At one point, you have""""""""
which, to my eye ends up being"""
which is 3 double quotes in a row, which probably isn't right.
– FreeMan
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
QUESTION UPDATED, PLEASE SEE BELOW
I am trying to use string variables (exedir
- the full network drive directory of an exe file, and inputdir
- the location of the input file argument) to launch an exe with its argument using shell in VBA.
The commented out line in the script below does not work, and I cannot figure out why. If I manually set the drive using a letter I can get it to work, as shown below, but the .exe and .lam input file used here are both on networked drives (the variable comp
is the unique name of the users PC, which sets the name of the .lam input file, so the input file name is different for every user).
I'm not too familiar with the syntax and borrowed it from elsewhere. Am I missing a quotation mark or maybe have too many in the commented out row?
Sub CallExeWithInput()
Set wsh = VBA.CreateObject("WScript.Shell")
'Statusnum = wsh.Run(Command:="""" & exedir & """""""" & inputdir & """""", windowstyle:=1, waitonreturn:=False)
SetCurrentDirectory "M:path"
Statusnum = wsh.Run(Command:="foo.exe ""M:path" & comp & ".lam""", windowstyle:=1, waitonreturn:=False)
End Sub
Example of exedir: \networkpathfoo.exe
Example of inputdir: \networkpathcompname.lam
Example of comp: compname
<< found using Environ$("computername")
I'm aware of some previous questions, having read many to get this far. I may be missing something, but I couldn't solve my issue using their contents. I'm sure it's something simple...
I don't want to provide the letter of the drive, and ideally want to launch the .exe with the input file using string variables to input everything. One major reason I want to only use strings, is that they can be controlled by a single variable, and when the directory changes (say with an upgrade of the .exe) it will be easy to update this script.
UPDATE
Following the comments kindly provided below I arrived here:
SetCurrentDirectory fldr
Statusnum = wsh.Run(Command:="foo.exe " & quote & inputdir & quote, windowstyle:=1, waitonreturn:=False)
Where quote = chr(34)
and fldr = \networkpath
Interestingly, if inputdir is defined as a path to a lettered drive (inputdir = M:etc
), this works. If it is to a network drive path (inputdir = \networketc
), it launches but the .exe immediately crashes with an invalid floating point operation.
How can I alter this so the input file can be provided as a network path?
If this is not possible I will presumably need a subroutine that locates which letter the user has mapped the network drive onto in order to build my string, such as this:
path = ":foobar"
dim x as integer
for x = 65 to 90
If CBool(Len(Dir(Chr(x) & path))) Then
msgbox "It's in drive " & Chr(x)
exit for
end if
next x
Thank you all for your help so far.
excel vba excel-vba shell
QUESTION UPDATED, PLEASE SEE BELOW
I am trying to use string variables (exedir
- the full network drive directory of an exe file, and inputdir
- the location of the input file argument) to launch an exe with its argument using shell in VBA.
The commented out line in the script below does not work, and I cannot figure out why. If I manually set the drive using a letter I can get it to work, as shown below, but the .exe and .lam input file used here are both on networked drives (the variable comp
is the unique name of the users PC, which sets the name of the .lam input file, so the input file name is different for every user).
I'm not too familiar with the syntax and borrowed it from elsewhere. Am I missing a quotation mark or maybe have too many in the commented out row?
Sub CallExeWithInput()
Set wsh = VBA.CreateObject("WScript.Shell")
'Statusnum = wsh.Run(Command:="""" & exedir & """""""" & inputdir & """""", windowstyle:=1, waitonreturn:=False)
SetCurrentDirectory "M:path"
Statusnum = wsh.Run(Command:="foo.exe ""M:path" & comp & ".lam""", windowstyle:=1, waitonreturn:=False)
End Sub
Example of exedir: \networkpathfoo.exe
Example of inputdir: \networkpathcompname.lam
Example of comp: compname
<< found using Environ$("computername")
I'm aware of some previous questions, having read many to get this far. I may be missing something, but I couldn't solve my issue using their contents. I'm sure it's something simple...
I don't want to provide the letter of the drive, and ideally want to launch the .exe with the input file using string variables to input everything. One major reason I want to only use strings, is that they can be controlled by a single variable, and when the directory changes (say with an upgrade of the .exe) it will be easy to update this script.
UPDATE
Following the comments kindly provided below I arrived here:
SetCurrentDirectory fldr
Statusnum = wsh.Run(Command:="foo.exe " & quote & inputdir & quote, windowstyle:=1, waitonreturn:=False)
Where quote = chr(34)
and fldr = \networkpath
Interestingly, if inputdir is defined as a path to a lettered drive (inputdir = M:etc
), this works. If it is to a network drive path (inputdir = \networketc
), it launches but the .exe immediately crashes with an invalid floating point operation.
How can I alter this so the input file can be provided as a network path?
If this is not possible I will presumably need a subroutine that locates which letter the user has mapped the network drive onto in order to build my string, such as this:
path = ":foobar"
dim x as integer
for x = 65 to 90
If CBool(Len(Dir(Chr(x) & path))) Then
msgbox "It's in drive " & Chr(x)
exit for
end if
next x
Thank you all for your help so far.
excel vba excel-vba shell
excel vba excel-vba shell
edited yesterday
asked yesterday
Petrichor
3881216
3881216
1
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
1
Dim Quote as String
,Quote = Chr(34)
, then replace the multitude of multiple quotes with& Quote &
to make it reasonably readable. At one point, you have""""""""
which, to my eye ends up being"""
which is 3 double quotes in a row, which probably isn't right.
– FreeMan
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday
|
show 1 more comment
1
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
1
Dim Quote as String
,Quote = Chr(34)
, then replace the multitude of multiple quotes with& Quote &
to make it reasonably readable. At one point, you have""""""""
which, to my eye ends up being"""
which is 3 double quotes in a row, which probably isn't right.
– FreeMan
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday
1
1
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
1
1
Dim Quote as String
, Quote = Chr(34)
, then replace the multitude of multiple quotes with & Quote &
to make it reasonably readable. At one point, you have """"""""
which, to my eye ends up being """
which is 3 double quotes in a row, which probably isn't right.– FreeMan
yesterday
Dim Quote as String
, Quote = Chr(34)
, then replace the multitude of multiple quotes with & Quote &
to make it reasonably readable. At one point, you have """"""""
which, to my eye ends up being """
which is 3 double quotes in a row, which probably isn't right.– FreeMan
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265621%2fusing-strings-for-network-paths-with-shell-in-vba%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Build the command you're going to run with the shell in a variable, then look at it using the debugger. Does it look correct?
– Comintern
yesterday
You need to know what value you're trying to achieve to compare with what you've got and know what's missing or superfluous.
– Mathieu Guindon
yesterday
@Comintern Thanks and nope, experimenting now. I'm not a fan of all of the """ syntax, but it's beginning to make more sense.
– Petrichor
yesterday
1
Dim Quote as String
,Quote = Chr(34)
, then replace the multitude of multiple quotes with& Quote &
to make it reasonably readable. At one point, you have""""""""
which, to my eye ends up being"""
which is 3 double quotes in a row, which probably isn't right.– FreeMan
yesterday
@FreeMan great suggestion, thanks
– Petrichor
yesterday