|

|
|
 |
7.8 フォームの操作1 |
 |
|
|
|
|
|
入力フォームなどで使うテキストボックスやチェックボックス、ラジオボタンに値を入れたりチェックしたりする方法です。IEオブジェクトのDocumentオブジェクトを使って行います。
と言っても難しくないです。基本的には、HTML内に書くスクリプト(JavaScriptやVBScript)でやるときの方法と一緒です。
|
|
|
|
|
|
|
|
|
|
Documentオブジェクトの使い方です。テキストボックスに値を入れたり、チェックボックスをチェックしたりなどは、次のように行います。
◆テキストボックスやhiddenのとき
IEオブジェクト.Document.フォーム名.テキストボックス(hidden)名.value = 設定したい内容
|
◆チェックボックスのとき
IEオブジェクト.Document.フォーム名.チェックボックス名.checked = TrueまたはFalse
|
TrueでチェックON、FalseでチェックOFFです。
◆ラジオボタンのとき
IEオブジェクト.Document.フォーム名.ラジオボタン名(0からの番号).checked = TrueまたはFalse
|
TrueでチェックON、FalseでチェックOFFです。
◆コンボボックスのとき
IEオブジェクト.Document.フォーム名.コンボボックス名.selectedIndex = 0からの番号
|
◆ボタンを押すとき
IEオブジェクト.Document.フォーム名.ボタン名.Click
|
ちなみに、フォーム名は<form>タグのnameエレメントに指定した名前、テキストボックス名やチェックボックス名は<input>タグのnameエレメントに指定した名前です。
|
|
|
|
|
|
|
|
|
|
フォーム操作の例です。プログラムでHTMLを書き込み、表示したフォームのテキストボックスに値を入れたりチェックボックスをチェックしたり(@〜Dの部分)してます。あと、最後にボタンをクリック(クリック動作はプログラム内、Eの部分)します。ボタンがクリックされると、ボタンのクリックイベント(JavaScriptで記述)が呼ばれますが、これはHTML内の処理です。
ちなみに、例ではテキストボックスなどの設定をするときにWScript.sleep(500)でインターバルを置いていますが、これは設定しているということを分かりやすくするためのもので、それ以外の意味はありません。
Option Explicit
Dim objIE
'objIEオブジェクトを作成します
Set objIE = CreateObject("InternetExplorer.Application")
'ウィンドウの大きさを変更します
objIE.Width = 800
objIE.Height = 600
'表示位置を変更します
objIE.Left = 0
objIE.Top = 0
'インターネットエクスプローラ画面を表示します
objIE.Visible = True
'カラのページを表示します
'(これを行わないと以降のdocument.writeなどがエラーになるため)
objIE.Navigate "about:blank"
'HTMLを出力します
objIE.Document.Write "<html>" & vbcrlf
objIE.Document.Write "<head>" & vbcrlf
objIE.Document.Write "<title>7.8 フォームの操作</title>" & vbcrlf
objIE.Document.Write "</head>" & vbcrlf
objIE.Document.Write "<script language=""javascript"">" & vbcrlf
objIE.Document.Write "function msg1() {" & vbcrlf
objIE.Document.Write " alert(""わっ!"");" & vbcrlf
objIE.Document.Write "}" & vbcrlf
objIE.Document.Write "</script>" & vbcrlf
objIE.Document.Write "<body>" & vbcrlf
objIE.Document.Write "<form name=""fm1"">" & vbcrlf
objIE.Document.Write "<table border=1 cellpadding=2 cellspacing=0>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>名前<br></td>" & vbcrlf
objIE.Document.Write "<td><input type=text name=txtName size=10><br></td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>性別<br></td>" & vbcrlf
objIE.Document.Write "<td>" & vbcrlf
objIE.Document.Write "<select name=txtSex>" & vbcrlf
objIE.Document.Write "<option value=""0""></option>" & vbcrlf
objIE.Document.Write "<option value=""1"">男</option>" & vbcrlf
objIE.Document.Write "<option value=""2"">女</option>" & vbcrlf
objIE.Document.Write "</select>" & vbcrlf
objIE.Document.Write "</td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>E-Mail<br></td>" & vbcrlf
objIE.Document.Write "<td><input type=text name=txtMail size=30><br></td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>URL<br></td>" & vbcrlf
objIE.Document.Write "<td><input type=text name=txtUrl size=50><br></td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>チェックボックス<br></td>" & vbcrlf
objIE.Document.Write "<td><input type=checkbox name=chkCheck size=50><br></td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>ラジオボタン<br></td>" & vbcrlf
objIE.Document.Write "<td>" & vbcrlf
objIE.Document.Write "<input type=radio name=radSelect value=1>A<br>" & vbcrlf
objIE.Document.Write "<input type=radio name=radSelect value=2>B<br>" & vbcrlf
objIE.Document.Write "<input type=radio name=radSelect value=3>C<br>" & vbcrlf
objIE.Document.Write "</td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "<tr>" & vbcrlf
objIE.Document.Write "<td>押してみて<br></td>" & vbcrlf
objIE.Document.Write "<td>" & vbcrlf
objIE.Document.Write "<input type=button name=btnMsg value=Click onClick=""msg1()"">" & vbcrlf
objIE.Document.Write "</td>" & vbcrlf
objIE.Document.Write "</tr>" & vbcrlf
objIE.Document.Write "</table>" & vbcrlf
objIE.Document.Write "</form>" & vbcrlf
objIE.Document.Write "</body>" & vbcrlf
objIE.Document.Write "</html>" & vbcrlf
'@名前欄を設定
WScript.sleep(500)
objIE.Document.fm1.txtName.value ="結城圭介"
'A性別を選択
WScript.sleep(500)
objIE.Document.fm1.txtSex.selectedIndex = 1
'Bメール欄とURL欄を設定
WScript.sleep(500)
objIE.Document.fm1.txtMail.value ="webmaster@happy2-island.com"
objIE.Document.fm1.txtUrl.value ="http://www.happy2-island.com/"
'Cチェックボックスをチェックする
WScript.sleep(500)
objIE.Document.fm1.chkCheck.checked = True
'Dラジオボタンをチェックする
WScript.sleep(500)
objIE.Document.fm1.radSelect(1).checked = True
'Eボタンを押す
WScript.sleep(500)
objIE.Document.fm1.btnMsg.Click
'オブジェクトの破棄
Set objIE = Nothing
|
|
|
|
|
|