Solved

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

Posted on 2004-08-29
6
1,145 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
  • 3
6 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 63 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 62 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

679 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