VBA to open and CLOSE windows explorer inside a vault path

Programming and macros
User avatar
mp3-250
Posts: 637
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 712
x 348

VBA to open and CLOSE windows explorer inside a vault path

Unread post by mp3-250 »

I am trying to open a vault path from a excel macro. explorer opens as a normal window without the green folders and the pdm tabs and showing the normal yellow folders and files.

I am alredy logged into the pdm and using the standard api calls to open explorer.

Any help? what method should I use to open (and close) an explorer window pointing a vault path?


UPDATE
I was trying it from excel VBA using

Code: Select all

ThisWorkbook.FollowHyperlink strDirectory, "", False, False 
I was suggested to use instead

Code: Select all

Dim vault As EdmVault5
   Set vault = New EdmVault5
   vault.LoginAuto "VAULTNAMEHERE", 0
   Dim location As String
   Shell "C:\WINDOWS\explorer.exe """ & vault.RootFolderPath & "", vbNormalFocus
the shell indeed opens explorer correctly inside the vault, but then I need to perform another operation:
search every explorer window and close the one inside the vault.
With the code below that would not be a problem and it works for paths like c:\ or c:\windows

Code: Select all

Sub CloseExplorerWindowByPath(targetFolderPath As String)

   Dim explorerWindow As Object
   Dim shellWindows As Object

   Set explorerWindow = CreateObject("Shell.Application").Windows

   ' Loop through each Explorer window
   For Each explorerWindow In shellWindows
       ' Check if the window's document folder path matches the target path
       
       If InStr(1, explorerWindow.Document.Folder.Self.Path, targetFolderPath, vbTextCompare) > 0 Then
           ' Close the matching window
           explorerWindow.Quit
           Exit For   
       End If
   Next explorerWindow
   
End Sub

When trying that code with an explorer window opened inside the vault I get an error error 445 on explorerWindow.Document.Folder.Self.Path

The path supplyed is valid but the object does not support the path property apparently.
Testing on Win11 and Excel 2016 64 bit.
User avatar
AlexB
Posts: 502
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 269
x 447

Re: vba open windows explorer inside a vault path

Unread post by AlexB »

I'm not sure if you can create a conisio url to do this or not. My quick testing did not yield consistent results but this would be my initial approach.
https://r1132100503382-eu1-3dswym.3dexp ... dYkzZfnCZg
User avatar
mp3-250
Posts: 637
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 712
x 348

Re: vba open windows explorer inside a vault path

Unread post by mp3-250 »

AlexB wrote: Mon Jun 24, 2024 8:34 am I'm not sure if you can create a conisio url to do this or not. My quick testing did not yield consistent results but this would be my initial approach.
https://r1132100503382-eu1-3dswym.3dexp ... dYkzZfnCZg
I updated the OP.
My real problem now is how to identify the opened explorer windows and close them.
When opening a certain path inside the vault the explorer window is no more able to identify its selfpath apparently.
User avatar
JSculley
Posts: 648
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 883

Re: vba open windows explorer inside a vault path

Unread post by JSculley »

mp3-250 wrote: Tue Jun 25, 2024 4:55 pm I updated the OP.
My real problem now is how to identify the opened explorer windows and close them.
When opening a certain path inside the vault the explorer window is no more able to identify its selfpath apparently.
As noted in my SWYMp reply:

Use the Windows API for this. Put this at the top of your Excel code:

Code: Select all

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal maxCount As Integer) As Integer
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
Private Const WM_CLOSE = &H10
Then to go through all the Explorer windows and find the ones pointing into the vault, do this:

Code: Select all

 Dim wh As Long
   Do While True
       wh = FindWindow("CabinetWClass", vbNullString)
       If wh = 0 Then Exit Do
       Dim captionLength As Long
       captionLength = GetWindowTextLength(wh) + 1 'Allow for null terminator
       Dim caption As String
       caption = String(captionLength, 0)
       GetWindowText wh, caption, captionLength
       caption = Left(caption, Len(caption) - 1) 'Remove null terminator
       If InStr(1, caption, vault.RootFolderPath) = 0 Then Exit Do
       PostMessage wh, WM_CLOSE, 0, 0
   Loop
User avatar
mp3-250
Posts: 637
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 712
x 348

Re: vba open windows explorer inside a vault path

Unread post by mp3-250 »

@JSculley Thank you very much!

I tested on a Win11 machine at work and tried to debug after my excel just froze.

Sometimes it works and close the opened explorer windows, but sometimes the loop never ends and excel stops responding.
I put a breakpoint in the loop and I noticed the same wh value with the same caption is detected over and over.
No windows opened, empty desktop, but I have 3 explorer.exe processes active.

I put 5000ms delay between opening and closing the windows.
JSculley wrote: Wed Jun 26, 2024 8:44 am As noted in my SWYMp reply:

Use the Windows API for this. Put this at the top of your Excel code:

Code: Select all

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal maxCount As Integer) As Integer
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Integer
Private Const WM_CLOSE = &H10
Then to go through all the Explorer windows and find the ones pointing into the vault, do this:

Code: Select all

 Dim wh As Long
   Do While True
       wh = FindWindow("CabinetWClass", vbNullString)
       If wh = 0 Then Exit Do
       Dim captionLength As Long
       captionLength = GetWindowTextLength(wh) + 1 'Allow for null terminator
       Dim caption As String
       caption = String(captionLength, 0)
       GetWindowText wh, caption, captionLength
       caption = Left(caption, Len(caption) - 1) 'Remove null terminator
       If InStr(1, caption, vault.RootFolderPath) = 0 Then Exit Do
       PostMessage wh, WM_CLOSE, 0, 0
   Loop
Post Reply