dポイントプレゼントキャンペーン実施中!

分かったら教えてください。
以下ではマクロでIEを開いてフォームをサブミットしています。

ie.Navigate ("http://www.yahoo.co.jp")
Do While (ie.ReadyState <> state) Or (ie.Busy = True)
DoEvents
Loop
With ie.Document.forms(0)
.Item("p").Value = "焼きいも"
.Item("Login").Click
End With

フォームに”焼いも”といれてサブミットし、検索結果を取得します。
ここまではできました。

ところが、この画面とは別のフォームに、
登録ボタンを押すと別のウィンドウが立ち上がるものがありました。
その場合、開いた後このie.Navigate で開いたウィンドウを操作
しようとしてもはじめのウィンドウがナビゲートされてしまいます。
javascriptのようにウィンドウを変数で受けるとか
別の方法がありますか?

連レス申し訳ありませんが、よろしくお願いします。

A 回答 (2件)

IE6でも動くはずです。



≪ie.Windows.Countでエラーの件≫
大変失礼しました。
コードを、全文拝見した方が間違いがないと思いますが
多分
nmbw1 = CreateObject("Shell.Application").Windows.Count
nmbw2 = CreateObject("Shell.Application").Windows.Count
とすれば良いと思います。

≪nmbw1 = Windows.Count なら2ととれる件≫
Application.Windows.Count ということです。
試しに
Msgbox Windows(1).Caption
とでも、やってみてください。

この回答への補足

ありがとうございました!
できました。

エクセル表のB列にIPを入力すると
C列に組織名を表示するvbaを作りました。
xls88さんならすぐ作れるでしょうけど、
ソースを載せておきます。


Sub IPtoCorpName()
' マクロ記録日 : 2009/1/21 ユーザー名 : shikinokage
Dim ie As Object
Dim ie2 As Object
Dim shell As Object
Dim htmltext As String
Dim cellhtml As String
Dim n As Integer
Dim i As Integer
Dim ip As String
Dim numberOfWindows As Integer
Dim htmlLength As Integer
Dim htmlLeftLength As Integer
Dim htmlRightLength As Integer

Set ie = CreateObject("InternetExplorer.Application")
Set shell = CreateObject("Shell.Application")
Const state = 4
'Dim ie As New InternetExplorer

ie.Visible = False

i = 2
Do While True
i = i + 1
ip = Cells(i, 2)
If (ip <> "") Then
ie.Navigate ("http://www.cman.jp/network/support/ip.html")
Do While (ie.ReadyState <> state) Or (ie.Busy = True)
DoEvents
Loop
With ie.Document.forms(0)
.Item("in_ip").Value = Cells(i, 2)
.Item("go_ip").Click
End With

numberOfWindows = Windows.Count
Windows(numberOfWindows - 1).Activate

Set ie2 = shell.Windows.Item(shell.Windows.Count - 1)

Do While (ie2.ReadyState <> state) Or (ie2.Busy = True)
DoEvents
Loop

htmltext = ie2.Document.body.innerHTML

Do While (ie2.ReadyState <> state) Or (ie2.Busy = True)
DoEvents
Loop

htmlLength = Len(htmltext)

htmlLeftLength = InStr(htmltext, "組織名")

If htmlLeftLength > 0 Then
htmlRightLength = InStr(htmlLeftLength, htmltext, "TR")

htmltext = Left(htmltext, htmlRightLength - 8)

Cells(i, 3) = Right(htmltext, htmlRightLength - htmlLeftLength - 22)
Else
Cells(i, 3) = "なし"
End If
Else
MsgBox ("終わりました")
Exit Sub
End If
Loop
End Sub

補足日時:2009/01/26 21:13
    • good
    • 0

窓の数を数えればどうでしょうか?


IE7で確認しています。

ie.Navigate ("http://www.yahoo.co.jp")
Do While (ie.ReadyState <> state) Or (ie.Busy = True)
DoEvents
Loop
nmbw1 = ie.Windows.Count '★(1)
With ie.Document.forms(0)
.Item("p").Value = "焼きいも"
.Item("Login").Click
End With
nmbw2 = ie.Windows.Count '★(2)

★(1)と★(2)で、開かれている窓の数を取得し、
nmbw1 < nmbw2 なら
新しい窓は
ie.Windows(nmbw2 - 1)
ということになります。

この回答への補足

ありがとうございます。
この方法で試してみます。
ただ、これってIE6で使えなかったりしますか?
末端にIE7か8を入れようと思いますが、IE6でも動く方法はありますか?

また、IE6の環境で動かそうとすると「そんなプロパティまたはメソッドは定義されていません」みたいなエラーが出るのですが、
ie.windows.countの部分はvbaを保存したときはエラーは出ません。
vbaが悪いのかブラウザにそのようなメソッド(関数)がないのか
いまいち判断が付かないです。

補足日時:2009/01/26 09:16
    • good
    • 0
この回答へのお礼

すみません、
nmbw1 = ie.Windows.Count でエラーになります。
nmbw1 = Windows.Count なら2ととれますが、

ie.Windows(nmbw2 - 1)のWindowsプロパティが無いと
言われます。

なぜ駄目なのかと開いたウィンドウの操作を教えていただきたく思います。よろしくお願い致します。

お礼日時:2009/01/26 18:25

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!