VBA to simulate ActiveX Control Click - vba

Using Sub OnSlideShowPageChange() , we can run a macro as we go to SlideShow Mode.
My objective is to recognise KeyPress and run macros accordingly during the SlideShow. For that, I can utilise this code:
Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If (KeyCode = vbKeyRight) Then 'Your code goes here
End Sub
In this case, I would have to click on CommandButton1 for the KeyPress to be recognised.
We need an ActiveX Item to make use of MSForms.ReturnInteger and we cannot directly make use of the ActiveX with Sub OnSlideShowPageChange()
Is there any way to collect KeyPress throughout the entire presentation without having to click on any ActiveX Buttons? Also, even if we click on it, the KeyPress is collected only for that specific slide and not in the subsequent slides.
I was trying to "Set Focus" on the CommandButton but that was futile. I tried tampering with keybd_event but was unable to fulfill the objectives of recognising keypresses and running an if-then condition after that. Also, SendKeys do not function in PowerPoint.
Is there any way to simulate the click of a CommandButton using VBA?

Related

Click/DoubleClick to open a Userform on Word

I was just creating a template on Word which uses content controls and I have a userform to help auto fill the data which is activated via a command key. You know how on excel VBA you can click a cell to make it refresh or open up a userform, or perform a series of tasks. On Word, can you double or single click a Content Control per say and have a userform pop up?
I think it would be pretty neat to be able to do that, open for any ideas that I should try out.
Unlike Content Controls, ActiveX controls have Click and DoubleClick events. You can add an ActiveX control (e.g., a button or a label) and use its Click event to do whatever you want:
And then simply, use something like this:
Private Sub lblTest_Click()
MsgBox "You clicked on a label."
End Sub
Private Sub btnTest_Click()
MsgBox "You clicked on a button."
End Sub
Result:
Hope that helps.

Drop-Down List Detached From ComboBox - Excel VBA

The drop down list of a ActiveX ComboBox detaches whenever I scroll down the page. How
can I fix it so it won't move?
Here's the VBA code designated for the comboBox.
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "DropDownList"
Me.ComboBox1.DropDown
End Sub
If you switch out your ComboBox to the one from the Forms toolbar, then it collapses the list as soon as you start scrolling. I think the "detaching" of the dropdown is the default behavior and I don't think you can influence that programmatically (as in here, too).
See this post on the different options for the comboboxes:
Run Macro When ComboBox is Clicked
Another option is to disable the mouse scroll, as in this post:
VBA Excel Combobox: drop-down list scrolling issue

How to run code when clicking a cell?

I'm aware that I can use Worksheet_SelectionChange but this doesn't do exactly what I want.
For example when I move the active cell with the arrow keys it will still run the code.
How do I only make it run the code when actually physically clicking on the cell?
You can detect left clicks as well. I answered a similar question here
Insert the following code inside the specific worksheet module (for example "Sheet1"):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If (GetAsyncKeyState(vbKeyLButton)) Then 'left mouse button
'do something here
End If
End Sub
additionally, you have to insert the following part on the top of a normal module (like the standard "Module1"):
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
That's it. The part "do something here" can be filled by your needs.
However, this has some flaws: If you do something that finishes with a click (e.g. a MsgBox), the next selection change with arrow keys will also fire the event and re-execute your stuff. To bypass this, you can add an extra "empty"
If (GetAsyncKeyState(vbKeyLButton)) Then 'left mouse button
'blank
End If
in the end of the selectionChange Sub. As I said, there are flaws, so this won't disable all unwanted behaviour by Excel. Another one is clicking somewhere else in Excel (e.g. choosing another ribbon) and changing the selection of cells per arrow keys afterwards. Haven't found a solution to that one unfortunately.
The left mouse click cannot be trapped with VBA for the Excel application. There are some methods to check globally if the left mouse button is pressed down but it does not seem simple and reliable from what I gather. See:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28343702.html which is unresolved (basically the same question you have)
SelectionChange cannot only work for the mouse: http://excel.tips.net/T003070_Mouse_Click_Event_in_VBA.html
After lots of searching nothing on the web is conculsive about this. This may not be the answer you're looking for but I don't think you'll find what you want.

How do I simulate a click event in VB Windows Forms

I just want to connect two arbitrary controls, so that if one is clicked, the other should act as though it's clicked - is this even remotely possible? it seems like it SHOULD be so easy, but the internet seems dry, unless I just don't know how to ask the question properly... I see a way to "click" a button control, but what if the target is not a button? - I don't know the name of any function that might be triggered by this control's click event, so I can't call it directly. I would guess there is some way of using Windows APIs, but I can't find anything that's nice, simple VB
Example
I click a Label control on the form. I want to handle that click event, run one line of code, then simulate a click event on an associated RadioButton control
Is this possible? How?
If you must, call (System.Windows.Forms.Controls.)Control.InvokeOnClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick%28v=vs.71%29.aspx
or even RadioButton.PerformClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.performclick.aspx
A better way would be to create a common Subroutine that would be called on click of either controls. This way clicking on controls will execute their own code which can differ, and some common code as well.
This is how you accomplish executing the same code regardless of which control event was fired.
Private Sub ClickMe()
'code to execute
End Sub
Private Sub label1_Click(...) ...
ClickMe()
End Sub
Private Sub rb_checked(...) ...
ClickMe()
End Sub

Excel - Display ComboBox DropDown by VBA

I need a workbook to display the Combobox List Dropdown when it opens.
The combobox in the Workbook is a form control, so a shape.
Cant seem to find the associated property.
If you are using ActiveX Controls then see the below, else if you are using Form Controls then replace them with ActiveX Controls if you want the drop down to happen via code. The below code will work for both ActiveX Controls in a Form as well as Worksheet. If the Control is on a worksheet then change ComboBox1.SetFocus to ComboBox1.Activate
Two ways I can think of...
Using a simple command
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.DropDown
End Sub
Using Sendkeys. Sendkeys are unreliable if not used properly.
Tried And Tested
Private Sub CommandButton1_Click()
ComboBox1.SetFocus
SendKeys "%{Down}"
End Sub
SCREENSHOTS
I have had plenty of crashes with .dropdown but find some success with
the SendKeys...
I consider best
UserForm combo box is as Above by Siddharth Rout
ComboBox1.SetFocus
SendKeys "%{Down}"
for some Combo boxes on a worksheets
CB.DropDown Is enough
.. Just as well as they have no setfocus or activate