Excel VBA - 保存当前Web地址以在另一个子例程中使用

时间:2015-08-18 05:35:43

标签: function excel-vba internet-explorer url subroutine

快速提问我希望有一个简单的答案。 我有一个子例程,可以浏览网页。 " Sub Navigate()"

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

依旧.....

这部分工作正常,我根据需要成功浏览IE。我的下一步来自一个单独的子例程" Sub copyimage()"我已经设计了从活动页面拉出图像的位置。但是,我无法弄清楚如何使用打开的网页。

" Sub Copyimage()"如果我直接导​​航到此子网中的URL,则子工作。但我希望能够从我的" 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

如何在第一个子目录中引用我结束的网址?

1 个答案:

答案 0 :(得分:1)

我相信你有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")

未经测试,但我确定没问题。