我正在使用excel vba从访问文件中复制文本选择(我希望不详细了解原因)。我将其在do while循环中进行,该循环应按下选项卡键(工作),然后复制数据(失败),将其放入剪贴板(工作),然后将剪贴板信息设置为变量(工作),然后,然后将其设置为出于调试目的,进行调试。这是要循环循环到一个"基本点",我可以100%使用选项卡,以导航到表格的其他部分。请参阅代码:
AppActivate ("Microsoft Access - Filename that is constant")
X = 0
Do While X < 14
Application.SendKeys "{TAB}", True
Application.SendKeys "^C", True
Sleep (500)
mydata.GetFromClipboard
cb = mydata.GetText
Debug.Print (cb)
If Len(cb) = 5 Then
X = 14
End If
X = X + 1
Loop
Set mydata = Nothing
我已经尝试使它起作用,但无济于事。我在做什么错,或者也许是一个更好的解决方案?
尽管我讨厌Sendkeys
,并且想知道我是否应该问您,但是由于您说不要问为什么要问为什么,我会关闭陷阱。:p
尝试这个小修复...如果有效,那就意味着,您需要给它一些时间,然后再发布下一个sendkeys命令。
Sub Sample()
'
'~~> Rest of your code
'
Application.SendKeys "{TAB}", True
Wait 2
Application.SendKeys "^{C}", True
'
'~~> Rest of your code
'
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
什么是更好的解决方案?
使用API,如下所示。这不是直接回答您的问题,但它解释了该概念的工作原理。
如此应用,就像这样
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Dim Ret As Long
Sub Sample()
Ret = FindWindow(vbNullString, "Microsoft Access - Filename that is constant")
If Ret <> 0 Then
MsgBox "Window Found"
Else
MsgBox "Window Not Found"
End If
End Sub
如果您希望像FindWindow
,FindWindowEx
和SendMessage
一样擅长API,则获取一个工具,可以为您提供有关系统过程,线程,窗口和窗口消息的图形视图。对于EX:uuSpy
或Spy++
。另一个示例说明了如何使用此API。
我弄清楚了。我从这里复制了代码:http://www.vbaexpress.com/forum/showthread.php?38826-sendinput()-in-excel-64bit我将vkkeymenu更改为vbkeycontrol,将" F"键更改为" C"。我知道可以简化少的行可以简化,但是如果它的作用就像说"如果不破产,不要修复它",我宁愿不觉得它。代码:
Private Declare PtrSafe Function SendInput Lib "user32" (ByVal nInputs As LongPtr, pInputs As Any, ByVal cbSize As LongPtr) As LongPtr
Private Declare PtrSafe Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Type KeyboardInput ' creating variable type
dwType As Long ' input type (keyboard or mouse)
wVk As Integer ' the key to press/release as ASCSI scan code
wScan As Integer ' not required
dwFlags As Long ' specify if key is pressed or released
dwTime As Long ' not required
dwExtraInfo As Long ' not required
dwPadding As Currency ' only required for mouse inputs
End Type
' SendInput constants
Private Const INPUT_KEYBOARD As Long = 1
Private Const KEYEVENTF_EXTENDEDKEY As Long = 1
Private Const KEYEVENTF_KEYUP As Long = 2
' Member variables
Private TheKeys() As KeyboardInput
Private NEvents As Long
Sub testage()
ReDim TheKeys(0 To 3)
With TheKeys(0)
.dwType = INPUT_KEYBOARD 'operation type
.wVk = vbKeyControl 'press CTRL key
End With
With TheKeys(1)
.dwType = INPUT_KEYBOARD ' operation
.wVk = VkKeyScan(Asc("C")) 'press chr key
End With
With TheKeys(2)
.dwType = INPUT_KEYBOARD 'operation type
.wVk = VkKeyScan(Asc("C"))
.dwFlags = KEYEVENTF_KEYUP 'release chr key
End With
With TheKeys(3)
.dwType = INPUT_KEYBOARD ' operation type
.wVk = vbKeyControl
.dwFlags = KEYEVENTF_KEYUP 'release CTRL Key
End With
Call SendInput(4, TheKeys(0), Len(TheKeys(0)))
Erase TheKeys
End Sub