VB6(SP6) NT4.0(SP5) IE5.5
http://www.microsoft.com/japan/msdn/columns/dude/dude050399.asp
↑ここを参考にして、コマンドボタンを押すとテーブルのセルを
追加していくプログラムを作っているのですが、下のコードでは
2つめのinsertbeforeで「引数が無効です」というエラーが出て
しまいます。
2番目の引数に変数を使わず、WebBrowser1.Document.activeElementと
すると、なぜかうまくいきます。
なぜ、変数に入れるとうまくいかないのでしょうか?
ちなみに変数の型をVariant型にしても同じでした。
Private Sub Command4_Click()
Dim objActiveElement As Object
Dim r As Object
Dim c As Object
Dim nc As Object
Dim txt As Object
Set objActiveElement = WebBrowser1.Document.activeElement
If objActiveElement.tagName = "TD" Then
Set r = objActiveElement.parentElement
Set c = objActiveElement
Set nc = WebBrowser1.Document.createElement("TD")
Set txt = WebBrowser1.Document.createTextNode("New Cell")
nc.insertBefore txt
r.insertBefore nc, c '←ここで「引数が無効です」のエラーになる
'r.insertBefore nc, objActiveElement '←こうしても「引数が無効です」のエラーになる
'r.insertBefore nc, WebBrowser1.Document.activeElement 'こうするとエラーにならない
End If
End Sub
> r.insertBefore nc, objActiveElement
のように、「変数」を直接指定するのではなく、
r.insertBefore nc, CVar(objActiveElement)
のように、「式」を渡すようにしてみてください。
> 'r.insertBefore nc, WebBrowser1.Document.activeElement 'こうするとエラーにならない
この場合、第2引数は「変数」ではありませんね。
> ちなみに変数の型をVariant型にしても同じでした。
タイプライブラリを用いて、変数 r の宣言を、
Dim r As HTMLTableRow
のように厳密な型にしてみるのも一つの手かも。
魔界の仮面弁士 様
r.insertBefore nc, CVar(objActiveElement)
で、うまくいきました。
どうもありがとうございます。
こんなに早く解決するとは思いませんでした、感謝です。
ツイート | ![]() |