Opening explorer and getting its thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I need to open explorer and get the thread for the window. I have found
code for doing this with Notepad but this does not work with explorer. I get
a 0 for the window thread.

Can anyone help?

Thanks
 
Hi.

I don't know what code you're using to retrieve the thread for the window,
but one may use the Windows API's to return the Windows handle for a
particular application that is open, and perhaps that will help you. Please
see the following Web page for the basic code to be modified for Windows
Explorer:

http://www.mvps.org/access/api/api0007.htm

In the Select Case block, add:

Case "explore": strClassName = "ExploreWClass"

The variable lngH contains the Windows Handle to the first instance of
Windows Explorer found. If there are multiple instances of Windows Explorer
open while this code is running, then iterate through the instances and check
the Windows Caption for a match to the window you're looking for. Use the
following function to determine the Window's Caption Property from this
Window Handle:

' (Add this API function to the Declarations section of the standard module:)

Private Declare Function apiGetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) _
As Long

' (Then paste this function into the standard module:)

Private Function fGetCaption(hWnd As Long) As String
' Returns the caption of a Window
'
Dim strBuffer As String
Dim lngCount As Long
strBuffer = String$(MAX_LEN + 1, 0)
lngCount = apiGetWindowText(hWnd, strBuffer, MAX_LEN)
If lngCount > 0 Then fGetCaption = Left$(strBuffer, lngCount)
End Function

On the other hand, one may also use AppActivate to return the TaskID for a
particular window. For example:

Dim rtn As Double

rtn = Shell("C:\Program Files\Internet Explorer\iexplore.exe",1) ' Run IE.
AppActivate rtn ' Activate IE.

.. . . where rtn is the TaskID.

Older versions have Windows Explorer as a separate application, but its
integrated into Windows XP, so you may not be able to run it in Shell on your
workstation.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Hi

Thanks for that I thought that I had it last night. But when I came to it
this morning (and put it into the rest of the code) I got the same problem
again

my code is as below

Option Compare Database

'***************** Code Start ***************
'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_NORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10
Private Const SW_MAX = 10

Private Declare Function apiFindWindow Lib "user32" Alias _
"FindWindowA" (ByVal strClass As String, _
ByVal lpWindow As String) As Long

Private Declare Function apiSendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal _
wParam As Long, lParam As Long) As Long

Private Declare Function apiSetForegroundWindow Lib "user32" Alias _
"SetForegroundWindow" (ByVal hWnd As Long) As Long

Private Declare Function apiShowWindow Lib "user32" Alias _
"ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function apiIsIconic Lib "user32" Alias _
"IsIconic" (ByVal hWnd As Long) As Long
Private Declare Function apiGetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) _
As Long
Private Declare Function apiPostMessage _
Lib "user32" Alias "PostMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Private Const WM_CLOSE = &H10

'-----------------------------------------------------------------
'Connect to c:\simon
'-----------------------------------------------------------------

Function MapDrives()
Dim ws As Object
Dim returncode As String

Set ws = CreateObject("WScript.Shell")

