VBA Check if Insert Key is Active

JValenti

New Member
Joined
Mar 7, 2018
Messages
11
Hello All,

So currently I have a macro that cleans up some data and the copies it so I can paste it into a different program. The formatting of this copied information needs to be very specific and if the "Insert" key is currently enabled it breaks the formatting when pasting the information into a different program.

Is there a way to disable the "Insert" key with a macro? At a very minimum does anyone know a way of VBA determining if the insert button is active, as then I can have a Msgbox pop up and warn them to manually disable it. The insert key is commonly used in the program in question so this error is more common that you may expect.

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Code:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long


Sub SetInsert(Value As Boolean)
' set the insert key .
Call SetKeyState(vbKeyInsert, Value)
End Sub


Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
' Retrieve the keyboard state, set the particular
' key in which you're interested, and then set
' the entire keyboard state back the way it
' was, with the one key altered.
Dim abytBuffer(0 To 255) As Byte
GetKeyboardState abytBuffer(0)
abytBuffer(intKey) = CByte(Abs(fTurnOn))
SetKeyboardState abytBuffer(0)
End Sub


Sub Macro1()
SetInsert (False)
'your code...........


End Sub
 
Upvote 0
Thanks a bunch, I also found another way on just a message box method:

Function GetInsert() As Boolean
'Return the Insert Value
GetInsert = CBool(GetKeyState(vbKeyInsert) And 1)
End Function


Sub CheckInsert()
If GetInsert() Then
MsgBox "Insert is ON!"
Else
MsgBox "Insert is OFF!"
End If
End Sub

If anyone ever wants to just inform the user.
 
Upvote 0
Sorry this would also need to be added prior to the Function declaration:

Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) _
As Integer
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top