Solved

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

Posted on 2016-09-14
5
40 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 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
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 13

Author Comment

by:Stacy Brown
Comment Utility
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 13

Author Comment

by:Stacy Brown
Comment Utility
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 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
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 13

Author Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now