[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Shell.Run with variables VBScript

Posted on 2014-08-27
Medium Priority
Last Modified: 2014-08-28
This is what I need to start:
 shell.Run """\\CT01\Root\Visual_Documents\Vendors\V-ACACF-LAFAY\Checks\2014\42484.pdf"""
but I can't figure out how to encapsulate this in quotes.

I have tried:
  set shell = createobject("WSCRIPT.SHELL")
sThePDF = "\\CT01\Root\Visual_Documents\Vendors\V-" & VENDOR_ID & "\Checks\" & Check_YYYY & "\" & Check_Num & ".pdf"
Shell.Run sThePDF
Shell.Run ("sThePDF")
Shell.Run """\\CT01\Root\Visual_Doucments\Vendors\V-"" & VENDOR_ID & ""\Checks\"" & Check_YYYY & ""\"" & Check_Num & "".pdf""",1, True

None of these work. All give an error message.

I even tried:

 sThePDF = AddQuotes(sThePDF)
 sThePDF = AddQuotes(sThePDF)
 sThePDF = AddQuotes(sThePDF)
 Shell.Run sThePDF

No Error, but it just opens windows explorer.

Function AddQuotes(strInput)
    AddQuotes = Chr(34) & strInput & Chr(34)
End Function
Question by:BFanguy
LVL 65

Accepted Solution

RobSampson earned 1200 total points
ID: 40289313
Hi, this has worked for me, so it should work this way.



VENDOR_ID = "123"
Check_YYYY = "2014"
Check_Num = "456"
Set objShell = createobject("WSCRIPT.SHELL")
sThePDF = "\\CT01\Root\Visual_Documents\Vendors\V-" & VENDOR_ID & "\Checks\" & Check_YYYY & "\" & Check_Num & ".pdf"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sThePDF) = True Then
	objShell.Run """" & sThePDF & """",1, True
	WScript.Echo "File not found: " & sThePDF
End If

Open in new window

LVL 11

Assisted Solution

by:Dany Balian
Dany Balian earned 800 total points
ID: 40289851
Rob's Answer is correct

just to explain string concatenation in vb:
dim mystring as string
assigns dany to mystring variable

now, if we want to add a double quotation inside the word, we just escape it by doubling it:
this will assign da"ny to the mystring variable.

so coming to your example, you just need your string to have a double quotation in the variable itself at the beginning and at the end, so you just need to insert 2 " at the beginning and at the end of your variable declaration.

so, if you look at your #1 code:
sThePDF = "\\CT01\Root\Visual_Documents\Vendors\V-" & VENDOR_ID & "\Checks\" & Check_YYYY & "\" & Check_Num & ".pdf"

your sThePdf variable's value would be:

sThePDF = """\\CT01\Root\Visual_Documents\Vendors\V-" & VENDOR_ID & "\Checks\" & Check_YYYY & "\" & Check_Num & ".pdf"""

your sThePdf variable's value would become:

Author Closing Comment

ID: 40290215
thanks guys... forest for the trees stuff...

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question