returncode = ws.Run("explorer.exe ""c:\simon\""")


Do
Loop Until ws.AppActivate("simon")

fIsAppRunning ("Explore")




End Function


Function fIsAppRunning(ByVal strAppName As String, _
Optional fActivate As Boolean) As Boolean
Dim lngH As Long, strClassName As String
Dim lngX As Long, lngTmp As Long
Const WM_USER = 1024
Dim lngRet As Long

On Local Error GoTo fIsAppRunning_Err
fIsAppRunning = False
Select Case LCase$(strAppName)
Case "excel": strClassName = "XLMain"
Case "word": strClassName = "OpusApp"
Case "access": strClassName = "OMain"
Case "powerpoint95": strClassName = "PP7FrameClass"
Case "powerpoint97": strClassName = "PP97FrameClass"
Case "notepad": strClassName = "NOTEPAD"
Case "paintbrush": strClassName = "pbParent"
Case "wordpad": strClassName = "WordPadClass"
Case "explore": strClassName = "ExploreWClass"
Case Else: strClassName = vbNullString
End Select

If strClassName = "" Then
lngH = apiFindWindow(vbNullString, strAppName)
Else
lngH = apiFindWindow(strClassName, vbNullString)
End If
If lngH <> 0 Then
apiSendMessage lngH, WM_USER + 18, 0, 0
lngX = apiIsIconic(lngH)
If lngX <> 0 Then
lngTmp = apiShowWindow(lngH, SW_SHOWNORMAL)
End If
If fActivate Then
lngTmp = apiSetForegroundWindow(lngH)
End If
fIsAppRunning = True
End If

MsgBox (lngH)

' lngRet = apiPostMessage(mWnd, WM_CLOSE, 0, ByVal 0&)

fIsAppRunning_Exit:
Exit Function
fIsAppRunning_Err:
fIsAppRunning = False
Resume fIsAppRunning_Exit
End Function
'******************** Code End ****************

Private Function fGetCaption(hWnd As Long) As String
' Returns the caption of a Window
'
Dim strBuffer As String
Dim lngCount As Long
strBuffer = String$(MAX_LEN + 1, 0)
lngCount = apiGetWindowText(hWnd, strBuffer, MAX_LEN)
If lngCount > 0 Then fGetCaption = Left$(strBuffer, lngCount)


End Function

Function simon()

MapDrives

fIsAppRunning ("Explore")

End Function



If I run simon "MsgBox (lngH)" =0

If I open explorer via start programs I get a value returned here, but if I
open explorer via code I always get a 0

Any ideass?
 
What exactly are you hoping to do? In other words, why are you opening
Explorer?

There may well be other ways of achieving what you're trying to do.
 
I have a module in access that copies files accross my network.

They have to go to to different network drives which are not mapped.

I use explorer to in effect log onto these drives. There are several
different people that use the program and they all must use their own user
name and password to log onto the network drive.

The problem I seem to be having is when I execute explorer in code the
window itself does not seem to have a property of being a explorer window.

Could open explorer run a timer and then send Alt F4 but...... I now really
want to find out how to do it this way


Simon
 
Hi.

Perhaps you've cut out a bunch of code to make it simpler for illustrative
purposes, but you've got me wondering why you're running Windows Script in a
VBA module, why you've got fGetCaption( ) when it's never called, why you've
got MapDrives( ) running an empty loop until it AppActivates what appears to
be a user-defined function, why you're missing Option Explicit, . . . why it
looks like you went on an Internet Easter Egg hunt and brought back a
basketful of code in the hopes that it'll hatch into the goose that lays the
golden eggs.

This is going to sound harsh, but you've posted your question in a newsgroup
for intermediate through expert Access users, not for beginners. The
questioner is expected to know certain basic things about programming,
because one cannot learn how to program from suggestions and code offered in
a few newsgroup posts. And if the questioner doesn't understand -- or know
how to use -- the code offered, he could end up with a huge disaster that is
unrecoverable. The blame will go to those who made the effort to assist,
because "that code didn't work" -- when the code is just a generic working
example that needs modification for specific uses.

Let's take a step back and determine the ultimate goal. Do you want to open
Windows Explorer for the user to open an application that needs to manipulate
a particular file? Perhaps a hyperlink will suffice. Perhaps the Open File
dialog window will suffice. Perhaps not.

Please let us know what you are trying to accomplish and perhaps we can come
up with suggestions for the easiest and quickest "how" for you.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Hi

Harsh but to an extent true. I have cut a fair bit of code out at does not
relate to the problem which is why there is the empty loop in there.

I am rusty on my programming as I have not done any to any extent for the
last couple of years.

I have a routine that gets files from one network drive and copies them to
another. The problem I have is that several people use this access database
and they all have to log onto these other drives using their own user name
and password. There are 3 different network drives that we need to access
and they are all on different workgroups or domains.

I use explorer to in effect log onto the drives so that I can move the files
around. The passwords are all changed on a monthly basis.

I could use a timer to delay the program until the explorer window gets the
focus and then send alt F4 but the problem I got there was it could take 30
sec plus for explorer to finish loading the directory so if I sent the alt F4
keys to early it was closing down another application.

I do appreciate your time and effort on this


Simon
 
Hi, Simon.

It's generally a bad idea to map network drives for users. And unless the
Windows Network Administrator has set up the network so that users of one
domain can log into that domain on their workstations and, during the same
session, authenticate to servers located in other domains on the network, the
users will have to log out of one domain in order to log into the second
domain to access files on a server located in the second domain. However, it
sounds like you are able to do this manually -- which means that it's
possible to do it programmatically, too.

If your Network Administrator has made your life easy, then just using the
UNC path will allow you to copy the files from your own domain. The
following example procedure will copy Word files from the server to the local
workstation if the files don't already exist on the workstation.

Public Sub copyDocFiles()

On Error GoTo ErrHandler

Dim fso As New FileSystemObject
Dim fl As File
Dim fls As Files
Dim sSrcDir As String
Dim sTargetDir As String
Dim Fldr As Scripting.Folder

sSrcDir = "\\ServerName\SharedDirectory\"
sTargetDir = "C:\LocalDirectory\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set Fldr = fso.GetFolder(sSrcDir)
Set fls = Fldr.Files

For Each fl In fls
If Right(fl.Name, 4) = ".doc" Then
If (Dir(sTargetDir & fl.Name) = "") Then
FileCopy sSrcDir & fl.Name, sTargetDir & fl.Name
End If
End If
Next

CleanUp:

Set fl = Nothing
Set fls = Nothing
Set Fldr = Nothing
Set fso = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in copyDocFiles( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

.. . . where \\ServerName\SharedDirectory is the shared network (UNC)
directory on the server, and C:\LocalDirectory is the user's local directory
on his own workstation. If the Network Administrator has made your job more
difficult, then please see the following Web pages for example code to
connect network drives:

http://vbnet.mvps.org/index.html?code/network/netconnect.htm

http://www.vbusers.com/code/codeget.asp?ThreadID=248&PostID=1&NumReplies=0

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Hi

Thanks for the code. This is very similiar (more tidy) to the code I am
already running. The problem I have is I am told the folder does not exist,
If I have not gone to the drive using explorer

What I have to do is use explorer to go to the directory on the network
drive. I am then prompted for my user name and password (different to the
one that I log onto my PC with). I can then see the files in the directory.

I can then close explorer and run the code which gets the 100 odd files and
copies/imports them.

I am very much just a user where networks are concerned so any help is useful.

Thanks


Simon
 
Hi

Thanks. I'm not certain how to use the net use command at the moment so
I'll have to go away and read up on it thanks

Simon
 
Hi, Simon.
The problem I have is I am told the folder does not exist,
If I have not gone to the drive using explorer

When attempting to access another domain's server, the current user's UserID
is used to authenticate. Your current UserID doesn't have Windows security
permissions to list contents of the directory above the destination
directory, so you'll get the message that this directory doesn't exist.

Windows API's give more control, but it sounds like you need something
simpler. Doug's suggestion of shelling out and using "Net Use" to connect to
the networked server is an excellent one. Since it's on a different domain,
you'll need to add the /User:OtherDomainName\MyOtherDomainUserID parameter.
(There's a password parameter, too, but don't store that in a batch file.
Let the user be prompted for it.) The syntax for "Net Use" is (watch out for
word wrap, as this is all one line):

NET USE \\ServerName\SharedDir /User:OtherDomainName\MyOtherDomainUserID

For an example of using batch files to connect to and disconnect from a
networked server using "Net Use," please see the following Web page:

http://groups.google.com/group/micr...public.access.*&rnum=3&hl=en#220fc0c0863f5359

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
Hi Gunny

Thanks for that.

I need to go and read up on "net use" but it does very much sound like it
will do the job for me

Thanks again for you input and hard work.

Simon
 
Hi

Thanks for that I got it working today. Could not test it at home as I have
no network.

Sorry I did not get back to you sooner but I have only just got in and I can
reply to a post from work.


Simon
 
Back
Top