Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on 

Open/ Close File Explorer...

Following line of code opens folder "C:\Test1"

   Shell "explorer.exe C:\Test1", vbNormalFocus

After opening "C:\Test1", I need code to close it, somthing like:

    ' If <C:\Test1> is Open Then
   '    Close "C:\Test1"
   ' Else
   '    MsgBox "C:\Test1 is not open"
   ' End If

Is this doable?

I am using Access 2003.

Thank you.
Microsoft AccessVisual Basic Classic

Avatar of undefined
Last Comment
Mike Eghtebas
Avatar of TRobinJames
TRobinJames

Could use the Call EndTask(sAppName) function under proper circumstances.
ASKER CERTIFIED SOLUTION
Avatar of TRobinJames
TRobinJames

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

TRobinJames,

Would I be able to use it in Access 2003 environment?

Thanks,

Mike
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

I have:

Private Sub cmdOpenP_Att_Click()

    If cmdOpenP_Att = 0 Then
''''''
    ' If  is Open Then
   '    Close "C:\Test1"
   ' Else
   '    MsgBox "C:\Test1 is not open"
   ' End If
'''''''
        cmdOpenP_Att.Caption = "Open C:\Test1"
    Else
        Shell "explorer.exe C:\Test1", vbNormalFocus
        cmdOpenP_Att.Caption = "Close C:\Test1"
    End If
     
End Sub

How can I link the code in your link to what I have. I coppied your code to a module in Access and it compiled okay.

What I need is a bit help to iron it out to work with what I have.

Thanks,

Mike
Avatar of TRobinJames
TRobinJames

Certainly with VBA in Access.  For better info on API use in VBA:

http://msdn.microsoft.com/en-us/library/aa165081(office.10).aspx
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

I guess as it stands it is not question of being able to use API (because the sample you gave would work okay).

The function:  "IsTaskRunning(sWindowName As String) As Boolean" in your code requires a parameter "sWindowName As String"

Would I be calling like:

IsTaskRunning("C:\Test1")

or

Dim i as integer
Private Sub cmdOpenP_Att_Click()

    If cmdOpenP_Att = 0 Then
        if IsTaskRunning("C:\Test1")
             i = EndTask("C:\Test1")
        Else
             MsgBox "C:\Test1 is not open"
        End If
        cmdOpenP_Att.Caption = "Open C:\Test1"

    Else
        Shell "explorer.exe C:\Test1", vbNormalFocus
        cmdOpenP_Att.Caption = "Close C:\Test1"
    End If
     
End Sub
 
I am testing this now,

brb
Avatar of Rey Obrero (Capricorn1)
try this sub

Sub closeWE(strFolder As String)
On Error Resume Next
    Dim shlWin As New SHDocVw.ShellWindows
    Dim winExp As SHDocVw.InternetExplorer

    For Each winExp In shlWin

        If InStr(winExp.LocationURL, strFolder) Then
              winExp.Quit
        End If
    Next

End Sub

need to add to your references Microsoft Internet Controls
to add Microsoft Internet Controls
Tools>references

> click Browse and locate
> C:\Windows\system32\SHDOCVW.dll
Avatar of TRobinJames
TRobinJames

eghtebas:
I think you will need to use the exact title of the window; on the title bar, not the appPath used in the shell statement.  Terminating is much different that opening, due to instance allowances, so you could have many C:\test1 open but to close you will need to be specific as to instance; exact title.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

TRobinJames,

You code di work changing "C:\Test1" to "Test1" in the lines marked with ***

   If cmdOpenP_Att = 0 Then
        if IsTaskRunning("Test1") then   '***
             i = EndTask("Test1")             '***
        Else
             MsgBox "C:\Test1 is not open"
        End If

capricorn1,

You always have short and easy ways of doing it. I will try and share the points if it works (becuase it is simple).

Thanks all.

Mike
Avatar of TRobinJames
TRobinJames

Keep in mind that I believe Capri's solution is only for Explorer and does require addition of reference.  TRJ code can be used for any app terminate, which you may come across in future.
isn't the question directed to windows explorer?
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

Cap,

I am not able to locate "C:\Windows\system32\SHDOCVW.dll" to test your solution.

TRobinJames,

Although I have asked this question with only explorer in mind, nonetheless, I appriciate its potential to be use elsewhere as well.
=========
Despite my intention, the first acceptable answer is from TRobinJames.

Thanks to all,

Mike
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

ASKER

I have a follow up question I will post a new question to handle it.

While both "C\Test1" and "D:\Test1" are open and the option group optDataTargetFolders reads 1, how can I change my code for the code to ignore the open file in D rive with the same caption and deal only with the one in C drive.

FYI, optDataTargetFolders on a form looks like:

- Target Folder ----------
|  o   C Drive                 |      <---  optDataTargetFolders  = 1
|  o   D Drive                 |      <---  optDataTargetFolders  = 2
------------------------------

I have a code like:

Private Sub cmdOpenCloseDraft_Click()

    Dim strCaption As String
    Dim strFolder As String
    Dim strTemp As String
    Dim M As Integer
       
    If optDataTargetFolders = 1 Then  '  "C drive"
        strTemp = "Test1"
        strFolder = "C:\Test1"
    Else                                               '  "D Drive"
        strTemp = "Test1"
        strFolder = "D:\Test1"
    End If

    If IsTaskRunning(strTemp) Then
        M = EndTask(strTemp)
    Else
        Shell "explorer.exe " & strFolder, vbNormalFocus
    End If

End Sub

I will post a link despite the fact the new EE site notifies you (I guess).

Thanks,

Mike
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo