简体   繁体   中英

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

Quick question I hope there is an easy answer. I have a sub routine that navigates through a webpage. "Sub Navigate()"

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

and so on.....

This part works fine and I successfully navigate through IE as needed. My next step comes from a separate sub routine "Sub copyimage()" I have designed where I pull images off the active page. However I cannot figure out how to use the open webpage for this.

The "Sub Copyimage()" sub works if I navigate directly to the URL within this sub. But I want to be able to get there from the work of my "Sub Navigate()" code.

I would like to be able to call the Copyimage() subroutine from the navigate() subroutine.

Please help

Here is full code from the first sub:

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

Fast Forward some code it ends like so:

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

My next sub starts with

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

How do I reference the web address I ended with in the first sub?

I believe you have in Navigate() something like:

Dim ieDocument as Object 

which represent a web page (web document). If so, you need to create Copyimage() subroutine with argument, like this:

Sub Copyimage(ieDocImages as object)

and call this sub from previous one passing document ( ieDocument ) as a parameter:

Call Copyimage(ieDocument)

or do something similar depending of variables you use.

EDIT, based on additional code in question

First sub- add the following line before End Sub or before you terminate ie object :

Call findimagA(ie.Document)

and change the second sub as follows:

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

Not tested but I'm sure it is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM