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.
Related
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
I'm trying to add a checkbox to my worksheet using code:
Sub DropCheckboxOnSheet()
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
End Sub
When I run this code, I do get a checkbox added to the worksheet, however I also receive the message that Excel "Can't execute code in break mode". I get that my code adds an object to the object model and that this is why it breaks, but it also offeres me the option of continuing.
How do I tell VBA to continue after adding the object?
What i've tried:
Adding DisplayAlerts=False does not work.
MSDN offers no help either:https://msdn.microsoft.com/en-us/library/office/gg264133.aspx
I am not sure this is the safest solution but you will not get a error since adding "End" kills all macros. The only time i ever really use it is on progressbars. I would dig harder on the real issue using "On error resume next" is a bandaid. If you do use it make sure to restore your default error handling with "On error goto 0".
Sub DropCheckboxOnSheet()
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
End
End Sub
e.g.
Sub DropCheckboxOnSheet()
On error resume next
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
On error goto 0
End Sub
Changing the name of the sub might fix your issue. If you have the same sub in another open workbook it could be throwing the error.
I have two macros that I want to work when I push a button. I inserted the code needed in the button sub and made it so that it is not private. Then I opened the macro dialog box and ran the macro and it worked as expected. But, when I try to press the button, it comes up with an error that says "the requested member of the collection does not exist."
The code shouldn't really matter in this case since it is working correctly when I run it manually but here it is:
Sub CommandButton4_Click()
Selection.Tables(1).Select
Selection.Copy
Selection.PasteAndFormat (wdPasteDefault)
ActiveWindow.ActivePane.VerticalPercentScrolled = 24
'Some other Code here
End Sub
When I perform a debug, it says that the problem is in this line: Selection.Tables(1).Select
Then in my other instance I have this code:
Sub CommandButton3_Click()
Selection.SelectRow
Selection.Copy
Selection.InsertRowsBelow 1
Selection.Paste
End Sub
The code error appears to be in this line:
Selection.SelectRow
It says
The SelectRow method or property is not available because some or all of the object does not refer to a table.
I got the code by using the Macro Recorder.
If you have any ideas as to why this is happening, I would appreciate your help.
This code
Selection.Tables(1)
works only when the Selection (the cursor) is inside a table.
When you click the button, the button gets the focus, and the Selection is moved there. If the button is not inside a table, the code will fail.
It does work if you move the button inside the table, but that would probably look silly.
Replace your
Selection.SelectRow
with
'r being which ever row you want to Select
Selection.Table(1).Row(r).Select
You can bind your macro to a key combination. This won't change any focus you've set, and thus should solve your problem.
How to do this differs among different versions of Word, but the recent versions are explained here:
http://wordribbon.tips.net/T008058_Assigning_a_Macro_to_a_Shortcut_Key.html
I'm sorry for the newbie question, but I simply can't get this to work.
I've attached plenty of callbacks to powerpoint objects so that I can change things about them when they are clicked.
Private Sub CommandButton1_Click()
ActivePresentation.Slides(1).Shapes("Grey Box").ZOrder msoSendToBack
ActivePresentation.Slides(1).Shapes("Suggest Box").ZOrder msoSendToBack
CommandButton1.Visible = False
End Sub
This Works just fine. Breakpoints activate, code works.
I want to have some code called when the slide changes. I'd also like to have some code called when the presentation starts. There is plenty of advice on this topic, and I can't get any of it to work - probably because of the same mistake or an assumption I am making.
I have copied the following code into Module 1:
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If Wn.View.CurrentShowPosition = 3 Then
'Perform Updates for slide #3
Shapes("TextBox 51").ZOrder msoBringToFront
End If
End Sub
Sub OnSlideShowPageChange()
Dim i As Integer
i = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
If i <> 1 Then Exit Sub
MsgBox "Insert your code here"
End Sub
I've riddled this with breakpoints..... code is never called.
I have two questions:
How can I get either of these routines to be called when a slide changes?
Is there a good list of these automatic events somewhere?
Thanks,
Grommit
Finally figured this out..... thought I'd answer my own question in case anyone else runs into this.
I had 2 powerpoint presentations open at once. I WAS adding the code to the correct one, because when I closed both and reopened the one with the code, it worked immediately.
Not sure what on earth was going on, but it works now.
I have a presentation with 6 slides. Slide 5 contains vba / macro codes for rotatechart, change angle etc., The codes work well thru' vba editor 'RUN'in active document.(.ppt) but when put in 'Slideshow" mode codes don't work. Dbug msg: "Invalid request. no active document present". Pl help with code.
Sub MoveItInSlideShow()
With SlideShowWindows(1).Presentation.Slides(5).Shapes("Picture 2")
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
Application.Run "'Hora.ppt'!Slide7.CreateSpirograph"
end with
end sub
Thanks
This line is unnecessary, since you've already set a reference to the shape in the With statement previous:
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
But since you can't select anything in slideshow view, any attempt to .Select anything will cause an error, which is probably what you're seeing.
If the code here requires a selected shape, you'll need to rewrite it:
Hora.ppt'!Slide7.CreateSpirograph