Solved

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

Posted on 2016-09-14
5
118 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

623 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