PDA

View Full Version : [SOLVED:] Problem using "controls" command



OnkelHo27
06-01-2023, 06:15 PM
Hello everybody,
I have a slide with many labels and named them "o_1", "o_2" and so on. I would like to access them using a loop. I found the "Controls" command as a potential useful tool. However, it does not work. I do not get any error messages, but the code is not executed. A simple example:
This works:


If o_1.BackColor = 0 Then
MsgBox "Hello World!"
End If

This does not:


If Controls("o_" & 1).BackColor = 0 Then
MsgBox "Hello World!"
End If


Any ideas why this is not working?

Thanks a lot!

Felix

Aussiebear
06-01-2023, 10:02 PM
Have you tried to reference the particular slide that has the label 0_1 on it?

OnkelHo27
06-02-2023, 03:04 AM
Have you tried to reference the particular slide that has the label 0_1 on it?

Unfortunately, the problem still remains if I reference the slide like


If Slide123.Controls("o_" & 1).BackColor = 0 Then
MsgBox "Hello World!"
End If

Paul_Hossler
06-02-2023, 09:35 AM
Assuming that you're talking about Userform controls, this seems to work. If not, then attach a small example

Only real thing I can see that you did not 'dot' the 'Controls(...) to the Userform



Option Explicit


Sub drv1()
Dim i As Long
Dim s As String

Load UserForm1

With UserForm1

For i = 0 To .Controls.Count - 1
s = "o_" & (i + 1)
.Controls(s).BackStyle = fmBackStyleOpaque
.Controls(s).BackColor = RGB(0, 255, 0)
Next i

'alternate - close to yours
i = 2
.Controls("o_" & i).BackColor = RGB(255, 0, 0)

.Show

End With


End Sub

OnkelHo27
06-02-2023, 09:48 AM
Thanks a lot for your answer Paul! The labels are not on a Userform, but integrated on a slide (see image below). I did not understand that it is just for userforms. Is there something similar if labels are not part of a Userform, but just placed on a slide? The problem in my case is that each field you can see is a label and I have this slide multiple times. I don't want to call each label on its own, but use a loop instead such that I can access labels o_1, o_2, o_3 etc. by one for loop...
30845

June7
06-02-2023, 11:41 AM
Suggest you provide pp file for analysis.

Paul_Hossler
06-02-2023, 06:50 PM
OK, I'm GUESSING that you're trying to make some sort of scoreboard using PowerPoint in presentation mode

You never said how you plan to trigger events, so I'm going with the way I do it. Maybe it'll give you ideas. Ask questions if you want

I think you thought the 'Shapes' on the slide were 'Controls' - something different

Sub Init() is required to start - click on it's shape on the slide.

It also has the answer to your original question

There are other ways to automatically run Init, but they require CustomUI. Not hard but can be tricky

Sub ShapeSub() responds to clicks. Use Insert, Action, Run Macro




Option Explicit


Sub Init()
Dim i As Long

For i = 1 To ActivePresentation.Slides(1).Shapes.Count
With ActivePresentation.Slides(1).Shapes(i)
If .Name Like "o_*" Then ' or If .Name = "o_" & i Then
.TextFrame.TextRange.Text = 100
End If
End With
Next i
End Sub




Sub ShapeSub(oShape As Shape)
oShape.TextFrame.TextRange.Text = oShape.TextFrame.TextRange.Text - 1
End Sub




30847

John Wilson
06-03-2023, 12:57 PM
If they really are ActivX labels (and they probably don't need to be) you can reference like this


Sub chex()
Dim oshp As Shape
Dim L As Long
'NOTE this is the real slide number not the activX container name
For Each oshp In ActivePresentation.Slides(1).Shapes
If oshp.Type = msoOLEControlObject Then
For L = 1 To 2
If oshp.Name = "o_" & CStr(L) Then MsgBox oshp.OLEFormat.Object.BackColor
Next L
End If
Next
End Sub

Much easier not to use controls

OnkelHo27
06-03-2023, 01:52 PM
Paul and John, thanks a lot for your help, that solved my issue :yes