PDA

View Full Version : Solved: How to get active item - regardless whether in inspector or explorer



Sebastian H
01-13-2012, 03:46 PM
I want to call a sub on the (first) active item, like this:

itm = IIf(inspector_window_is_active, _
ActiveInspector.CurrentItem, _
ActiveExplorer.Selection.Item(1))

How to get inspector_window_is_active?

JP2112
01-23-2012, 12:57 PM
I think you mean this:

Sub Test()
Set itm = GetCurrentItem
End Sub

Function GetCurrentItem() As Object
' returns reference to current item, either the one
' selected (Explorer), or the one currently open (Inspector)
Select Case True
Case IsExplorer(Application.ActiveWindow)
Set GetCurrentItem = ActiveExplorer.Selection.Item(1)
Case IsInspector(Application.ActiveWindow)
Set GetCurrentItem = ActiveInspector.CurrentItem
End Select
End Function
Function IsExplorer(itm As Object) As Boolean
IsExplorer = (TypeName(itm) = "Explorer")
End Function
Function IsInspector(itm As Object) As Boolean
IsInspector = (TypeName(itm) = "Inspector")
End Function

Sebastian H
01-23-2012, 02:04 PM
What a great reply! Not only did I get a solution for my problem, but I also learned a cool way to use a Case statement. And, of course, you're right that I would need Set for the assignment of an object.
:dance:

bonnero
06-18-2018, 08:56 AM
Thanks a lot for posting this code. It really helped me a lot and I also learned from the Case method you have used.