Solved

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

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

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

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

752 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