簡體   English   中英

Excel VBA - 保存當前網址以在另一個子程序中使用

[英]Excel VBA - save current web address to use in another sub routine

快速問題我希望有一個簡單的答案。 我有一個通過網頁導航的子例程。 “子導航()”

Dim ie As Object
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True

等等.....

這部分工作正常,我根據需要成功瀏覽了 IE。 我的下一步來自一個單獨的子例程“Sub copyimage()”,我設計了從活動頁面拉出圖像的地方。 但是,我無法弄清楚如何為此使用打開的網頁。

如果我直接導​​航到此子文件中的 URL,則“Sub Copyimage()”子文件有效。 但我希望能夠從我的“Sub Navigate()”代碼的工作中到達那里。

我希望能夠從 navigate() 子例程調用 Copyimage() 子例程。

請幫忙

這是第一個子文件的完整代碼:

Sub FMSWebsite()
Dim strURL
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
strURL = "https://functionalmovement.com/login?return=%2F"
ie.Navigate strURL
ie.Visible = True
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
ie.Document.forms(0).all("Username").Value = Worksheets("Cover").Range("E8")
ie.Document.forms(0).all("Password").Value = Worksheets("Cover").Range("E10")
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

ie.Document.forms(0).submit
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Application.Wait (now + TimeValue("0:00:01"))
ie.Navigate ("http://functionalmovement.com/pro/clients")
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop


ie.Document.forms(1).all("gvClients$DXFREditorcol1").Value = Worksheets("Template").Range("X2")
ie.Document.forms(1).all("gvClients$DXFREditorcol1").Select

SendKeys String:="{enter}", Wait:=True

Application.Wait (now + TimeValue("0:00:03"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Dim e
For Each e In ie.Document.getElementsByTagName("strong")
If e.innerText = "Client Workouts" Then
e.ParentElement.Click
Exit For
End If
Next

Application.Wait (now + TimeValue("0:00:01"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "Create a New Workout" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Application.Wait (now + TimeValue("0:00:01"))

For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "NEXT" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Application.Wait (now + TimeValue("0:00:01"))
Set goBtn = ie.Document.getElementById("newscreen")
goBtn.Click

Application.Wait (now + TimeValue("0:00:01"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

快進一些代碼它像這樣結束:

For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "Complete" Then
e.ParentElement.Click
Exit For
End If
Next
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop

Application.Wait (now + TimeValue("0:00:03"))
Do While (ie.Busy Or ie.READYSTATE <> 4)
DoEvents
Loop
For Each e In ie.Document.getElementsByTagName("span")
If e.innerText = "View Assigned Workout" Then
e.ParentElement.Click
Exit For
End If
Next
End Sub

我的下一個子從

Sub findimageA ()
For Each Elem In ie.Document.getElementsByTagName("img")
If Elem.getAttribute("title") = "Step 1" Then

如何引用我在第一個子文件中結尾的網址?

我相信你在Navigate()類似的東西:

Dim ieDocument as Object 

它代表一個網頁(網絡文檔)。 如果是這樣,您需要創建帶有參數的Copyimage()子例程,如下所示:

Sub Copyimage(ieDocImages as object)

並從前一個傳遞文檔( ieDocument )中調用此子函數作為參數:

Call Copyimage(ieDocument)

或根據您使用的變量做類似的事情。

編輯,基於有問題的附加代碼

首先在End Sub之前或終止ie object之前添加以下行:

Call findimagA(ie.Document)

並按如下方式更改第二個子項:

Sub findimageA(ieDocument As Object)
For Each Elem In ieDocument.getElementsByTagName("img")

沒有經過測試,但我確定沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM