+ Reply to Thread
Results 1 to 11 of 11

Open a non xls file (emulate the double-click from Windows Explore

  1. #1
    Budget Programmer
    Guest

    Open a non xls file (emulate the double-click from Windows Explore

    Hello,
    From within my Excel VBA I want to run another file (which will in-turn
    start the associated application) when certain conditions exist.
    The Shell statement works on the EXE. I need something similar in concept
    to work on the actual data file (".anx" and others).
    It would be the equivalent of double-clicking on a data file in Windows
    Explorer.
    Many Thanks for your help.
    --
    Programmer on Budget

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Budget,

    You need to use the ShellExecute API call. Copy this code and paste it into a VBA code module in your project. This will launch any registered file type's associated application.

    Please Login or Register  to view this content.
    Example:
    OpenProgram "C:\MyFile.avi", ShowMinimized

    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 07-22-2006 at 02:13 AM.

  3. #3
    Ron de Bruin
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Explore

    Or use

    ActiveWorkbook.FollowHyperlink "C:\Test.pdf"



    --
    Regards Ron de Bruin
    http://www.rondebruin.nl



    "Leith Ross" <[email protected]> wrote in message
    news:[email protected]...
    >
    > Hello Budget,
    >
    > You need to use the ShellExecute API call. Copy this code and paste it
    > into a VBA code module in your project. This will launch any registered
    > file type's associated application.
    >
    >
    > Code:
    > --------------------
    >
    > Public Const ShowNormal As Long = 1
    > Public Const ShowMinimized As Long = 2
    >
    > Public 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
    >
    > Public Sub OpenProgram(File_To_Open As String, Show_How As Long)
    > Dim RetVal
    >
    > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString, vbNullString, Show_How)
    >
    > End Sub
    >
    > --------------------
    >
    >
    > Example:
    > OpenProgram "C:\MyFile.avi", ShowMinimized
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
    > View this thread: http://www.excelforum.com/showthread...hreadid=563916
    >




  4. #4
    Chip Pearson
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Explore


    > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString,
    > vbNullString, Show_How)


    The "open" program isn't available on all operating systems, XP
    for example. I think it is a Win 9x command.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com




    "Leith Ross"
    <[email protected]> wrote
    in message
    news:[email protected]...
    >
    > Hello Budget,
    >
    > You need to use the ShellExecute API call. Copy this code and
    > paste it
    > into a VBA code module in your project. This will launch any
    > registered
    > file type's associated application.
    >
    >
    > Code:
    > --------------------
    >
    > Public Const ShowNormal As Long = 1
    > Public Const ShowMinimized As Long = 2
    >
    > Public 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
    >
    > Public Sub OpenProgram(File_To_Open As String, Show_How As
    > Long)
    > Dim RetVal
    >
    > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString,
    > vbNullString, Show_How)
    >
    > End Sub
    >
    > --------------------
    >
    >
    > Example:
    > OpenProgram "C:\MyFile.avi", ShowMinimized
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile:
    > http://www.excelforum.com/member.php...o&userid=18465
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=563916
    >




  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    I am running Windows XP Home Edition and the macro works fine.

    Sincerely,
    Leith Ross

  6. #6
    Peter T
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Explore

    > The "open" program isn't available on all operating systems,

    I didn't know that but I use vbNullString in lieu of "open".

    Regards,
    Peter T

    "Chip Pearson" <[email protected]> wrote in message
    news:[email protected]...
    >
    > > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString,
    > > vbNullString, Show_How)

    >
    > The "open" program isn't available on all operating systems, XP
    > for example. I think it is a Win 9x command.
    >
    >
    > --
    > Cordially,
    > Chip Pearson
    > Microsoft MVP - Excel
    > Pearson Software Consulting, LLC
    > www.cpearson.com
    >
    >
    >
    >
    > "Leith Ross"
    > <[email protected]> wrote
    > in message
    > news:[email protected]...
    > >
    > > Hello Budget,
    > >
    > > You need to use the ShellExecute API call. Copy this code and
    > > paste it
    > > into a VBA code module in your project. This will launch any
    > > registered
    > > file type's associated application.
    > >
    > >
    > > Code:
    > > --------------------
    > >
    > > Public Const ShowNormal As Long = 1
    > > Public Const ShowMinimized As Long = 2
    > >
    > > Public 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
    > >
    > > Public Sub OpenProgram(File_To_Open As String, Show_How As
    > > Long)
    > > Dim RetVal
    > >
    > > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString,
    > > vbNullString, Show_How)
    > >
    > > End Sub
    > >
    > > --------------------
    > >
    > >
    > > Example:
    > > OpenProgram "C:\MyFile.avi", ShowMinimized
    > >
    > > Sincerely,
    > > Leith Ross
    > >
    > >
    > > --
    > > Leith Ross
    > > ------------------------------------------------------------------------
    > > Leith Ross's Profile:
    > > http://www.excelforum.com/member.php...o&userid=18465
    > > View this thread:
    > > http://www.excelforum.com/showthread...hreadid=563916
    > >

    >
    >




  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Using vbNullString for the lpOperation parameter will invoke the default verb for the file. The default in most instances is open.

    Sincerely,
    Leith Ross

  8. #8
    Peter T
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Explore

    Double clicking a file in explorer invokes the default action, so I suppose
    strictly speaking what the OP asked for.

    My default for text files is to open in my preferred text editor, though I
    still have an "open" setting to open in Notepad (gives me both options if I
    right click a file).

    IOW if I send "open" and a textfile to the API it opens in Notepad, but
    vbNullString opens in my preferred editor.

    Subject to "open" always available (you & Chip seem to be in slight
    disagreement, I don't know), on balance wouldn't the default be the safer
    choice.

    Regards,
    Peter T

    "Leith Ross" <[email protected]> wrote
    in message news:[email protected]...
    >
    > Using vbNullString for the lpOperation parameter will invoke the default
    > verb for the file. The default in most instances is open.
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile:

    http://www.excelforum.com/member.php...o&userid=18465
    > View this thread: http://www.excelforum.com/showthread...hreadid=563916
    >




  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Peter,

    The reason "open" launches NotePad is this is the registered "open' verb in the registry. When you use the default, the system again looks to the registry, but in a different place if no verb is given, to select the user's preferred program, if one exists for file type specified. In the end, it really is just a matter of personal choice.

    Sincerely,
    Leith Ross

  10. #10
    Peter T
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Explore

    Fair point :-)

    Regards,
    Peter T

    "Leith Ross" <[email protected]> wrote
    in message news:[email protected]...
    >
    > Peter,
    >
    > The reason "open" launches NotePad is this is the registered "open'
    > verb in the registry. When you use the default, the system again looks
    > to the registry, but in a different place if no verb is given, to
    > select the user's preferred program, if one exists for file type
    > specified. In the end, it really is just a matter of personal choice.
    >
    > Sincerely,
    > Leith Ross
    >
    >
    > --
    > Leith Ross
    > ------------------------------------------------------------------------
    > Leith Ross's Profile:

    http://www.excelforum.com/member.php...o&userid=18465
    > View this thread: http://www.excelforum.com/showthread...hreadid=563916
    >




  11. #11
    Budget Programmer
    Guest

    Re: Open a non xls file (emulate the double-click from Windows Exp

    Hello,
    The ActiveWorkbook.FollowHyperlink worked perfectly. Thanks.
    --
    Programmer on Budget


    "Ron de Bruin" wrote:

    > Or use
    >
    > ActiveWorkbook.FollowHyperlink "C:\Test.pdf"
    >
    >
    >
    > --
    > Regards Ron de Bruin
    > http://www.rondebruin.nl
    >
    >
    >
    > "Leith Ross" <[email protected]> wrote in message
    > news:[email protected]...
    > >
    > > Hello Budget,
    > >
    > > You need to use the ShellExecute API call. Copy this code and paste it
    > > into a VBA code module in your project. This will launch any registered
    > > file type's associated application.
    > >
    > >
    > > Code:
    > > --------------------
    > >
    > > Public Const ShowNormal As Long = 1
    > > Public Const ShowMinimized As Long = 2
    > >
    > > Public 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
    > >
    > > Public Sub OpenProgram(File_To_Open As String, Show_How As Long)
    > > Dim RetVal
    > >
    > > RetVal = ShellExecute(0&, "open", File_To_Open, vbNullString, vbNullString, Show_How)
    > >
    > > End Sub
    > >
    > > --------------------
    > >
    > >
    > > Example:
    > > OpenProgram "C:\MyFile.avi", ShowMinimized
    > >
    > > Sincerely,
    > > Leith Ross
    > >
    > >
    > > --
    > > Leith Ross
    > > ------------------------------------------------------------------------
    > > Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
    > > View this thread: http://www.excelforum.com/showthread...hreadid=563916
    > >

    >
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1