Jonathan Walker
Jonathan Walker

Reputation: 41

VBA to open Onscreen keyboard in 64bit Excel

I am trying to open the osk.exe from VBA within Excel 64 bit on Windows 10 64 bit.

I have pieced together the following code that works for 32bit Excel on 64bit Windows 10, but I don't know how to modify it to get it working again with 64bit Excel:

Option Explicit

Type SHELLEXECUTEINFO
    cbSize As LongPtr
    fMask As Long
    hwnd As LongPtr
    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 LongPtr
    hProcess As Long
End Type

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


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

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

   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

Sub OpenKeyboard()
Call KeyboardOpen
End Sub

Upvotes: 2

Views: 815

Answers (1)

Jonathan Walker
Jonathan Walker

Reputation: 41

I have found a solution. To get the 64bit Windows 10 On screen keyboard (osk.exe) to run, add the following code to a module to a 64bit Excel, then you can call OpenKeyboardSub from within your application:

Private Declare PtrSafe 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 OpenKeyboardSub()
    ShellExecute 0, vbNullString, "osk.exe", vbNullString, "C:\", 1
    End Sub

Upvotes: 2

Related Questions