|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 lss001 于 2024-7-30 21:59 编辑
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetSystemMenu Lib "user32" ( _
ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare PtrSafe Function GetSubMenu Lib "user32" ( _
ByVal hMenu As Long, ByVal nPos As Long) As Long
'Private Declare ptrsafe Function GetMenuItemCount Lib "user32" ( _
ByVal hMenu As Long) As Long
Private Declare PtrSafe Function GetMenuItemID Lib "user32" ( _
ByVal hMenu As Long, ByVal nPos As Long) As Long
'Private Declare ptrsafe Function GetMenuString Lib "user32" Alias "GetMenuStringA" _
(ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, _
ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112, WM_CLOSE = &H10, WM_SETTEXT = &HC
Sub cmdfs() 'winapi操作CMD
Dim hwnd&, hMenu&, hSubMenu&, SelectAllID&, CopyID&, PasteID&, closeMenu&
Shell "cmd.exe", vbMinimizedFocus '打开CMD程序
Application.Wait Now + TimeSerial(0, 0, 2) '延时响应打开CMD程序
hwnd = FindWindow("ConsoleWindowClass", vbNullString) '获取cmd窗口句柄
hMenu = GetSystemMenu(hwnd, False) '获取cmd窗口系统菜单的句柄
hSubMenu = GetSubMenu(hMenu, 7) '获取"编辑"控件的句柄
Set hf = CreateObject("htmlfile") '或xmlfile
Set clip = hf.parentwindow.clipboarddata 'htmlfile剪贴板
sText = "cd C:\Users\Administrator\Documents" '设置cmd命令
clip.Setdata "Text", CStr(sText) '设置剪贴板数据
PasteID = GetMenuItemID(hSubMenu, 2) '获取"粘贴"项的ID
SendMessage hwnd, WM_SYSCOMMAND, PasteID, ByVal 0 '向cmd窗口发送粘贴命令
PostMessage hwnd, &H101, &HD, ByVal 0& '向cmd窗口发送回车键<Enter>
Application.Wait Now + TimeSerial(0, 0, 2)
SelectAllID = GetMenuItemID(hSubMenu, 3) '获取"全选"项的ID
SendMessage hwnd, WM_SYSCOMMAND, SelectAllID, ByVal 0 '向cmd窗口发送全选命令
CopyID = GetMenuItemID(hSubMenu, 1) '获取"复制"项的ID
SendMessage hwnd, WM_SYSCOMMAND, CopyID, ByVal 0 '向cmd窗口发送复制命令
closeMenu = GetSubMenu(hMenu, 6) '获取"关闭" 控件的句柄
SendMessage hwnd, WM_CLOSE, closeMenu, ByVal 0 '向cmd窗口发送关闭命令
Sheet1.Range("a1").Select
Sheet1.Paste '粘贴到A1单元格
Set hf = Nothing '释放剪贴板对象
Set clip = Nothing
End Sub
|
评分
-
2
查看全部评分
-
|