Search code examples
vbapowerpoint

How to pass string user input (RGB) to VBA


I have a userform with RGB values to be assigned to shapes that will be later created by the macro:

Private Sub UserForm_Initialize()

    With lstChosenColor
        lstChosenColor.AddItem "RGB(0, 0, 0)"
        lstChosenColor.AddItem "100, 100, 100" 'different from the above to show one alternative of my trials
    End With

End Sub

However I do not know how to insert the selected value into the macro itself. I tried in many ways:

    Dim lstChosenColor As String 'somewhere else i saw it as Long, tried but without success
'   Dim ChosenColor As String
'   ChosenColor = lstChosenColor 'I tried this one, too



        If lstChosenColor = False Then
            MsgBox "No Color Selected"
        Else: New_Shape.Fill.ForeColor.RGB = lstChosenColor.Selected 'I tried also 'New_Shape.Fill.ForeColor.RGB = lstChosenColor and 'New_Shape.Fill.ForeColor.RGB = RGB(lstChosenColor)
        End If



            Set New_Shape = myDocument.Shapes.AddShape(Type:=msoShapeOval, Left:=Shp_Cntr - ((Shp.Width + 2) / 2), Top:=Shp_Mid - ((Shp.Width + 2) / 2), Width:=Shp.Width, Height:=Shp.Width)
'            New_Shape.Fill.ForeColor.RGB = RGB(lstChosenColor) 'I commented this as it is part of a repeating block for a various number of shapes and I thought I could assign the RGB value above

Could anyone please advise?


Solution

  • If you use (eg) "100, 100, 100" as the value you can do something like:

    Dim v, arr
    
    v = lstChosenColor.Value              'get selected value
    
    arr = Split(Replace(v, " ", ""), ",") 'remove any spaces and split to array 
    
    'assign each array element as an argument to RGB()
    New_Shape.Fill.ForeColor.RGB = RGB(CLng(arr(0)), CLng(arr(1)), CLng(arr(2)))