Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pause VBA code while opening and closing Windows Explorer 1

Status
Not open for further replies.

Tipster007

Technical User
Nov 9, 2014
8
AU
Hi all,
I have a button on a form to open Windows Explorer and select a file variable set in the code. I want to pause the execution of the code while the Explorer window is open.
I am using WScript.Shell as below but the code continues to run, Explorer opens and correctly selects the file. It works fine when opening Paint or Calculator but won't work for Win Explorer.
I need Windows explorer to open not a file dialog box.
I am using Access 2013 and Windows 8.1. Code below courtesy of many VBA experts.
Thank you in advance smile

Dim strImgPath As String
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 3
Dim errorCode As Integer

strImgPath = Me.txtImageName
errorCode = wsh.Run("explorer.exe /Select, " & """" & strImgPath & """", windowStyle, waitOnReturn)

If errorCode = 0 Then
'Insert code here
Else
MsgBox "Program exited with error code " & errorCode & "."
End If
 
There are several ways to do that with a file dialog instead of a shelling to the OS...

One of the would be to use the Office Dialog.


There are other API calls that can be used to. I like that one unless you have a compelling reason to use something else.

As to why your code doesn't wait.... I think explorer is causing another program to run. So it is waiting on the handle for explorer to end as opposed to the program explorer ran.

I'm not familiar enough with the Windows API to know specifics but to make it work you would probably have to find all the running handles, then get the new one from explorer and then find out which other one is the new one and wait on it. Even then it could be problematic if other programs are starting for other reasons. I only mention this in case some wayward soul finds this thread later. For that wayward soul I would recommend trying to find a way to directly execute the program in question because that handle rabbit hole could take days or weeks away from your life.
 
Hi lameid, thank you for your help.
I was trying to avoid using a file dialog but as you say it may be too hard using Windows Explorer, I've spent a fair bit of time on it already.
I will try the dialog idea or I may have to rethink what I am trying to achieve.
Appreciate your help Lameid [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top