Running macro in PowerPoint slideshow does not run unless done manually first - vba

I'm creating a slideshow to display some stats from a sql table. The macro does not want to run unless I run it manually once so I created a simple test to see if it was something with the sql call. Doing this in PPT 2016.
New presentation with 3 slides, a "title page" and two for data. Created a text box on slides 2 & 3. Saved as .pptm
Sub OnSlideShowPageChange()
Dim i As Integer
i = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
If i = 1 Then Call ChangeData
End Sub
Sub ChangeData()
If ActivePresentation.Slides(2).Shapes("TextBox 1").TextFrame.TextRange.Text = "Answer 1" Then
ActivePresentation.Slides(2).Shapes("TextBox 1").TextFrame.TextRange.Text = "Answer A"
ActivePresentation.Slides(3).Shapes("TextBox 1").TextFrame.TextRange.Text = "Answer B"
Else
ActivePresentation.Slides(2).Shapes("TextBox 1").TextFrame.TextRange.Text = "Answer 1"
ActivePresentation.Slides(3).Shapes("TextBox 1").TextFrame.TextRange.Text = "Answer 2"
End If
End Sub
Close the presentation. Open presentation and run the slideshow. It cycles through the 3 slides and repeats as I want but the "data" is not changing. Esc slideshow and manually run the ChangeData macro. Start slideshow again. Now it cycles through the slides and changes the data as desired.
Any ideas as to why it requires a macro to be run first? I would like to go straight to the slideshow or better yet use the ppt viewer.
Edit:
After looking around more I found this thread How do you run vba code when changing slides in powerpoint?
So, added an ActiveX TextBox and moved off slide with the following code for the control
Private Sub TextBox1_Initialize()
End Sub
Interestingly on the test presentation I have to click on the title slide to kick off the slideshow but when I include the control on my actual presentation it works just fine, without having to click on the first slide. I also tested saving my presentation as a ppsm and it still works. Can modify the sql data and it updates the slide table as desired.
I incorporated Sam's suggestion of Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow) and also tested as orig and both work. I also tested by adding an ActiveX Label but that did not work, had to be a TextBox or maybe other but not as a Label.

Related

ppActionRunMacro not working when converted to addin(ppam)

I want to execute a piece of code when the user clicks a rectangle(shape) in the powerpoint slideshow mode . I added a sample code , when the shape names "timelimit" is pressed the script(macro) "correctAns" should be executed. This is working fine when run on the pptm file but when I converted it to ppam file(addin) it is giving the following error
"Runtime error-2147188160 (80048240): ActionSetting(unknown member): Invalid request"
Sub test_action()
With ActivePresentation.Slides(1).Shapes("timelimit").ActionSettings(ppMouseClick)
.Action = ppActionRunMacro
.Run = "correctAns"
End With
End Sub
Sub correctAns()
MsgBox ("correct!!!")
End Sub
I found some similar questions on different forums but couldn't get any solution. Please provide me some suggestions to solve this issue
The PPAM has no slides/shapes, so you'd need to make sure that whatever presentation you're running this in has a shape on slide 1 named "timelimit". Then you'd have the problem of getting the click action to run the macro from within the PPAM.
It'd likely be a lot simpler to add this to your presentation and save it as a PPTM:
Sub correctAns()
Msgbox "correct"
End Sub
Then assign this as the macro to run when the shape is clicked.

Pause VBA Word macro, allow user to make a selection, and restart where it left off

I have a requirement for VBA script in Microsoft Word to pause so that the user can select text that will be copied to the clipboard so that it can exported to an Excel file. The user will make a number of selections and finally indicate he/she is done when the contents of the clipboard will be copied to a template Excel file.
I have the code working to copy each selection to the clipboard and then all rows to the Excel file. But I need assistence in figuring out how to pause the code to allow the user to make the selection and then restart the code to copy the selection to the clipboard. I am able to get the userform with toggle switch to switch states and labels when pressed. But have not figured out how to pause the VBA code to allow the user to navigate to the next section of the Word document for the next selection.
The Stakeoverflow question/answer below appears to address this requirement but I have not been able to get it to work. It appears that the code is incomplete.
Pause VBA macro, allow user to make a selection, and restart where it left off
Can someone provide example VBA code that accomplishes this?
Your assistence is much appreciated as I have been beating my head against the wall and it is starting to hurt!
There's no way in VBA to "pause" a macro. Code must run to completion... Unless there's a command for user input.
Input can be requested via the InputBox and MsgBox methods, but those block access to the document because they're modal. A UserForm, however, can be set to display as non-modal, meaning it stays on top, but doesn't block access to the document or the application features. Since you're already working with a UserForm, this can be implemented relatively easily.
In the small example below, the Continue button runs the code to perform an action on the user selection. When Done is clicked the entire code is exited and the form unloaded.
Code behind the user form
Option Explicit
Private Sub cmdContinue_Click()
Debug.Print Selection.Range.Text
End Sub
Private Sub cmdDone_Click()
Me.Hide
End Sub
Private Sub UserForm_Activate()
'Position the form near the top-left of the window
'So that the user can work with the document
Me.Top = Application.ActiveWindow.Top + 50
Me.Left = Application.ActiveWindow.Left + 50
End Sub
Code in a regular module
Option Explicit
Sub DisplayModeless()
Dim frm As frmModelessForInput
Set frm = New frmModelessForInput
frm.Show False 'Display as non-modal
Set frm = Nothing
End Sub

