返回列表 上一主題 發帖

[發問] 如何用vba執行網頁選擇檔案的功能?

[發問] 如何用vba執行網頁選擇檔案的功能?

網址:
  1. https://www.pinkoi.com/
複製代碼
這個網站可以直接用fb登入
註冊之後會有信件,打開信件有個地方可以選擇檔案如圖片

如同麻辣家族的附件的選擇檔案也是一樣,按下去之後都會停止動作,除非手動關閉該視窗,請問有如何解決這個問題呢?

但程式一旦.Click 這個選擇檔案的按鈕之後就無法動作了
必須要手動關閉該視窗才可以繼續動作
請問要如何解決這個問題呢?

PS:許多網站都有相同的這個問題,還請各位大大幫忙解決
PKKO

  1. Sub test()
  2. 'PS:要自己先用ie開啟https://www.pinkoi.com/網址
  3. '並且用FB點選登入,不然下列語法將會找不到傳送訊息的部分,要有登入才可傳送訊息給別人
  4. Set IE = CreateObject("InternetExplorer.Application")
  5. IE.Visible = True
  6. IE.Navigate "https://www.pinkoi.com/user/testpkko"
  7. Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
  8. With IE.Document
  9.     i = 0: On Error Resume Next
  10.     For Each E In .GetElementsByTagName("A")
  11.         A = E.Text
  12.         If CStr(A) = "傳送訊息" Then
  13.             E.Click '這邊呼叫出傳送訊息的視窗
  14.             '要如何控制該視窗輸入主旨+內文+選擇檔案呢?
  15.             
  16.             Call WEB_DETAIL(IE) '我用網頁元素的部分無法查不出相關資訊
  17.         End If
  18.     Next
  19. End With
  20. End Sub
複製代碼
  1. Sub WEB_DETAIL(IE)
  2. With IE
  3.     Do While .Busy Or .readyState <> 4: DoEvents: Loop
  4.     With .Document
  5.         i = 0: On Error Resume Next
  6.         For Each E In .ALL
  7.             i = i + 1
  8.             Cells(i, "a") = E.tagname
  9.             Cells(i, "b") = E.ID
  10.             Cells(i, "c") = E.Name
  11.             Cells(i, "d") = E.Value
  12.             Cells(i, "e") = E.innertext
  13.             Cells(i, "f") = E.Type
  14.             Cells(i, "g") = E.href
  15.             Cells(i, "H") = E.Text
  16.         Next
  17.     End With
  18. End With
  19. End Sub
複製代碼
PKKO

TOP

本帖最後由 PKKO 於 2017-1-5 15:01 編輯

我已經把完整的登入程式碼以及跳出表單的程式碼都完成
但是要如何才能夠將跳出的表單輸入主旨和內文以及選擇檔案並且寄出資料呢?
  1. Sub test()
  2. Dim ie As Object 'SHDocVw.InternetExplorer
  3. Set ie = CreateObject("InternetExplorer.Application")
  4. Call login(ie) 'just for login
  5. Call show_the_form(ie) 'My problem in here
  6. End Sub
  7. Sub login(ie)
  8. ie.Visible = True
  9. ie.Navigate "https://www.pinkoi.com/user/testpkko"
  10. Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop


  11. Dim anchorTags As Object 'MSHTML.IHTMLElementCollection
  12. Set anchorTags = ie.document.GetElementsByTagName("A")

  13. Dim oAnchorLoop As Object 'MSHTML.IHTMLAnchorElement
  14. For Each oAnchorLoop In anchorTags
  15.     Dim anchorText As String
  16.     anchorText = oAnchorLoop.Text
  17.     If anchorText = ChrW(20659) & ChrW(36865) & ChrW(35338) & ChrW(24687) Then
  18.         Dim oAnchorLogon As Object 'MSHTML.IHTMLAnchorElement2
  19.         Set oAnchorLogon = oAnchorLoop
  20.         oAnchorLogon.Click
  21.         Exit For
  22.     End If
  23. Next

  24. Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
  25. '* so now logon form should be visible

  26. On Error Resume Next
  27. Dim oUserNameInput As Object 'MSHTML.IHTMLInputElement
  28. Set oUserNameInput = ie.document.getElementById("n-login-id")
  29. nowtime = Timer
  30. Do While ie.Busy Or ie.readyState <> 4 Or oUserNameInput Is Nothing
  31.     DoEvents
  32.     If Timer - nowtime > 1 Then Exit Do 'Already logoin
  33.     Set oUserNameInput = ie.document.getElementById("n-login-id")
  34. Loop

  35. oUserNameInput.Value = "testpkko"

  36. Dim oUserPassword As Object 'MSHTML.IHTMLInputElement
  37. Set oUserPassword = ie.document.getElementById("n-login-password")

  38. oUserPassword.Value = "abc123"


  39. Dim oListElement As Object 'MSHTML.HTMLLIElement
  40. Set oListElement = oUserNameInput.parentElement

  41. Dim oUnorderedList As Object 'MSHTML.IHTMLUListElement
  42. Set oUnorderedList = oListElement.parentElement

  43. Dim oForm As Object 'MSHTML.IHTMLFormElement
  44. Set oForm = oUnorderedList.parentElement

  45. Dim oSubmitInputElememt As Object 'MSHTML.HTMLInputElement
  46. Set oSubmitInputElememt = Nothing

  47. Dim lFormChildrenLoop As Long
  48. For lFormChildrenLoop = 1 To oForm.all.Length

  49. If oForm.elements.Item(lFormChildrenLoop).Type = "submit" Then
  50.     Set oSubmitInputElememt = oForm.elements.Item(lFormChildrenLoop)
  51.     Exit For

  52. End If

  53. Next lFormChildrenLoop
  54. If Not oSubmitInputElememt Is Nothing Then
  55.     'Stop '* get ready .....
  56.     oSubmitInputElememt.Click
  57.     Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
  58. End If
  59. End Sub
  60. Sub show_the_form(ie)
  61. Application.Wait Now + TimeValue("00:00:03")

  62. ie.Navigate "https://www.pinkoi.com/user/testpkko2" 'After login we can trip to other people website
  63. Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop
  64. With ie.document
  65. On Error Resume Next
  66. For Each E In .GetElementsByTagName("A")
  67.     a = E.Text
  68.     If CStr(a) = "傳送訊息" Then
  69.         E.Click '這邊會跳出寄送的表單,要如何輸入主旨以及內文和選擇檔案並按下寄送按鈕呢?
  70.     End If
  71. Next
  72. End With
  73. End Sub
複製代碼
PKKO

TOP

回復 7# singo1232001


    非常感謝您的回答,我受益了三個部分
一、原來可以不用每次都開啟新的IE,這個對我幫助很大
二、我之前都是透過另外一個方法將網頁移到最前端(程式碼較長,您的方法更簡單了)
  1. ie.Visible = False     ''就是從新隱藏使用視窗
  2.                 ie.Visible = True      ''接著又重新顯示active使用視窗
  3.                     ie.document.Focus
複製代碼
三、原來當按下按鈕程式碼會無法動作的時候,另外一個設定時間啟動的巨集也無法啟動(我原本也要做實驗了,省下了做實驗的時間)
您的問題我已經看到另外一篇文章,我做點實驗之後再一起回復您!
PKKO

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題