Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using ShellExecute to Open Explorer from VB - not working in Windows XP

Posted on 2004-08-29
6
Medium Priority
?
1,148 Views
Last Modified: 2008-01-09
I have a program which I wrote and tested some time ago on all operating system except WinNT, Win2000 and Win XP. However, I have just started using it on Win XP and have come across a problem. The program uses the ShellExecute API function to open an Explorer window at a particular folder, however, this does not work in Windows XP!

My code is as follows:

URL = MessageFilesLocation
ingReturn = ShellExecute(fMainForm.hWnd, vbNullString, URL, vbNullString, "c:\", SW_SHOWNORMAL)

where MessageFilesLocation is the full path to the folder containing the required files. In all systems tested previously this worked perfectly, but Windows XP just sits there and looks at you without doing anything! I would appreciate any suggestions as to how to get this to work in XP/

Thanks.
0
Comment
Question by:cbitservices
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 252 total points
ID: 11924939
Try this:

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 Function StartDoc(ByVal FileName As String, Optional CommandLine As String = "") As Long
    StartDoc = ShellExecute(0&, "Open", FileName, CommandLine, vbNullString, 1)
End Function

try like:

startDoc URL

* Tested working on WinXP Pro Service Pack 1

cheers
0
 
LVL 3

Assisted Solution

by:realrael
realrael earned 248 total points
ID: 11924961

using the Shell function might be a more general way of implementing it on any windows platform. less complex too. - rael

error = Shell("explorer c:\temp", vbNormalFocus)
0
 
LVL 1

Author Comment

by:cbitservices
ID: 11925047
No. I have tried both suggestions. I should emphasise again that I am trying to open a FOLDER in Explorer, not find or open a file. If I specify a filename it works fine and opens the file and if I specify an Internet URLit opens Internet Explorer at that website. In previous versions of word just providing a path to a folder to the ShellExecute function causes Explorer to open up at the specified folder, but this doesn't hapen with XP. Using Shell just generates an error message that the "File Cannot be found"!, whilst ShellExecute seems to do absolutely nothing.

ryancys; you say you have tested your solution; but did you test it with a the full path to a folder, not including a filename? If so, then I don't understand why it doesn't work here! Thanks.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 11925060
OK. I found the problem! It was just an error in the path being passed to the function! Thanks anyway guys.
0
 
LVL 1

Author Comment

by:cbitservices
ID: 11925069
ryancys - your answer was technically perfect, even though my code was also perfect and working correctly once the correct path was passed to it! It turned out that the reason it wasn't opening the folder was the fact that the folder didn't exist!!! The program was assuming that the folder had been created, but on this one instance, due to rare and particular circumstances, it hadn't! The fact that the Shell function produced an error message, which was actually quite accurate, means that realrael's contribution must also be considered useful; even though I didn't pick up on it at the time! I quickly accepted ryancys's comment as the answer just to close the question, but on reflection I feel you both deserve to share the points. i will therefore request that the points are split equally between you; I hope you both agree that this is the fairest solution.Thanks again for your prompt replies and your considered thoughts.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 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