简体   繁体   中英

VB.net closing folder - doesn't work when IE open

Sort of an odd problem I'm encountering.

Im using the following code to find an open folder and close it

Dim sh = CreateObject("shell.application")
For Each item In sh.Windows
    If item.document.folder.self.Path = DBFolder Then
        item.Quit()
    End If
Next

Here's the weird part, it works as intended unless an Internet Explorer window is open. With IE open I get the following error:

An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

Additional information: Public member 'folder' on type 'HTMLDocumentClass' not found.

I don't personally use IE but my users probably do so this is going to cause issues if I deploy. Does anyone have a workaround or suggestion?

This is expected behavior, you will be picking up any open explorer or internet explorer windows. You can handle this by adding a reference to mshtml in Microsoft HTML Object Library and also SHDocVw in Microsoft Internet Controls.

Add Reference > COM > Type Libraries > Microsoft HTML Object Library

Add Reference > COM > Type Libraries > Microsoft Internet Controls

    Dim sh As New SHDocVw.ShellWindows

    For Each item In sh

        If Not TypeOf item.Document Is mshtml.HTMLDocument Then
            MessageBox.Show("it is not an IE window! " + item.Path())
        Else
            MessageBox.Show("it is an IE window!" + item.Path())
        End If


    Next item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM