Shell.Run with variables VBScript

Posted on 2014-08-27
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

    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
    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

    thanks guys... forest for the trees stuff...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now