Board logo

標題: [發問] 想請教使用FindWindow按下對話框的"儲存" [打印本頁]

作者: iamaraymond    時間: 2018-1-27 14:31     標題: 想請教使用FindWindow按下對話框的"儲存"

我想抓取在google trend 的CSV,但當我點選下載CSV的按鈕時,會先出現對話框,我想點選對話框的"儲存",但盡量不要用sendkey的方式
如圖
[attach]28295[/attach]
我有google到findwindow來解決,但還是不太知道如何應用
想請問版上各位高手,謝謝
相關網址
google trend:
https://trends.google.com.tw/trends/explore?q=%E5%8F%B0%E7%A9%8D%E9%9B%BB

我找到的相關答案,但看得不是很懂
https://www.mrexcel.com/forum/excel-questions/567033-automate-file-download-dialog-box-without-sendkeys.html
使用findwindow時要先加入

Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long



Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

最後是我的code
  1. Sub googleTrend()

  2. Dim IE As Object
  3. Dim myhtml As Object

  4. [A5].CurrentRegion.Clear

  5. Set IE = CreateObject("InternetExplorer.Application")
  6. Set myhtml = CreateObject("htmlfile")

  7. With IE
  8.     .Visible = False
  9.     .Navigate "https://trends.google.com.tw/trends/explore?q=%E5%8F%B0%E7%A9%8D%E9%9B%BB"
  10.     Do While .Busy Or .readyState <> 4: DoEvents: Loop
  11.    
  12.     Dim doc
  13.     Set doc = .document
  14.    
  15.     Dim myButton
  16.     Dim myButtons
  17.    
  18.     Call delay
  19.     Set myButtons = doc.getElementsByTagName("button")
  20.     For Each myButton In myButtons
  21.         If myButton.getAttribute("title") Like "*CSV*" Then
  22.             myButton.Click
  23.             Call delay
  24.             
  25.             Application.SendKeys ("%{s}")
  26.             Exit For
  27.         End If
  28.         'Debug.Print myButton.getAttribute("title")
  29.     Next
  30.    
  31.     Dim theCSV
  32.     Dim WB
  33.     Dim theData
  34.     theCSV = "C:\Users\User\Downloads" & "\" & "multiTimeline.csv"
  35.     If Dir(theCSV) <> "" Then Kill theCSV
  36.     On Error GoTo errHandler
  37. continue:
  38.     Set WB = Workbooks.Open(theCSV)
  39.     With WB.ActiveSheet
  40.         Set theData = .[A1].CurrentRegion
  41.         ThisWorkbook.Sheets("工作表1").[A5].Resize(theData.Rows.Count, theData.Columns.Count) = theData.Value
  42.     End With
  43.     WB.Close
  44.     Set WB = Nothing
  45.     Kill theCSV
  46.    
  47.     .Quit
  48.     Set IE = Nothing
  49. End With

  50. Exit Sub

  51. errHandler:
  52. On Error GoTo 0
  53. Call delay
  54. GoTo continue

  55. End Sub

  56. Sub delay()
  57. Application.Wait Now() + TimeValue("00:00:03")
  58. End Sub
複製代碼
感謝




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)