smart3135 wrote:
但公司目前的資安非常嚴格,連外網都沒辦法連,更不用說讓我去安裝一些從網上抓的軟體
smart3135 wrote:
把VBA的程式碼改成開啟EDGE呢
目前不行,微軟沒有做 edge object,未來不知
不過SeleniumBasic,也有支援edge
如果連程式都不能裝,您也不想改用xmlhttp
那還有WScript.Shell+sendkey、滑鼠點擊這個方法,但非常容易誤動作
程式執行時,滑鼠、鍵盤都不能動
網頁如果有popup視窗,有時還要增加程式碼重新定位、active
通常內部網頁不會有太特別的設計,可以的話試著改用xmlhttp
說不定用21樓簡易方式就能處理
但是因為您無法公開資料,所以無法給您正確的建議
以下只有sendkey,滑鼠功能請參考(909樓範例)
#If Win64 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub test()
Dim Target As String, Wsh As Object, Run_App As Object, UrL As String, title As String
Set Wsh = CreateObject("WScript.Shell")
UrL = "https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=123"
title = "(不定期更新)使用VBA解決 excel web 查詢無法匯入、匯入太慢的股市資料 (第123頁) - Mobile01 - Google Chrome"
Target = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe " & UrL
'這裡要特別注意,視窗標題,是工作管理員裡面看到的那個

'但也有例外,特殊符號、unicode、不可見字元…像edge的標題就不一樣

'title = "(不定期更新)使用VBA解決 excel web 查詢無法匯入、匯入太慢的股市資料 (第123頁) - Mobile01 - 設定檔 1 - Microsoft? Edge"
'Target = "C:\Program Files (x86)\Microsoft\Edge\Application\102.0.1245.33\msedge.exe " & UrL
Set Run_App = Wsh.exec(Target)
Sleep 5000
AppActivate title
Sleep 1000
SendKeys "^a", True
Sleep 3000
SendKeys "^c", True
Sleep 3000
With Sheets("工作表1")
.Select
.Cells.Clear
.Cells(1, 1).Select
Sleep 6000
.PasteSpecial NoHTMLFormatting:=True 'false
.Columns.AutoFit
.Cells(1, 1).Select
End With
If Run_App.Status = 0 Then Run_App.Terminate
Set Run_App = Nothing
Set Wsh = Nothing
End Sub
補一個自動找完整標題的副程式,不過用途不太
基本上只要瀏覽器不變,標題是定值,一次性的東西
想要合併到上面的程式也行
#If Win64 Then
Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
#Else
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
#End If
Sub findtitle()
Dim AppTitle As String
AppTitle = find_windows_full_title("不定期") '輸入部份特定關鍵字即可
Debug.Print AppTitle, Len(AppTitle)
'Cells(1, 1) = AppTitle
End Sub
Function find_windows_full_title(s As String) As String
Dim apphWnd As Long, AppTitle As String
apphWnd = FindWindow(vbNullString, vbNullString)
While apphWnd
AppTitle = Space(255)
AppTitle = Left(AppTitle, GetWindowText(apphWnd, AppTitle, Len(AppTitle)))
If AppTitle Like "*" & s & "*" Then
find_windows_full_title = AppTitle
Exit Function
End If
apphWnd = GetWindow(apphWnd, 2)
Wend
End Function