Results 1 to 8 of 8

Thread: [RESOLVED] Having a very difficult time using Shell to open up a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Resolved [RESOLVED] Having a very difficult time using Shell to open up a file

    Alright, I spent 4 hours yesterday trying to figure this out until I past out from being so tired. I have tried to do what I am about to explain in EVERY way I can think of or have found, and it's something simple and I use to be able to do it easily but I completely forgot how to do it. It's been 1 year since I have done any programming and it wasn't much back then.

    Anyway, all I am trying to do is simply launch a freaking PDF file. I need it to be in the same path in whatever the folder will be on so i'm using "app.path". My suspicion is that I am messing up the "quotes" in my lines of code.

    Here is one of the lines of code I have been working with..here is the last one (I have tried it with different quotes and other things like "()" or using "Call"):

    VB Code:
    1. Shell app.path & "\book.pdf", vbnormalfocus

    I have tried all of them like

    VB Code:
    1. Shell app.path & "\book.pdf", 1

    VB Code:
    1. Call Shell (app.path & "\book.pdf")

    ..Ecetera, and it keeps giving me a runtime 5 error, invalid procedure.

    I have looked at other threads on here and tried those methods...I get the same error or I just get nothing. I have also tried shellexcute. Anyway, what am I doing wrong?


    edit:

    is my code showing? it's not for me, I used the vbcode tags.
    Last edited by si_the_geek; Jun 16th, 2011 at 01:09 PM. Reason: corrected issue with tags

  2. #2
    Lively Member
    Join Date
    Apr 2011
    Posts
    95

    Re: Having a very difficult time using Shell to open up a file

    Here you go. Very easy to do.

    Code:
    Private Sub Command1_Click()
    Shell ("c:\windows\system32\calc.exe")
    End Sub
    That will load the windows calculator when you click the command1 button

    P.S. A quick google yielded me hundreds of variations of this code just so you know :-)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Having a very difficult time using Shell to open up a file

    Hey, I appreciate the effort, but I already know how to do that and have no problems loading things directly, however I can't load things using app.path and I can't load a PDF file.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Having a very difficult time using Shell to open up a file

    And yeah, I have tried for 4 hours using different variations.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Having a very difficult time using Shell to open up a file

    Shell is only for executable files, not documents etc. To open (or print etc) a document, use ShellExecute.

    For more information and examples, see the article How do I open a file/web-page in its default application? from our Classic VB FAQs (in the FAQ forum)

    Quote Originally Posted by EndResult View Post
    edit:

    is my code showing? it's not for me, I used the vbcode tags.
    It wasn't, but I've fixed it for you.

    It seems that you made a mistake, presumably you got the pop-up asking for an option, which should be: VB

    That will then give you this:
    [HIGHLIGHT="VB"] [/HIGHLIGHT]
    ..and then you can paste your code into the middle.

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Having a very difficult time using Shell to open up a file

    First of all why are you doing this:

    Shell app.path & "\book.pdf", vbnormalfocus

    book.pdf is a file, not an application. You need to Shell an application

    You need to do something like this:

    Code:
    Private Declare Function ShellExecute _
                                Lib "shell32.dll" _
                                Alias "ShellExecuteA" ( _
                                ByVal hwnd As Long, _
                                ByVal lpOperation As String, _
                                ByVal lpFile As String, _
                                ByVal lpParameters As String, _
                                ByVal lpDirectory As String, _
                                ByVal nShowCmd As Long) _
                                As Long
    
    Private Sub Command1_Click()
     ShellExecute 0, "open", App.Path & "\book.pdf", 0, 0, 1
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Having a very difficult time using Shell to open up a file

    Shell is only for executable files, not documents etc. To open (or print etc) a document, use ShellExecute.
    Thank you for that information it was very helpful (about Shell being only for executable), anyway that line of code you showed me did the trick, I got it.

    It wasn't, but I've fixed it for you.

    It seems that you made a mistake, presumably you got the pop-up asking for an option, which should be: VB
    Alright, I don't understand how to use those tags. When I highlight my text and click vbcode...nothing happens.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Location
    Southern California
    Posts
    88

    Re: Having a very difficult time using Shell to open up a file

    Quote Originally Posted by jmsrickland View Post
    First of all why are you doing this:

    Shell app.path & "\book.pdf", vbnormalfocus

    book.pdf is a file, not an application. You need to Shell an application

    You need to do something like this:

    Code:
    Private Declare Function ShellExecute _
                                Lib "shell32.dll" _
                                Alias "ShellExecuteA" ( _
                                ByVal hwnd As Long, _
                                ByVal lpOperation As String, _
                                ByVal lpFile As String, _
                                ByVal lpParameters As String, _
                                ByVal lpDirectory As String, _
                                ByVal nShowCmd As Long) _
                                As Long
    
    Private Sub Command1_Click()
     ShellExecute 0, "open", App.Path & "\book.pdf", 0, 0, 1
    End Sub
    Thanks, I know now, I got it, si the geek just showed me.

Posting Permissions

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



Click Here to Expand Forum to Full Width