Consulting

Results 1 to 9 of 9

Thread: Problem using "controls" command

  1. #1

    Problem using "controls" command

    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

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Have you tried to reference the particular slide that has the label 0_1 on it?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Quote Originally Posted by Aussiebear View Post
    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

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    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...
    Bild1.jpg

  6. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    331
    Location
    Suggest you provide pp file for analysis.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    Paul and John, thanks a lot for your help, that solved my issue

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •