VBA - Powerpoint - Handling onClick Event - vba

I am developing a macro for PowerPoint in vba
My goal is to have one single Sub that 1-6 shapes were manually assigned to.
So the sub is executed when a shape is clicked and should determine with a Select case statement which of the shapes was clicked exactly (and therefore run a specific script)
I am struggeling to find Code snippets that show how the onclick event is handled and how I can determine for which shape the „onclick is True“
Can you guys help me with this? Thanks in advance!

If you assign the following macro to each of the clickable shapes, it'll do what you're after in Slide Show view. Select the shape, choose Insert | Action and choose run Macro: HandleShapeClick.
You may need to modify it to run on Macs because of a bug in the VBA implementation there.
If you need something like this to function in Normal or Slide view, you'll need entirely different (and considerably more complex) code.
Sub HandleShapeClick(oSh As Shape)
Select Case oSh.Name
Case Is = "Rectangle 3"
MsgBox "You clicked Rectangle 4"
Case Is = "Rectangle 4"
MsgBox "You clicked Rectangle 5"
Case Is = "Rectangle 5"
MsgBox "You clicked Rectangle 3"
' etc, for each add'l clickable shape
End Select
End Sub

Related

How to run the VBA code while selecting an drop down menu item

In my MS Word form, there are some drop down menus. By default "Yes" is the green and "No" is the red in color while the file is opened. I would like to change the font color of the drop down menu items while selected, to be Red in case of "No" and to be Green in case of "Yes".
For this purpose I have created a Document_ContentControlOnExitsub currently only for the "Did the AA's performance drop after the incident? question in the following MS Word form. But the code is not running while selecting the "Yes" from the drop down menu. I can write and run VBA code in MS Excel but new to MS Word VBA, thus confused how to run the code. Can any one help?
I also would like to create a VBA code which will check and make sure that all "No" are Red and "Yes" are Green while the file is opened. So I think, there should be a VBA which will run during the MS Word file opening and iterate through all the Drop Down menus for that purposes. But this is secondary, if solved, bonus but not so important, at this moment it is important to me to change the font color during selecting menu items.
Here is the VBA code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
With ContentControl
If .Title = "incident" Then
Select Case .Range.Text
Case "Yes": .Range.Font.ColorIndex = wdRed
Case "No": .Range.Font.ColorIndex = wdBrightGreen
Case Else: .Range.Font.ColorIndex = wdAuto
End Select
End If
End With
End Sub
Here is the MS Word file:
https://drive.google.com/file/d/1Avjx8nubEIp9K9NwqtyfHvdohQEsQiP8/view?usp=sharing
Check which module your code is in.
Document_ContentControlOnExit is a document level event handler so it should be in the "ThisDocument" module.
Also note that in the tools area at the top of that module there are two drop down menus. These can be used to create event handlers for the document's objects and are useful to ensure that the syntax is correct.

How to find the label of a shape on a Powerpoint slide

Can someone please point me in the right direction to finding the label Powerpoint uses for each shape, active x control, etc... I've researched till I'm blue in the face. Here's what I'm talking about:
I have a very simple slideshow that asks a question in a text box. Just below the question I placed an active x control text box to get the user's answer. All I want to do is take the answer and append it to a text file. Here's the code:
Public Sub WriteAnswerToFile(slideNum As Integer, shapeNum As String)
Dim filePath As String
Dim objFSO As FileSystemObject
Dim objFile As Variant
filePath = "C:\Batch Files\Powerpoint\ButtonPushes\AnswerFile.txt"
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Batch Files\Powerpoint\ButtonPushes\AnswerFile.txt", ForAppending)
objFile.WriteLine (Application.ActivePresentation.Slides(slideNum).Shapes(shapeNum).TextFrame.TextRange)
objFile.Close
End Sub
From each slide, I pass the slide number and the shape designator to the sub routine. The shapeNum is a string because I found that it worked (no better reason than that) for me to pass something like "TextBox 1" as the param. It all works absolutely great if I know the Shape(designator); as in Shape(TextBox1). For the life of me I cannot figure out how to pass the text input by the user into the active x control text box. I have no idea what the designator is. The property sheet for the text input box calls it TextBox1. The Code sheet calls it TextBox1. When I pass that parameter, it prints the question I asked, not the answer, to my text file. I don't know how to call the input text box in my code.
My code prints "How did you hear about us?" to my text file when my code runs with Call WriteAnswerToFile(2, "TextBox 1"). I'm sure that textbox is called textbox 1; it's the first textbox on the slide. I just don't know what Powerpoint labels the user input box.
If there is some sort of Powerpoint scripting or layout page that will define all the shapes in the slidewhow, I'd sure like to be pointed that direction.
You just need to refer to the form part of the Object since this is not a normal shape.
objFile.WriteLine (Application.ActivePresentation.Slides(slideNum).Shapes(shapeNum).OLEFormat.Object.Text)
To clarify from your comment. I think that you have two "Text Box 1". One is "TextBox 1" (note the space) which is the normal textbox and the ActiveX control is "TextBox1" (no space).
If I create a new blank slide and first add a normal text box and then an ActiveX textbox and then run the following code:
For Each shp In Slide2.Shapes
Debug.Print shp.Name
Next shp
The Immediate window will show the following:
TextBox 1
TextBox1
By chance I happened to stumble upon the simplest answer to my question of "finding the label Powerpoint uses for each shape, active x control, etc.."
I am using the Office 16 (Office 365) suite that contains Powerpoint and would not be certain this feature is avaiable to other versions.
On the Home tab in Powerpoint there is an Editing submenu that contains a Select function. As pictured here:
When you click on Select, another submenu appears that shows a Selection Pane function. If you click on that, the selction pane shows up on the right hand of the screen. In that pane you will see all of the objects on the current slide and the names Powerpoint has given to each of them.
It shows there, the discrepancy I was having with calling TextBox(Space)1 and TextBox(NoSpace)1.
This works to be much more expedient for me to grab the name of the shape I want to call in my VBS Scripts.
I am thankful; however, for the time and frustration #Diederik Sieburgh saved me in allowing me to move forward with my project as it is now 2 weeks later that I stumbled upon this information.

