Using strings for network paths with Shell in VBA











up vote
1
down vote

favorite
1












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.










share|improve this question




















  • 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















up vote
1
down vote

favorite
1












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.










share|improve this question




















  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?