• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need VBA code to close specific opened folder seen in taskbar. [SOLVED]

Hi All Excel Ninja & Experts,


I need your help to know the VBA code to close specific opened folder seen in taskbar.


I have searched a lot but could not find exact code which can help me to loop through all the opened folders in taskbar and then can close specific folder window like we do to close all the opened workbooks except this workbook.


On some websites, I found that we can loop in "Task" entries which is there in task manager and for that they suggested to define variable like:


Dim myFld as Task


And then used the For Each loop like below

[pre]
Code:
For Each myFld in Tasks
'VBA code
myFld.Close
[/pre]

Next


However when I am defining the variable as Task its showing error as its not there in VBA library.


Please help me on this problem.


I hope you can understand my query.


Please let me know if you need more details.


Thanks for all of your kind support.


Thanks & Regards,

Anupam Tiwari
 

shrivallabha

Excel Ninja
Task object resides in Word object library.


Here's one thread from which you can adopt the code to suit your requirement.

http://chandoo.org/forums/topic/how-to-play-a-song-with-the-starting-of-excel-file
 
Hi shrivallabha


Thanks a lot for your help.


I have gone through the above link and learnt new things which are usefull for me however still I could not find anything to close opened folder.


I am also putting my efforts to find the solution and also looking forward to get the solution from our experts.


Thanks shrivallabha


Thanks & Regards,

Anupam Tiwari
 

shrivallabha

Excel Ninja
In my 2nd last post, I have posted a code for closing window for winamp which you could have modified to suit your usage.


See if the following code suits your requirement:

Code:
Public Sub CloseFolder()
Dim oWord As Object
Dim tsk As Object
Dim bCreated As Boolean

'See if any instance of word is running then use it
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
On Error GoTo 0

'We know that no instance of word is running then
'lets create one invisible instance and run the rest of code
If oWord Is Nothing Then
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
bCreated = True
End If

'Loop through all tasks
For Each tsk In oWord.Tasks
'Here you have to specify the folder name you wish to close
'e.g. following code will close explorer with folder name "songs"
'as current folder
If tsk.Name = "songs" Then
tsk.Close
End If
Next tsk

'If we have created instance then lets close it again
If bCreated Then oWord.Quit

End Sub
 
Last edited:
Hi shrivallabha


Thanks a lot for your help. Your code is working fine for me.


Really appreciate your help.


Thanks & Regards,

Anupam Tiwari
 

huha818

New Member
In my 2nd last post, I have posted a code for closing window for winamp which you could have modified to suit your usage.


See if the following code suits your requirement:

[pre]
Code:
Public Sub CloseFolder()
Dim oWord As Object
Dim tsk As Object
Dim bCreated As Boolean

'See if any instance of word is running then use it
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
On Error GoTo 0

'We know that no instance of word is running then
'lets create one invisible instance and run the rest of code
If oWord Is Nothing Then
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
bCreated = True
End If

'Loop through all tasks
For Each tsk In oWord.Tasks
'Here you have to specify the folder name you wish to close
'e.g. following code will close explorer with folder name "songs"
'as current folder
If tsk.Name = "songs" Then
tsk.Close
End If
Next tsk

'If we have created instance then lets close it again
If bCreated Then oWord.Quit

End Sub
[/pre]
He is a god in human form
 
Top