VBA routines not tied to objects in Powerpoint - vba

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.

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.

issue with concatenated subs

DISCLAIMER: I'm not a developer, just an average guy trying to use VBA on his own project. First time I post on stackoverflow so forgive me if I'm violating any unwritten community rule..
Hi all, I'm trying to have a bunch of subs running in sequence, so the next one starts only when the previous one has finished.
The problem is that excel keep on crashing during the execution, while if I run each sub manually they have no issue at all.
So far I have been using this method: First I have a mini sub that assigns value=1 to the global variable "oneclick", and then calls the first macro of the chain
then, in each sub there is a tiny piece just before the end:
sub macro1()
...macro code...
if oneclick=1 then
call macro2
end if
end sub
and so on for all the subs until the final sub resets the oneclick variable to zero.
I have no idea why this keeps on crashing. I can see that actually none of these subs is really closing until the very end of the last one, would this hit any sort of code limitation? I would be actually happier to have a single master sub that directs the process instead of relying on a kludge like this! any suggestion?
EDIT:
oh wow already so many answers.. now I'm gonna try some of those. Answering some of your questions:
1) the crash is really something sudden, not even an error message, simply excel quits and reopens, in the same state as it was when the macro chain first started.
2) I agree with you guys that there should be a master sub calling the smaller ones, however last time I tried I got a series of problems because one macro needs to work on the results of the previous one and so on. How do you tell to vba to wait until the previous sub has ended?
It is best if you don't chain the macros, but instead call them from another sub. This way you will have a clear way of understanding what you are doing, in what order.
Sub AllOfIt()
macro1
DoEvents
macro2
DoEvents
macro3
End Sub
Sub macro1
...
End Sub
Sub macro2
...
End Sub
Sub macro3
...
End Sub
There is generally no need to worry about if something starts before the previous macro ended. That does not happen unless you do something like using Application.OnTime
What probably is happening is that oneclick is not defined with a Dim statement, so by default its defined at procedure level, meaning if you set oneclick it to 0 in another Sub, your setting another local/procedure level variable, and your variable in macro1 is unchanged, so your chain of macros never stops, which leads to a stack overflow/crash as stated in one of the comments above.
So either define oneclick as public variable (I recommend to define it as Boolean as it seems to basically be a switch .... True/False) or you pass the variable as argument down your chain of macros ( Sub Macro2(ByRef oneclick as Boolean) ).
All that said, as one of comments above stated, you can have all your chained subs in the main sub ( macro1 ) as they will only get executed one after the other, e.g.
Sub macro1()
Dim oneclick As Boolean
oneclick = True 'Need to get set to true to start
If oneclick Then Call macro2(oneclick)
If oneclick Then Call macro3(oneclick)
........
End Sub
Sub macro2(ByRef oneclick As Boolean)
oneclick = False 'One of your macros has to set oneclick to false to stop the chain/execution, probably under acondition
End Sub

how do I change the position of my image with coordinate ranges in vba

I'm trying to program a Chess-Game in VBA. I'd like to change the position of the figures with mousemove. I can already move them but It would be great if I could release the figure and the figure so jumped into the middle of the field.
I have no Idea how do solve the Problem
Thanks in advance
PS: Sorry for my bad english
Best case scenario - use the macro recorder. Copy the figure and paste it somewhere.
See the code. Then make sure that it works, only in the given range of the chessboard. Then make an event like this:
Option Explicit
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A1:H8")) Is Nothing Then Exit Sub
Debug.Print Target.Address
End Sub
And try to build the recorded code in Target.Address. The event is for the right click of the mouse. It should be put in the Worksheet part of the VBEditor.

Getting Values from Controls in MS Excel Custom Dialog Boxes

first question here. I've been racking my brains all morning for the simplest thing to do in Excel's VBA: get the value of what a user types into a text box (list box, etc.) in a custom dialog box and copy that, via a macro, into a cell. Not sure why it has to be so ridiculously tough. I've tried the .Value property, the .Text property, everything, but it all comes up blank.
I'd post an image of my dialog box, but I don't have the reputation yet to do so. At any rate, just assume it's a banking dialog box with an Amount text box and two buttons: Ok and Cancel.
And here's my code, cut down to the necessary bit:
Sub AddTransaction()
frmAddTransaction.Show
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = frmAddTransaction.txtAmount.Value
End Sub
Private Sub cmdCancel_Click()
Unload frmAddTransaction
End Sub
Private Sub cmdOk_Click()
Unload frmAddTransaction
End Sub
As I've said, I've tried the .Value and .Text properties and everything else that came to mind. I've tried throwing it up into a MsgBox or just sticking it into a cell and, at this point, I'm racking my brains not only to find the answer but to figure out why Microsoft made is so tough just to take a value from one thing and put it into a cell.
Same thing with the buttons: I'd like to know how to tell a macro what button was pushed. I've tried using any property that even remotely looks promising, as well as adjusting the .Top property so I can compare that number. Everything. Nothing is working.
Any help would be great. I've been stuck on this literally all morning.
You want to add the buttons to the form, not to the sheet.
So you have 1 button on the sheet that says "Enter Amount" which runs the following:
Private Sub CommandButton1_Click()
frmAddTransaction.Show
End Sub
and the user form has 3 elements: an input box (txtAmount) and two buttons.
Private Sub Cancel_Click()
Me.Unload
End Sub
Private Sub OK_Click()
'modify this to add it to the correct cell
ActiveCell.Offset(0, 0).Value = txtAmount.Value
Me.Unload
End Sub

Selecton.Rows.AutoFit does not work - why?

I have a problem with a piece of VBA code:
Sub Macro3()
Sheets("Output").Select
Rows("5:160").Select
Selection.Rows.AutoFit
End Sub
What - from my point of view it should do - is applying Autosize to Rows 5:160 within the Sheet "Output". I added the code to the sheet "Output" so whenever I open it it should be properly resized automatically. However, nothing happens. But if I manually select the rows and press CRTL+C+H+O+A to auto size the rows it works properly.
Does anyone has a guess where the mistake lies? Would be a huge help!
Thanks upfront!
whenever I open it it should be properly resized automatically.
For this you have to place the code in the ThisWorkbook code area and you have to use the Workbook_Open() event
See this
Private Sub Workbook_Open()
Sheets("Output").Rows("5:160").Rows.AutoFit
End Sub
SNAPSHOT