Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Use a content control to change the fill color of a shape

Posted on 2016-09-14
5
Medium Priority
?
184 Views
Last Modified: 2016-09-16
I would like to use a Combo Box Content Control to give people a list of colors to choose from and then change the fill color on a circle in the document to whatever color they choose.  Colors are white, yellow, orange, red, and black.  I am able to make the Combo Box Content Control but I'm unsure of the rest.

I've worked in VBA but I usually find or record a macro to start with.  Can anyone give me a hand here or point me in the right direction?
0
Comment
Question by:Stacy Brown
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 41798937
Unfortunately the events for content controls are a little lacking. For this instance, the best option is to use the OnExit event which only runs when you leave the content control.

The below code assumes the shape name is "Oval 2" and the Title of your content control is "Select Shape Color". Adjust as necessary. You'll see there are 2 methods for setting the color - by using one of the color constants or using the RGB value.

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Title = "Select Shape Color" Then
        With ActiveDocument.Shapes("Oval 2").Fill
            Select Case ContentControl.Range.Text
                Case "Blue": .ForeColor = vbBlue
                Case "Red": .ForeColor = vbRed
                Case "Yellow": .ForeColor = vbYellow
                Case "Green": .ForeColor = vbGreen
                Case "Orange": .ForeColor.RGB = RGB(255, 128, 0)
            End Select
        End With
    End If
End Sub

Open in new window

0
 
LVL 14

Author Comment

by:Stacy Brown
ID: 41799834
Wayne...thanks so much!  I'll test it out and let you know how I fare.  The code looks good though.  More to come!
0
 
LVL 14

Author Comment

by:Stacy Brown
ID: 41799917
Code doesn't seem to be working.  Shape color isn't changing.  Shape shows as Oval 3 in the Selection pane.  Shape is set to Inline and is in a table.

Here is the code:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    If ContentControl.Title = "Select Shape Color" Then
        With ActiveDocument.Shapes("Oval 3").Fill
            Select Case ContentControl.Range.Text
                Case "White": .ForeColor = vbWhite
                Case "Yellow": .ForeColor = vbYellow
                Case "Orange": .ForeColor.RGB = RGB(255, 128, 0)
                Case "Red": .ForeColor = vbRed
                Case "Black": .ForeColor = vbBlack
            End Select
        End With
    End If
End Sub

Open in new window


Here is a screenshot of my content control properties:
Content Control Properties Screenshot
I'm not against using a classic combo or list box if you think it would be easier.
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 41800631
Hi Stacy,

I'm unable to upload an example as it's ".docm" is not one of the allowable extensions. But as I mentioned the events for content controls is lacking and the only on available to use is the OnExit event, which only fires once you leave the control. It won't do anything while it is still selected.

An ActiveX control is easier to handle as it has a Change event which fires when the selection changes. Although you do need a line of code to populate the list on document open. Below is the code you will use which will go in the ThisDocument module.

Private Sub Document_Open()
    ComboBox1.List = Array("White", "Yellow", "Orange", "Red", "Black")
End Sub

Private Sub ComboBox1_Change()
    With ActiveDocument.Shapes("Oval 3").Fill
        Select Case ComboBox1.Text
            Case "White": .ForeColor = vbWhite
            Case "Yellow": .ForeColor = vbYellow
            Case "Orange": .ForeColor.RGB = RGB(255, 128, 0)
            Case "Red": .ForeColor = vbRed
            Case "Black": .ForeColor = vbBlack
        End Select
    End With
End Sub

Open in new window


Wayne
0
 
LVL 14

Author Comment

by:Stacy Brown
ID: 41801506
That did it Wayne!   ActiveX control was so easy!  Thanks so very much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question