powerpoint linked object update flickers

After much hunting the last couple days, I've managed to get my powerpoint to update its excel link (which itself has data from an autoupdate query off our SQL db).
I open the excel file, then the powerpoint.
I have Excel autosaving every minute (will be every 10 when go live).
the powerpoint has this in module 1
Sub SlideShowNextSlide(ByVal Wn As SlideShowWindow)
ActivePresentation.UpdateLinks
With Application.ActivePresentation
If Not .Saved And .Path <> "" Then .Save
End With
End Sub
Sub OnSlideShowPageChange(ByVal objWindow As SlideShowWindow)
ActivePresentation.UpdateLinks
With Application.ActivePresentation
If Not .Saved And .Path <> "" Then .Save
End With
End Sub
I set the slideshow to loop until ESC.
the VBA does (almost all the time) run.
But when it runs, it loads the slide with the refreshed data.. and then in the last instant before the slide changes to the next one, you see the old data flicker onto the screen.
That's why I put that save after the updatelinks, hoping it would make the update stick for the next cycle.. but no joy.
Is there some sort of cache it uses while the slideshow is running? Can it be updated? Somehow have the running slideshow 'reload' from the now saved file.
If I have to I'll have the slideshow stop every hour and restart using some sort of Windows macro.. but that would be pretty ducktape-y.

How do you run vba code when changing slides in powerpoint?

I'm trying to reset the contents of some text boxes and labels when I change slides, but I'm struggling to get it to work. I've come up with this after doing a lot of googling and searching, but it doesn't seem to work. I'm trying to use the OnSlideShowPageChange event in PowerPoint 2013 and 2016, but it seems to have no effect. I'm not used to working with PowerPoint vba, so I might be doing something completely wrong.
Edit: I've managed to find an alternative method of resetting the label text. I've managed to get it to reset when the user focuses on one of the text boxes or moves their mouse over the label. But, I'm still curious to know the answer to this question; I'm not sure why my code isn't working.
I'll be greatful if anyone can point out any issues and how to fix them.
Here's what I've got so far:
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
Dim Sld As Slide
If Wn.View.CurrentShowPosition = 9 Then
'Perform Updates for slide #9
Set Sld = Application.ActivePresentation.Slides(9)
Sld.Shapes(TextBox_Form_Name).TextFrame.TextRange.Text = ""
Sld.Shapes(TextBox_Form_Email).TextFrame.TextRange.Text = ""
Sld.Shapes(TextBox_Form_Message).TextFrame.TextRange.Text = ""
Sld.Shapes(Label_Form_Info).TextFrame.TextRange.Text = ""
End If
If Wn.View.CurrentShowPosition = 18 Then
'Perform Updates for slide #18
Set Sld = Application.ActivePresentation.Slides(18)
Sld.Shapes(TextBox_Form_Name).TextFrame.TextRange.Text = ""
Sld.Shapes(TextBox_Form_Email).TextFrame.TextRange.Text = ""
Sld.Shapes(TextBox_Form_Message).TextFrame.TextRange.Text = ""
Sld.Shapes(Label_Form_Info).TextFrame.TextRange.Text = ""
End If
End Sub
I've also tried putting the shape names in speech marks, but that doesn't seem to help.
By the way, I need the code to work in both PowerPoint 2013 and 2016.
Here's an answer from the PowerPoint FAQ at http://www.pptfaq.com
Suppose your code that depends on the OnSlideShowPageChange( SHW as SlideshowWindow ) event works when run from within VBA or when you launch the presentation from within PowerPoint, but not when you start the show by doubleclicking the icon for the PPS or PPSM. The slide show launches normally, but the code in your OnSlideShowPageChange subroutine never runs.
Solution
Add an Active-X control (from the Developer tab) on first slide (drag it just off the slide if you don't want it visible during the slide show).
This forces VBA to initialize when the presentation starts, so the event gets triggered and your code runs.

Execute menu commands in powerpoint

I have a custom plugin that was made for powerpoint and has a functionality to export the current slide as HTML5. It doesn't support exporting the entire PPT so basically I would have to go slide by slide and export.
My question is, can I write something in VB that can execute a menu command, finish, next slide, execute menu command etc?
I don't even know if VB would be the correct language to use. I've never written anything in it.
VBA might be simpler since it's built into PowerPoint.
If you know the name of the command bar and the control on the command bar that you want to launch:
Sub LaunchTheCommand()
Dim oCmdbar As CommandBar
Set oCmdbar = Application.CommandBars("CommandBarName")
oCmdbar.Controls("ControlName").Execute
End Sub
View | Toolbars will show you the names of your toolbars.
This could help you work out the right name for the individual controls:
Sub ShowTheControlNames()
Dim oCmdbar As CommandBar
Dim oCtl As CommandBarControl
' for example, let's look at the Standard toolbar:
Set oCmdbar = Application.CommandBars("Standard")
For Each oCtl In oCmdbar.Controls
Debug.Print oCtl.Caption
Next
End Sub
Note that your code won't work on non-English versions of PowerPoint ... the menu names are different.