tagCANDY CGI VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re^3: VB.NETでIEのアドレス文字列につきまして
投稿日: 2006/02/11(Sat) 23:34
投稿者YAS
[OSのVer]:Windows    [VBのVer]:VB.NET  
> 起動しているInternetExplorerの現在のURLを取得したいです。

それならばアドレスの文字列を取得するよりもシェルオブジェクトを取得して
document.urlを参照した方がよいのではないでしょうか。
アドレス欄は非表示の場合もありますし,入力中の場合もあります。
以下のサンプルは開いているInternetExplorerのURLを列挙します。
参考になれば幸いです。

'ShellObject取得サンプル
Imports System.Runtime.InteropServices

Public Class Form1
    Dim WithEvents List As ListBox
    Dim WithEvents Timer As Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        List = New ListBox
        List.Dock = DockStyle.Fill
        List.IntegralHeight = False
        Me.Controls.Add(List)
        Timer = New Timer
        Timer.Interval = 1000
        Timer.Enabled = True
    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Timer.Tick
        Dim Shell As Object = Nothing 'Shellオブジェクト(COMオブジェクト)
        Dim Document As Object = Nothing 'IWebDocument2オブジェクト(COMオブジェクト)
        Dim State As READYSTATE
        List.Items.Clear()
        Try
            Shell = CreateObject("Shell.Application").Windows 'Shellオブジェクトを取得
            For Each Obj As Object In Shell
                Try
                    State = CType(Obj.ReadyState, Integer) 'アクセス可能かどうか調べる
                    If State >= READYSTATE.INTERACTIVE Then
                        Document = Obj.document
                        Dim DocumentType As String = TypeName(Document)
                        If DocumentType.Substring(0, 4) = "HTML" Then 'IEだけ
                            List.Items.Add(Obj.document.url)
                        End If
                    End If
                Catch ex As Exception
                    Debug.Print(ex.Message)
                Finally
                    If Document IsNot Nothing AndAlso Marshal.IsComObject(Document) Then
                        Marshal.ReleaseComObject(Document) 'COMオブジェクトを解放
                    End If
                End Try
            Next
        Catch ex As Exception
            Debug.Print(ex.Message)
        Finally
            If Shell IsNot Nothing AndAlso Marshal.IsComObject(Shell) Then
                Marshal.ReleaseComObject(Shell) 'COMオブジェクトを解放
            End If
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Timer.Enabled = False
        Me.Timer.Dispose()
        Me.List.Dispose()
    End Sub

End Class

Public Enum READYSTATE
    UNINITIALIZED = 0
    LOADING = 1
    LOADED = 2
    INTERACTIVE = 3
    COMPLETE = 4
End Enum

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。