|
本帖最后由 lss001 于 2024-7-30 15:34 编辑
https://blog.csdn.net/weixin_30889885/article/details/97699269
'可参考以上链接
'把以下代码保存为vbs文件
Call RunStr
Sub RunStr()
Set eApp = CreateObject("excel.application") '创建excel对象
Set wb = eApp.Workbooks.Add '添加表格
Set vbaBas = wb.VBProject.VBComponents.Add(1) '添加模块
'winApi函数
str1 = "Private Declare Sub keybd_event Lib ""user32.dll"" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)"
With vbaBas.CodeModule '添加模块代码
.InsertLines 1, str1 '添加winapi函数
.InsertLines 3, "Sub myMacro()"
.InsertLines 4, " keybd_event &H5B, 0, 0, 0"
.InsertLines 5, " keybd_event &H12, 0, 0, 0"
.InsertLines 6, " keybd_event &H47, 0, 0, 0"
.InsertLines 7, " keybd_event &H47, 0, 2, 0"
.InsertLines 8, " keybd_event &H12, 0, 2, 0"
.InsertLines 9, " keybd_event &H5B, 0, 2, 0"
.InsertLines 11, "End Sub"
End With
eApp.Run "myMacro" '执行代码
wb.VBProject.VBComponents.Remove vbaBas '删除模块
wb.Saved = True '忽略保存提示!
wb.Close '关闭表格
eApp.Quit '退出excel
Set vbaBas = Nothing '释放对象
Set wb = Nothing
Set eApp = Nothing
End Sub
'虚拟键盘参考链接
https://learn.microsoft.com/zh-cn/windows/win32/inputdev/virtual-key-codes |
评分
-
1
查看全部评分
-
|