応用
新しいタブでサイトを開く(InternetExplorer7以降)
InternetExplorerでのページの表示を新しいタブで行う方法です。(バージョン7以降)
Navigateメソッドの第二引数に「&H800」または「&H1000」を指定することで新しいタブでページを表示します。
「&H800」を指定すると、今表示しているタブから新しいタブに切り替えてページを表示します。
「&H1000」を指定すると、今表示しているタブはそのままに、別に新しいタブを作成してページを表示します。
タブを指定してページを表示させたい場合は、新しいタブでページを開いた際にオブジェクトを保持しておき、
そのオブジェクトに対して以降Navigateや操作をすることで行えます。
Public Class Form1
Private Const NAV_OPEN_IN_NEW_TAB As Integer = &H800
Private Const NAV_OPEN_IN_BACKGROUND_TAB As Integer = &H1000
Private WithEvents objIE As New SHDocVw.InternetExplorer()
Private ieList As New List(Of SHDocVw.InternetExplorer)
Private shell As Object
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
objIE.Navigate("https://web.biz-prog.net/index.html")
objIE.Visible = True
shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1))
End Sub
'タブを追加してページを表示する。追加したタブに切り替える。
Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
objIE.Navigate("https://web.biz-prog.net/test/testpage.html", NAV_OPEN_IN_NEW_TAB)
System.Threading.Thread.Sleep(3000) '本来は表示が完了するまで待ってからリストに追加する。でないと正しいオブジェクトを取得できない
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1))
End Sub
'タブを追加してページを表示する。タブは切り替えない
Private Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
objIE.Navigate("https://web.biz-prog.net/test/testpage.html", NAV_OPEN_IN_BACKGROUND_TAB)
System.Threading.Thread.Sleep(3000) '本来は表示が完了するまで待ってからリストに追加する。でないと正しいオブジェクトを取得できない
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1))
End Sub
'最後に開いたタブで別のページを表示する
Private Sub button4_Click(sender As Object, e As EventArgs) Handles button4.Click
ieList(ieList.Count - 1).Navigate("https://www.google.co.jp/")
End Sub
End Class
public partial class Form1 : Form
{
private static int NAV_OPEN_IN_NEW_TAB = 0x800;
private static int NAV_OPEN_IN_BACKGROUND_TAB = 0x1000;
private SHDocVw.InternetExplorer objIE = new SHDocVw.InternetExplorer();
private List<SHDocVw.InternetExplorer> ieList = new List<SHDocVw.InternetExplorer>();
dynamic shell;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
objIE.Navigate("https://web.biz-prog.net/index.html");
objIE.Visible = true;
shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1));
}
//タブを追加してページを表示する。追加したタブに切り替える。
private void button2_Click(object sender, EventArgs e)
{
objIE.Navigate("https://web.biz-prog.net/test/testpage.html", NAV_OPEN_IN_NEW_TAB);
System.Threading.Thread.Sleep(3000); //本来は表示が完了するまで待ってからリストに追加する。でないと正しいオブジェクトを取得できない
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1));
}
//タブを追加してページを表示する。タブは切り替えない。
private void button3_Click(object sender, EventArgs e)
{
objIE.Navigate("https://web.biz-prog.net/test/testpage.html", NAV_OPEN_IN_BACKGROUND_TAB);
System.Threading.Thread.Sleep(3000); //本来は表示が完了するまで待ってからリストに追加する。でないと正しいオブジェクトを取得できない
ieList.Add(shell.Windows.Item(shell.Windows.Count - 1));
}
//最後に開いたタブで別のページを表示する
private void button4_Click(object sender, EventArgs e)
{
ieList[ieList.Count - 1].Navigate("https://www.google.co.jp/");
}
}
Sub sample()
'定数定義
Const IE_MAX As Integer = 5
Const NAV_OPEN_IN_NEW_TAB As Long = &H800
Const NAV_OPEN_IN_BACKGROUND_TAB As Long = &H1000
'変数定義
Dim objShell As Object
Dim objIE As SHDocVw.InternetExplorer
Dim count As Integer
Dim ieList(4) As SHDocVw.InternetExplorer
Dim i As Long
'オブジェクト設定
Set objShell = CreateObject("Shell.Application")
Set objIE = CreateObject("InternetExplorer.Application")
'IE起動&タブを5つまで開く
objIE.Visible = True '可視
While count < IE_MAX
If count = 0 Then
objIE.Navigate "about:blank"
Else
objIE.Navigate "about:blank", NAV_OPEN_IN_BACKGROUND_TAB
End If
For i = 0 To 100000
DoEvents 'ある程度の時間Sleepしないと、開く時に異なるタブで開けない
Next
Set ieList(count) = objShell.Windows.Item(objShell.Windows.count - 1)
count = count + 1
Wend
DoEvents
'タブ1と2でサイトを表示
ieList(0).Navigate "https://www.google.co.jp/"
ieList(1).Navigate "https://www.yahoo.co.jp/"
End Sub
