In short, the issue I'm running into is that when using VBA to change font size during a presentation, the slide runs all animations for that slide a second time. Changing other qualities, such as font color or shape fill, the slide does not animate a second time.
The longer version: I'm creating a tool in PowerPoint for my trainers to be able to build quizzes and computer-based trainings. I'm using a template shape for each state of the button (correct, incorrect or unanswered), and then have .pickup and .apply to change the formatting of the shapes on the question slides. At runtime, the slide builds based on the animations, the user clicks an option and the shapes format immediately. However, the font size also changes to mirror the template shape. When I temporarily store the font size and reapply it after .apply, at runtime, the slide animates, the user clicks an option and the slide runs the animations again (albeit correctly with the new formatting). The code works, but certain changes cause the slide to animate a second time (text, size), and some changes don't (shape color, text color). It's this second animation of the slide I need to avoid.
Windows 7 Professional;
PowerPoint 2016
Instead of using .pickup & .apply, one thought was to change qualities of everything but the font size, (the fill and line qualities), but for the fill, alone there are 20+ qualities, and that's before getting into number of gradient stops, shadows, etc. I need my developers to have the freedom of formatting the shape as they want that my code can quickly pull from.
.SlideShowSettings.ShowWithAnimation - setting to 'false' at the start of the code and 'true' yielded no change in results. (Attempt shown in code below.)
.SlideShowWindow.View.State - setting to 'paused' and 'running' also did not affect anything. (Attempt shown in code below.)
All objects on the slide are set to fade in automatically upon showing the slide. The simplified code here is set to run when a shape is clicked and shows what works and what doesn't work. I've commented lines in and out so I can try different things.
Sub ProcessResponse()
'These options do not prevent re-animation when in mid-presentation:
'ActivePresentation.SlideShowSettings.ShowWithAnimation = msoFalse
'ActivePresentation.SlideShowWindow.View.State = ppSlideShowPaused
'The following items do NOT cause the slide to run the animation again (desired result):
'Changing the object fill.
'ActivePresentation.Slides(1).Shapes(1).Fill.ForeColor.RGB = RGB(50, 25, 100)
'Changing text color.
'ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Font.Color.RGB = RGB(50, 25, 150)
'The following lines of code cause the slide to run the animation again (undesired result):
'Changing the font size.
ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Font.Size = 40
'Changing the text.
'ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text = "Changed"
'Closing the above attempts.
'ActivePresentation.SlideShowSettings.ShowWithAnimation = msoTrue
'ActivePresentation.SlideShowWindow.View.State = ppSlideShowRunning
End Sub
Setup: Slide with animations (i.e., fade in) that run "after next". 3-4 shapes, each with the above code called when clicked.
During the slide show:
Expected - animations, user clicks shape, the changes are made, no further actions/animations.
Actual Result - animations, user clicks shape, animations (changes are included).
Thank you for any and all help and considerations. Your time is appreciated.
Related
I think the title is self - explanatory.
I want to use a VBA macro to start the slideshow from a specific slide, for example the 5th one.
If i use
ActivePresentation.SlideShowSettings.Run.View.GotoSlide 5
it flashes the first slide for a brief moment. I want to get rid of that.
I'm thinking that it might be possible to show the slideshow with a black screen only, change the slide and then show the slideshow correctly, but I don't know how to start it as a black screen.
Try this:
With ActivePresentation.SlideShowSettings
.RangeType = ppShowSlideRange
.StartingSlide = 5
.Run
End With
edit: .RangeType = ppShowSlideRange was missing.
I'm trying to have several slides with tables, each table has 3 columns, the last column is the "reference value" and I want it to be hidden during the presentation and show it only by pressing a button or a hyperlink, each row individually. I think it's possible because I'm really new at coding and I have managed to do it by changing the cell's text format from white (which is the table's background color) to red, but I can only do it for all the tables at once, and I need individual values on each one of them. (I hope I'm making myself clear). This is what I have done so far:
Sub format()
Dim s As Slide
Dim oSh As Shape
Dim oTbl As Table
For Each s In ActivePresentation.Slides
For Each oSh In s.Shapes
If oSh.HasTable Then
Set oTbl = oSh.Table
With oTbl.Cell(2, 3).Shape.TextFrame.TextRange
.Text = "4500-9000"
.Font.Size = 12
.Font.Color = vbRed
End With
End If
Next
Next s
End Sub
But this will change the same cell on every table I have, I want it to change specific cells in specific tables one by one, since they all have different valued. I know I could do this with animations, but I'd rather do it this way.
EDIT: It would be great if, instead of pressing a button, I could get the data by hovering the pointer over the empty cell, and have it hidden away again when I hover the cursor off the cell.
In any case, whenever I do any change to the presentation during slideshow, the change will still be there at the end, which means It would only work once and then I would have to fix and hide all the values again, is there a way to restore the changes done during the presentation when it ends?
you could adpt your sub and call it from another procedure by passing a reference to the table and cell you want to process like this:
Sub FormatTableCell(oTbl As Shape, lRow As Long, lCol As Long)
With oTbl.Cell(lRow, lCol).Shape.TextFrame.TextRange
.Text = "4500-9000"
.Font.Size = 12
.Font.Color = vbRed
End With
End Sub
For example:
With ActivePresentation
FormatTableCell .Slides(1).Shapes("MyTable"), lRow:=1, lCol:=1
End With
Regarding the triggers for hover over and hover out, this is a tricky aspect of PowerPoint. There IS a trigger to run code when hovering over a shape but there is NOT a trigger for hovering out of a shape. To achieve the latter, you could put a transparent rectangle in the back layer of your slide and use that to spoof the hover out trigger by linking a hover over macro to it. Write your code and use the Insert / Action / Mouse Over function to trigger your VBA procedure with a signature like this:
Public Sub FormatThisTable(oTbl As Shape)
Note that this method only passes the shape (a table in your case) and not the cell the mouse is hovering over. The only way I can see you could achieve that would be to use a lot of very complex Windows APIs to detect the mouse cursor position relative to the table's on-screen coordinates.
Alternative approaches could either be to ungroup the table to a set of separate shapes or create cover shapes for each cell you need to show/hide and manage their visibility properties using the mouse in/out technique above.
For the last point, you will need to use application level events which requires code in a class module and this a good article to show you how:
http://www.pptfaq.com/FAQ00004_Make_your_VBA_code_in_PowerPoint_respond_to_events.htm
Step 1
Launch PowerPoint and open the PPTX file that contains the rows you want to hide. Click the appropriate slide in the Slides pane to the left of the screen.
Step 2
Double-click the spreadsheet on the slide, which will allow you to edit it. Select the rows that you want to hide. Click the “Home” tab and locate the “Cells” section. Click the “Format” option, which will display a list of available features.
Step 3
Place the pointer over the “Hide & Unhide” listing in the “Visibility” section. Click the “Hide Rows” option to hide the selected rows. Click outside the spreadsheet to return to the PowerPoint slide.
I have some code which changes all texts in a chart object to a specific size and font. Thing is the first time one runs the code it works like a charm. But if I change any part of the text within the chart and then re-run the code nothing happens.
E.g I run the code, then change the title heading to size 15 and font arial, then rerun macro, and nothing happens.
What can be wrong?
My code
With Selection
.Format.TextFrame2.TextRange.Font.Size = 10
.Format.TextFrame2.TextRange.Font.Name = "Times New Roman"
.Format.TextFrame2.TextRange.Font.Bold = msoFalse
End With
When you apply the fonts/sizes to the ChartArea in order to propagate them down to the individual pieces, Excel stores that info at the CharArea level. If you make a change to the one of the components (ChartTitle, Axis, etc.) and try to run your code again, there is no change on the ChartArea. Seems that Excel does not propagate those changes "back up". This makes sense since now the different items are styled differently.
The easiest way to deal with this is to reset the styles before you make your changes. ClearToMatchStyle applied to the Chart (i.e. ActiveChart or Selection.Parent in your context) will do it. It appears it will also make the change if you use a different font size or actually make a change to one of the ChartArea.Format properties (e.g. Size, Name, etc.).
Code for the reset option
ActiveChart.ClearToMatchStyle
With Selection
.Format.TextFrame2.TextRange.Font.Size = 12
.Format.TextFrame2.TextRange.Font.Name = "Times New Roman"
.Format.TextFrame2.TextRange.Font.Bold = msoFalse
End With
I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.
I have a userform called ImageForm, and this is what I'm doing right now:
Sub Button_Clicked()
ImageForm.Picture = LoadPicture("appropriate_image_path")
ImageForm.Show
End Sub
Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.
I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.
Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)
Thanks!
Here is a neat little trick based on one of my posts
The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image
SNAPSHOT (DESIGN TIME)
SNAPSHOT (RUN TIME)
CODE
Private Sub UserForm_Initialize()
With Frame1
'~~> This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'~~> Change the values of 2 as Per your requirements
.ScrollHeight = .InsideHeight * 2
.ScrollWidth = .InsideWidth * 9
End With
With Image1
.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
.BorderStyle = fmBorderStyleNone
.PictureSizeMode = fmPictureSizeModeClip
End With
End Sub
Is it possible to trigger a shape animation from VBA code?
pom
Pom,
In any version less than powerpoint 2010, no it does not appear possible: http://www.pptalchemy.co.uk/vba_Triggers.html.
However, if you are using powerpoint 2010, MSDN details a straightforward demo for triggering shape animations in powerpoint:
Sub TestShapeAnimation()
With ActivePresentation.Slides(1)
Dim shp1, shp2, shp3 As Shape
' This sets the initial shape, with which we will test the animation sequences.
Set shp1 = .Shapes.AddShape(msoShape12pointStar, 20, 20, 100, 100)
' This creates the animations.
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectFadedSwivel, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectPathBounceRight, , msoAnimTriggerAfterPrevious
.TimeLine.MainSequence.AddEffect shp1, msoAnimEffectSpin, , msoAnimTriggerAfterPrevious
' This acquires the animation... [i]
shp1.PickupAnimation
' [i] ... and applies it to another shape.
Set shp2 = .Shapes.AddShape(msoShapeHexagon, 100, 20, 100, 100)
shp2.ApplyAnimation
' Another shape creation / animation application.
Set shp3 = .Shapes.AddShape(msoShapeCloud, 180, 20, 100, 100)
shp3.ApplyAnimation
End With
End Sub
Let me know if you have further questions/thoughts -
~JOL
JackOrangeLantern's response is unfortunately not especially responsive to the original question.
JOL's response will help you copy animations on the timeline and apply them to different shapes, but it will not actually trigger them.
If you have Powerpoint 2010 or earlier, there is no direct way to actually trigger an animation with VBA. I am not very familiar with the newer versions of Powerpoint, but I don't think they have this feature either.
Actually triggering interactive animations seems to require an actual mouse click which can not be easily simulated in Powerpoint without some advanced and overly complicated coding that might involve actually messing with the Windows API. The best workaround I have found for this is to not send mouse clicks to the shapes on the slide but to instead send keyboard strokes.
I discovered this solution from PPT Alchemy Mouseover trigger an animation with vba code
Sub anim()
SendKeys("{TAB}")
SendKeys("{ENTER}")
End Sub
This code will activate the first interactive Trigger On the slide. To activate the second Trigger animation on the slide you would send the {TAB} key twice instead of once. This is definately the easiest way to activate trigger animations on a powerpoint slide. It is a shame there isn't a better way.
Follow the link for a more thorough explanation.
Take care. :)