Results 1 to 12 of 12
  1. #1
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using VBS to open a program and file.

    I want an application to run and open a specific file when I click a button. I can create the button in HTML easily like this:

    <INPUT Type = "Button" Name = "cmdReadMe" Value = "Open Read Me text">

    Now, to associate the click event with this button, I need a script section that looks something like this:

    <SCRIPT Language=VBScript>

    Sub cmdReadMe_onclick

    Dim WshShell
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("notepad Readme.txt ", 1, TRUE)

    End Sub

    </SCRIPT>

    Except, that does nothing. Help! What do I need to do to make a click event run a program and open a file?? Do I need the fully qualified path to Notepad? Is there any other way to get the text file open?

    Thanks.
    ___________

    Add: I get a RunTime error -- "Cannot use parentheses when calling a Sub". How do I correct this? Thanks.

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    OK, I fixed my parentheses problem by changing this line:

    WshShell.Run "notepad Readme.txt ", 1, TRUE

    But... now when I click the button, I get an error here:

    Error: Object required: 'WScript'

    at this line:

    Set WshShell = WScript.CreateObject("WScript.Shell")

    OK, I have used WScript many times in my registry editing scripts and I have never had a problem. This is the first time I have tried to use "WshShell.Run" -- and so far it has not been fun...

    WHAT am I doing wrong now?

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using VBS to open a program and file.

    What security zone is the page in, and what are the applicable settings for (1) running and (2) scripting ActiveX controls? If you aren't set for at least Prompt, it should fail (with varying degrees of drama).

    Edited to add: WScript is almost certainly considered "not safe for scripting."

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    Agreed. But this is in the "My Computer" zone and I was hoping I could still get away with using it in this zone. Perhaps you are right -- this just may be impossible to do in HTML. <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

    What if I link it to a external .vbs file? I suspect the same restrictions may apply... <img src=/S/frown.gif border=0 alt=frown width=15 height=15>

    I was trying to avoid having to do this as an Executable, but perhaps I have no other choice...
    ___________________

    Is there any other way I can use VBS to run a program or open a file with a program??
    _________________________________________________

    Just so you may understand, my project is to create an HTML page with a background image and three buttons. The buttons should:

    1) Open another .html page.
    2) Run a slide show executable program.
    3) Open a folder in Windows Explorer.

    Should be simple, eh? I think opening another .html page is easy, but how do I get a button to RUN a program if I can't use WshShell.Run? And how can I get Explorer to open a given folder?

    Any advice is GREATLY appreciated.

  5. #5
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    Nope. It is not a problem with 'restricted' functions. I needed to change this line:

    Set WshShell = CreateObject("Wscript.Shell")

    ... and it works. Sort of...

    I am unable to get logical addressing to find the correct readme.txt file in the above example. How do I do that??

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using VBS to open a program and file.

    Of course. Duh!

    So now you want to open a file that's in a "relative path" - relative to the web page that contains the link? I recently posted some JavaScript that could return that path (related to the location object, I think), but VBScript... I'm not sure. You might have to dive into the IE DOM (document object model).

  7. #7
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    Do I assume that because the script is in IE I don't need to use the "WScript." prefix?? I still don't understand why I sometimes need it (raw .vbs files) and other times not (VB).

    I believe the JavaScript would be something like this:

    document.all.location.value = win.document.URL

    That might give me the path to page... but I have to find the equivalent coding for VBS. Still more work to do.

    Thanks.

  8. #8
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    Hmmm... I am not sure I can get that in VBS.

    It looks like I need something like the "locationURL" property of the "WebBrowser" control or the "Internet Explorer" control.

    Alternetively, the "Path" property of the Internet Explorer control may be even more appropriate -- but I am not sure.

    Can I access these controls in VBS or just in VB???
    ______________

    ADD:

    I just found out that I can get the path using this:

    window.location.pathname

    Why is that so hard to find??!!??

    Now, can I get just the DRIVE LETTER?? Or, can I truncate the data just to the point of the Drive Letter?? Thanks.

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using VBS to open a program and file.

    > Why is that so hard to find??!!??

    <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> I felt the same when I finally found it in my JavaScript book.

    > Now, can I get just the DRIVE LETTER?? Or, can I truncate the data just to the point of the Drive Letter??

    Uh-oh, the page path is a URL from the server's web root, which might not be the same as a servershare path to which you can map a drive letter. Can we trick Notepad into opening a file from a URL?? I can't seem to do it. Rats. ASP can calculate the server path, but that's probably overkill here. Hopefully I'm missing something simple.

  10. #10
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    To finalize this, here is the reorganized script to do it all. This allows you to stick an HTML file on a CDR disk. In the HTML page you can stick a button (cmdRunSlides) that will run a program file on the same disk -- regardless of what drive letter the CD player has been assigned by the user's computer.
    --------------------------------------------

    <SCRIPT Language=VBScript>

    Sub cmdRunSlides_onclick

    ' List variables
    Dim oFileSys, oWshShell, sRawPath, sFolderPath, sDriveName

    ' Create objects
    Set oFileSys = CreateObject("Scripting.FileSystemObject")
    Set oWshShell = CreateObject("WScript.Shell")

    ' Define paths to get drive letter
    sRawPath = window.location.pathname
    sFolderPath = replace(Mid(sRawPath, 2, InStrRev(sRawPath, "") -1), "percent20", " ")
    sDriveName = oFileSys.GetDriveName(sFolderPath)

    ' Run program in specified folder
    oWshShell.Run(sDriveName + "FilesSlideShow.exe")

    ' Release objects
    Set oFileSys = Nothing
    Set oWshShell = Nothing

    End Sub

    </SCRIPT>
    -----------------------------------------

    Note: as above, I just realized I cannot post a "percent sign" followed by a "20" on this board -- it simply makes it a blank space. Furthermore, "%" is not printed as a percent sign either... So, percent20 above means a "percent sign" followed directly by "20".

  11. #11
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    For anyone's future reference:
    --------------------------------------------------------

    '1) Display raw path to the HTML file:

    Sub cmdShowPath_onclick

    Dim test
    test = window.location.pathname
    msgbox test

    End Sub

    '---------------------------------------------------------

    '2) Display cleaned-up path to the HTML file (without the file name):

    Sub cmdShowCleanPath_onclick

    Dim tmp1, tmp2
    tmp1 = window.location.pathname
    tmp2 = replace(Mid(tmp1, 2, InStrRev(tmp1, "") - 1), "percent20 ", " ")
    msgbox tmp2

    End Sub

    '---------------------------------------------------------

    '3) Display Parent Folder:


    Sub cmdShowParentFolder_onclick

    Dim tmp1, tmp2
    tmp1 = window.location.pathname
    tmp2 = replace(Mid(tmp1, 2, InStrRev(tmp1, "") - 1), "percent20 ", " ")

    Dim fso, drive
    Set fso = CreateObject("Scripting.FileSystemObject")
    drive = fso.GetParentFolderName(tmp2)
    msgbox drive

    End Sub

    '---------------------------------------------------------

    '4) Display ReadMe.txt file in same Folder as HTML file

    Sub cmdOpenReadMe_onclick

    Dim tmp1, tmp2
    tmp1 = window.location.pathname
    tmp2 = replace(Mid(tmp1, 2, InStrRev(tmp1, "") - 1), "percent20 ", " ")

    Dim WshShell, oExec
    Set WshShell = CreateObject("WScript.Shell")
    Set oExec = WshShell.Exec("notepad """ + tmp2 + "Readme.txt""")

    End Sub

    '---------------------------------------------------------

    Buttons to add into HTML file to trigger the above events:




    <INPUT Type = "Button" Name = "cmdShowPath" Value = "Show Path to File"></p>



    <INPUT Type = "Button" Name = "cmdShowCleanPath" Value = "Show Clean Path to File"></p>



    <INPUT Type = "Button" Name = "cmdShowParentFolder" Value = "Show Parent Folder"></p>



    <INPUT Type = "Button" Name = "cmdOpenReadMe" Value = "Open Readme Text"></p>
    __________________________

    Note: as above, I just realized I cannot post a "percent sign" followed by a "20" on this board -- it simply makes it a blank space. Furthermore, "%" is not printed as a percent sign either... So, percent20 above means a "percent sign" followed directly by "20".

  12. #12
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Long Beach, California, USA
    Posts
    1,912
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Using VBS to open a program and file.

    Even if I search the Internet, it is hard to find something this simple...

    Remember, this is all on the "Local Machine" -- My Computer. The URL is truly a path. If I add a fourth button and make it show me the path, the image below is what I get -- the drive letter included.

    Here is the code for the new button:

    Sub cmdShowPath_onclick

    Dim test
    test = window.location.pathname
    msgbox test

    End Sub

    <INPUT Type = "Button" Name = "cmdShowPath" Value = "Show Path to File">
    _______________________________

    When I asked someone else about extracting the folder, I was given this code:

    Dim tmp1, tmp2
    tmp1 = window.location.pathname
    tmp2 = replace(Mid(tmp1, 2, InStrRev(tmp1, "") - 1), "percent20", " ")
    msgbox tmp2

    Dim WshShell, oExec
    Set WshShell = CreateObject("WScript.Shell")
    Set oExec = WshShell.Exec("notepad """ + tmp2 + "test.txt""")


    This will create the path and open notepad with a file named "test.txt" in the same folder as your HTML page...
    ______________________

    And... it works!! <img src=/S/smile.gif border=0 alt=smile width=15 height=15> Cool.
    _______________________________

    Note: I just realized I cannot post a "percent sign" followed by a "20" on this board -- it simply makes it a blank space. Furthermore, "%" is not printed as a percent sign either... So, percent20 above means a "percent sign" followed directly by "20".
    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •