Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-14
5
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 47

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 47

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Suggested Courses

688 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