■■■ 为什么EbExecuteLine是执行字符串表达式无效???
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long
Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Timer1_Timer()
'通过ExecuteLine执行字符串表达式无效
Dim hotkey As String
hotkey = "E" '这是一个动态值,取自 用户设定的值,这里暂时假定指为E
If ExecuteLine("GetAsyncKeyState(vbKey" & hotkey & ")") Then
MsgBox "okE"
End If
'直接的 有效
If GetAsyncKeyState(vbKeyD) Then
MsgBox "okD"
End If
End Sub
请问该怎么写才能执行字符串表达式?
[解决办法]
vbKey" & hotkey 不行吧,
if hotkey = "E" then
vbkeyE
[解决办法]
改用VBScript控件msscript.ocx吧
[解决办法]
目前来说只知道EbExecuteLine 在 VB-IDE 中有效,这一点这个帖子有提到:
http://social.microsoft.com/forums/zh-CN/vbasiczhchs/thread/d6f7dbcf-1e05-49dd-8e02-264609c78f4b/
所以楼主要谨慎使用了。至于你的代码执行结果本身的问题,是因为你自认为EbExecuteLine会把你里面的语句的返回值返回来,事实上可没看见哪个文档这样说明过,你把语句换成我这样的就看得出来GetAsyncKeyState的返回值在按键前后的变化了
If ExecuteLine("msgbox GetAsyncKeyState(vbKey" & hotkey & ")") Then