Powerpoint slides: how to copy my combobox and textbox controls on one slide to a another slide, same presentation?

Powerpoint Slides: Trying to create duplicate slides so that user can, on each slide,
1. select an option
2. enter free text.
I have the following VBA and controls in Slide2, I want to copy everything, including combobox and textbox, from slide2 for any/all subsequent slides. I want an easy way to do this. just copying or duplicating the slides will not allow users to make selections or add free text on any slides after slide2 (the original). Although the controls still APPEAR on the subsequent slides, no selection or typing can be made. Subsequent slides contain the VBA script but not any controls copied over? I'm new-ish to this so not sure all of my language is correct here.(i used to know how to do all this stuff about 12 years ago, lol)
Please tell me there is a fast and easy way to do this! It's taken me all afternoon to teach myself the current script (combo/text boxes) and research a slide copy fix, to no avail.
Here's my simple script for the combo box and textbox from slide2.
Private Sub ComboBox1_DropButtonClick()
If ComboBox1.ListCount = 0 Then
With ComboBox1
.AddItem "Select One", 0
.AddItem "Accept", 1
.AddItem "Reject", 2
ComboBox1.ListRows = 3
End With
End If
End Sub
Private Sub TextBox1_Change()
End Sub
Not sure if I understand everything you want, but this duplicates slide 1, including all the code belonging to the controls in the slide:
ActivePresentation.Slides(1).Duplicate
Let me know if there's something I'm missing

Is it possible to identify the shape ID that triggers a mouseover on a powerpoint presentation?

Is it possible to identify the shape ID that triggers a mouseover on a powerpoint presentation.
What I want to do is have a series of shapes on a slide with titles, and when the user mouseovers a shape, I want to use VBA to show more details about the shape in another part of the page, possibly in a separate help text box. I could then use the selected shapes ALt Text to display in the help box. What i can't see is how to identify what shape has triggered the mouseover macro. Is this possible? If I can identify the shape that triggered the action, I could then get info about that shape.
The Shape object in PowerPoint does not allow you to interact with its events as you normally would with other objects in VBA code. The only two 'events' that are exposed are not actually VBA events. They are a special class called Actions, and they are 'ppMouseOver' and 'ppMouseClick'. If you know all the information about your shapes and their corresponding information beforehand, you could write some code to do what you want, but it would all be hard-coded, which is probably not what you want.
You could run a routine something like this when the presentation starts:
Sub SetActionsRoutine()
Shape1.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape1.ActionSettings(ppMouseOver).Run = "showInformation1"
Shape2.ActionSettings(ppMouseOver).Action = ppActionRunMacro
Shape2.ActionSettings(ppMouseOver).Run = "showInformation2"
...
...
End Sub
And then whenever you did a MouseOver of those shapes, one of the following routines would run.
Sub showInformation1()
myTextBox.Text = Shape1.AlternativeText
End Sub
Sub showInformation2()
myTextBox.Text = Shape2.AlternativeText
End Sub
This is very limited and requires that you write a subroutine for every shape on your slide. Probably not the approach you want, but again, with PowerPoint, your options are very limited.
New to StackOverflow, late to answer, but you can do this:
Sub RespondToShape(oSh as Shape)
MsgBox "You clicked " & oSh.Name
End Sub
Assign every shape you want to interact with an Action setting of Run Macro and choose RepondToShape as the macro.

Manipulating excel "autoshapes" with VBA

I am trying to write a macro in VBA (Excel) that is assigned to a Checkbox. Whenever the checkbox is clicked, an "autoshape" will change its "order" from "Send to Back" to "Send to Front".
Basically, I am trying to create a dashboard with multiple panels, so that users can access information without moving between sheets. Each panel will have a rectangular autoshape as its background and the components of the panel will be "grouped" within the autoshape.
Can this be done? I would greatly appreciate any ideas into writing the code.
Thanks,
I'm not quite following your larger goal, but in order to bring a shape to the front use this:
If MyCheckBox.Value = True Then
MySheetName.Shapes("MyShapeName").ZOrder msoBringToFront
End If
You should select your desired ZOrder movement from the MsoZOrderCmd enumeration and your code should be in the Change event routine for your checkbox control.
EDIT:
You could also refer to the shape by its index number. For example:
MySheetName.Shapes(0).ZOrder msoBringToFront
Also, to get the name of a shape, either click it and look in the Name Box in the upper left corner of Excel (below the toolbars), or iterate through all the shapes like so:
Sub Macro1()
Dim MyShape As Shape
For Each MyShape In Sheet1.Shapes
Debug.Print MyShape.Name
Next MyShape
End Sub