Excel VBA自动化 - 在图像显示时单击某处

时间:2018-02-15 02:49:59

标签: excel-vba automation vba excel

我有以下excel vba代码,目的是" test.png"出现时,光标应移动到某处并左键单击。程序设置为运行10次,每次运行5秒,检查" test.png"显示与否。

但是,以下代码返回错误。怎么改写它,有什么想法吗?非常感谢!

'Declare mouse events
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPtr
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
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub leftclick()
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  Sleep 50
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Sub MacroAutoGUI()
img=("C:\Users\lawrence\Desktop\test.png")
for i= 1 to 10
  if img.show =true then
    SetCursorPos 38, 1048
    Call leftclick
    Sleep (5000) 
  end if
next
End Sub

1 个答案:

答案 0 :(得分:0)

您还没有声明变量@Service public class UsuarioServicio implements IUsuarioServicio{ // Some services } ,而且我不确定您是否希望它会从这行代码中加载图像。

img

目前你的代码假定你想要一个字符串变量,并且没有img=("C:\Users\lawrence\Desktop\test.png")方法,所以我希望你的代码会失败

Show

我建议您在所有VBA项目中使用Option Explicit和Option Strict;它会帮助你避免这些问题。有关详细信息,请参阅此处What do Option Strict and Option Explicit do?