VBA代码网页指定位置输入文本求助:1-8点击后无法在位置9输入Ok
Hey there! Your current VBA code handles the mouse clicks for positions 1-8 perfectly, but it's missing the critical step to send the text "Ok" to the input field at position 9 once you've clicked it. Let's adjust your code to add that functionality, plus share a more reliable alternative if basic text sending doesn't work.
Quick Fix with SendKeys
The simplest way to add text input is using VBA's SendKeys method right after activating the input field at position 9. This sends keystrokes directly to the currently active control. Here's your modified code:
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) Public Const MOUSEEVENTF_LEFTDOWN = &H2 Public Const MOUSEEVENTF_LEFTUP = &H4 Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8 Public Const MOUSEEVENTF_RIGHTUP As Long = &H10 'Declare sleep Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub EnterValue() SetCursorPos 250, 250 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '1 SetCursorPos 475, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '2 SetCursorPos 550, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '3 SetCursorPos 625, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '4 SetCursorPos 700, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '5 SetCursorPos 775, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '6 SetCursorPos 850, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '7 SetCursorPos 925, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 50 '8 ' 点击位置9激活输入框 SetCursorPos 1000, 350 mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 Sleep 100 ' 延长等待时间确保输入框完全激活 ' 输入"Ok" SendKeys "Ok" ' 若需要按回车确认,可添加此行 ' SendKeys "{ENTER}" End Sub
Key Notes for SendKeys:
- I added a slightly longer
Sleep 100after clicking position 9 to ensure the input field has time to become active before sending text. SendKeysworks for most basic scenarios, but it can fail if the portal window loses focus while the code runs. Make sure the portal is the active window when you run the macro.
More Reliable Alternative: keybd_event API
If SendKeys is finicky (common with custom web portals or apps), use the Windows API to simulate hardware key presses. Add these declarations and a custom text-sending function:
' 新增API声明 Public Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Public Const KEYEVENTF_KEYUP = &H2 ' 自定义函数发送文本 Sub SendText(text As String) Dim char As String For Each char In Split(StrConv(text, vbUnicode), Chr(0)) If char <> "" Then keybd_event Asc(char), 0, 0, 0 ' 按下按键 keybd_event Asc(char), 0, KEYEVENTF_KEYUP, 0 ' 松开按键 Sleep 10 ' 每个字符间的短暂延迟 End If Next char End Sub
Then replace the SendKeys line in EnterValue with:
SendText "Ok"
This method directly mimics physical key presses, making it more reliable for stubborn input fields.
Final Tips
- If your portal is a web page, consider using VBA with Internet Explorer or Selenium for automation instead of mouse clicks. This lets you target input fields directly by their ID/name, which is far more stable than relying on screen coordinates (which break if the window size changes).
- Adjust sleep times if needed: if the portal loads slowly, increase
Sleepvalues to ensure each action completes before moving to the next.
内容的提问来源于stack exchange,提问作者Dipal Shah

