2015-08-17 258 views
1

目前我正在嘗試搜索一個打開的網頁中的一些PNG圖像,並將它們導入到我的Excel工作簿的選項卡中的不同單元格中。Excel VBA找到網頁圖像並將其導入excel

我現有的宏瀏覽一個網頁,指示將顯示什麼圖片和有多少圖片。

我想寫一個宏到此時搜索打開的網頁並將圖像導入到excel中。

每次程序運行時都無法知道圖像的數量。但是,每個圖像都在以下html代碼中:

img src="/files/exercises/31a_3 - trunk stability rotation kneex flexed.still002.194x146.png" title="Step 2" 

雖然文件名將會更改。我正在尋找標題=「第2步」的所有圖像

並將所有這些圖像導入到excel中。 在當前時刻,我可以通過下面的代碼successfuly找到網址:

max = 100 
For i = 0 To max 
For Each Elem In ie.Document.getElementsByTagName("img") 
If ie.Document.getElementsByTagName("img").Item(i).getAttribute("title") = "Step 2" Then 
Worksheets("Sheet4").Range("A1") = "http://functionalmovement.com" & ie.Document.getElementsByTagName("img").Item(i).getAttribute("src") 
Exit For 
Exit For 
End If 
Next 
Next 
End Sub 

問題:

  1. 我如何可以搜索整個頁面,並第一個被發現放在一個特定的細胞,第二次發現放在另一個單元格中,等等,直到沒有更多的發現。

  2. 有沒有辦法讓圖像本身出現,因爲每個圖像的完整url已知。

回答

1

找到我正在尋找的東西。此代碼搜索title =「Step 1」的圖像。然後我下載這些圖像。

Sub findimageA() 

Dim strURL As String 
Dim ie As Object 
Dim Elem 
Dim X 

Set ie = CreateObject("InternetExplorer.Application") 
strURL = "http://blahblah" 
ie.Navigate strURL 
ie.Visible = True 
Do While (ie.Busy Or ie.READYSTATE <> 4) 
    DoEvents 
Loop 
For Each Elem In ie.Document.getElementsByTagName("img") 
    If Elem.getAttribute("title") = "Step 1" Then 
     Select Case X 
      Case Is = 0 
      pic1aURL = "http://functionalmovement.com" & Elem.getAttribute("src") 
      pic1aName = Elem.getAttribute("src") 
      pic1aName1 = Replace(pic1aName, "/files/exercises/", "") 
      Case Is = 1 
      pic2aURL = "http://functionalmovement.com" & Elem.getAttribute("src") 
      pic2aName = Elem.getAttribute("src") 
      pic2aName1 = Replace(pic2aName, "/files/exercises/", "") 
      Case Is = 2 
     End Select 
     X = X + 1 
    End If 
Next 
    If Not IsEmpty(picaName) Then 
      DownloadFile 
    End If 
End Sub 
+0

我的新進程搜索帶有標題名稱的所有圖像,然後下載圖像。 從這裏我將創建一個子插入文件的圖片 – JT31