我正在尝试设计一个打开特定 URL 的代码,在特定点,我遇到了与该网站相关的 OPEN 对话框。就像那样
现在我遇到的问题是,我需要将“C:\Users\User\Desktop\Pics(423).jpg”之类的特定路径粘贴到文件名字段,然后单击“打开”按钮
而且我也被困在如何使用剪贴板复制内存中的路径.. 感谢高级帮助
最佳答案
如果您想避免使用 SendKeys
方法,您可以使用 UIAutomationClient library或 user32.dll 中的 SendMessage
函数。这些将允许您使用 Windows API 与对话窗口进行交互。
关于VBA 中的UIAutomationClient
库的在线资源并不多,但是this video (和 part 2 )应该足以让您入门。
编辑:
但是,如果您决定使用 SendMessage
,下面是一个如何实现它的示例:
1) 将以下内容放在代码模块的顶部
Option Explicit
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Private Const BM_CLICK = &HF5
Private Const WM_SETTEXT = &HC
2) 添加以下子过程并将FileName的值更改为所需的值
Sub FillFileNameAndPressOpenButton()
Dim FileName As Variant
FileName = "(423)" 'Change this to the appropriate file name
Dim WindowCaption As String
WindowCaption = "Open"
Dim OpenDialogHandle As LongPtr
OpenDialogHandle = FindWindow(vbNullString, WindowCaption)
If OpenDialogHandle = 0 Then
MsgBox "Couldn't find the Window"
Exit Sub
End If
Dim TextBoxHandle As LongPtr, tempHandle As LongPtr
tempHandle = FindWindowEx(OpenDialogHandle, 0, "ComboBoxEx32", vbNullString)
tempHandle = FindWindowEx(tempHandle, 0, "ComboBox", vbNullString)
TextBoxHandle = FindWindowEx(tempHandle, 0, "Edit", vbNullString)
If TextBoxHandle = 0 Then
MsgBox "Couldn't find the textbox"
Exit Sub
End If
Dim Rslt As LongPtr
Rslt = SendMessage(TextBoxHandle, WM_SETTEXT, 0, FileName)
DoEvents
Dim ButtonHandle As LongPtr, ButtonCaption As String
ButtonCaption = "&Open"
ButtonHandle = FindWindowEx(OpenDialogHandle, 0, "Button", ButtonCaption)
If ButtonHandle = 0 Then
MsgBox " Couldn't find the button"
Exit Sub
End If
Rslt = SendMessage(ButtonHandle, BM_CLICK, 0, 0)
DoEvents
End Sub
关于excel - SendKeys到selenium VBA中打开对话框中的文件名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59415167/