本文介绍了从Excel VBA调用屏幕键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拉起屏幕键盘.
到目前为止,这是我的尝试:

I'm trying to pull up the on-screen keyboard.
Here are my attempts so far:

' Only needed for Test3
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub Test1()
    ' Run-time error'53':
    ' File Not found
    Dim RetVal As Variant
    RetVal = Shell("C:\WINDOWS\system32\osk.exe", 1)
End Sub

Sub Test2()
    ' Run-time error '432':
    ' File name or class name not found during Automation operation
    ActiveWorkbook.FollowHyperlink Address:="C:\Windows\System32\osk.exe"
End Sub

Sub Test3()
    ' No error. Nothing happens at all
    ShellExecute 0, vbNullString, "osk.exe", vbNullString, "C:\", 1
End Sub

Test2 from 此论坛.
来自此论坛的Test3.

Test2 from this forum.
Test3 from this forum.

我检查了osk.exe的路径是否正确.
我有一台Surface笔记本电脑/平板电脑,因此它具有触摸屏和触摸"功能.键盘(不同于osk).是造成问题的原因,还是Windows 10?

I checked the path to osk.exe is correct.
I have a Surface laptop/tablet, so it has a touch screen and a "touch" keyboard (different from the osk). Is that what's causing the issue or possibly it's a Windows 10 thing?

推荐答案

在64位操作系统上,请尝试

On a 64-Bit OS try this

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function Wow64EnableWow64FsRedirection Lib "kernel32.dll" (ByVal Enable As Boolean) As Boolean

Private Sub RunOsk_on64Bit()
Const SW_SHOWNORMAL = 1
    On Error Resume Next
    Wow64EnableWow64FsRedirection False
    ShellExecute 0, "open", "osk.exe", "", "C:\windows\system32\osk.exe", SW_SHOWNORMAL
    Wow64EnableWow64FsRedirection True
End Sub

找到此处,这可能是解释,请从链接中引用

Found here, this might the explanation, quote from the link

基本上,您正在调用osk.exe,但您的程序正在调用它from是一个32位应用程序.Windows不允许您调用64位OSK.exe从您的程序.任何人的评论似乎都错过了您的观点可以从运行"启动osk.exe,但可以在32位内调用它应用程序无法在64位Windows上运行.

Basically you are calling osk.exe, but your program you are calling it from is a 32-bit app. Windows won't allow you to call a 64-bit OSK.exe from your program. The comments appear to miss your point here, anyone can start osk.exe from Run, but call it from within a 32-bit application won't work in 64-bit Windows.

我正在开发使用唯一的屏幕键盘的软件解决方法是Wow64DisableWow64FsRedirection.

I am developing software that uses the on-screen keyboard, the only work around is Wow64DisableWow64FsRedirection.

更新:更精细"的版本可能看起来像这样

Update: A "nicer" version might look like that

Option Explicit

Type SHELLEXECUTEINFO
    cbSize As Long
    fMask As Long
    hwnd As Long
    lpVerb As String
    lpFile As String
    lpParameters As String
    lpDirectory As String
    nShow As Long
    hInstApp As Long
    lpIDList As Long
    lpClass As String
    hkeyClass As Long
    dwHotKey As Long
    hIcon As Long
    hProcess As Long
End Type

Public Declare Function ShellExecuteEx Lib "shell32.dll" _
                                       (lpExecInfo As SHELLEXECUTEINFO) As Long


Declare Function Wow64DisableWow64FsRedirection Lib "kernel32.dll" (ByRef ptr As Long) As Boolean
Declare Function Wow64RevertWow64FsRedirection Lib "kernel32.dll" (ByRef ptr As Long) As Boolean


Public Function KeyboardOpen()
  Dim shInfo As SHELLEXECUTEINFO
  Dim lngPtr As Long

   With shInfo
      .cbSize = Len(shInfo)
      .lpFile = "C:\Windows\Sysnative\cmd.exe" 'best to use Known folders here
      .lpParameters = "/c start osk.exe"
      .lpDirectory = "C:\windows\system32" 'best to use Known folders here
      .lpVerb = "open"
      .nShow = 0
   End With
   Call Wow64DisableWow64FsRedirection(lngPtr)
   Call ShellExecuteEx(shInfo)
   Call Wow64RevertWow64FsRedirection(lngPtr)
End Function

基于

Based on the information in MSDN it might be more reliable to use Wow64DisableWow64FsRedirection and Wow64RevertWow64FsRedirection functions instead.

这篇关于从Excel VBA调用屏幕键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!