PDA

View Full Version : Filling a shape with color



Roderick
12-15-2017, 04:40 PM
Windows 7, Word 2013.

I have a shape with a green background and using the following code I can fill it with a picture:

Sub InsertPictureDialog()
Dim strName As String
Dim fd As FileDialog
Dim FileChosen As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)
'get the number of the button chosen
FileChosen = fd.Show


If FileChosen <> -1 Then
'didn't choose anything (clicked on CANCEL)
MsgBox "You chose cancel"
' Clean up
Set fd = Nothing
Exit Sub
Else
'gets the full path and filename
strName = fd.SelectedItems(1)
End If


'selects the shape
ActiveDocument.Shapes.Range(Array("Green Box")).Select
' now fills the shape with the picture
With ActiveDocument.Shapes("Green Box").Fill
.Visible = msoTrue
.UserPicture strName
End With
' Clean up
Set fd = Nothing
'deselects the shape and goes to the start bookmark
Selection.GoTo What:=wdGoToBookmark, Name:="start"
End Sub

I now want to return the shape to its original green color using the following code:

Sub ChangeToGreen()
'selects the shape
ActiveDocument.Shapes.Range(Array("Green Box")).Select

With ActiveDocument.Shapes("Green Box").Fill
.Visible = msoTrue
.BackColor.RGB = RGB(67, 176, 42)
' .ForeColor.RGB = RGB(67, 176, 42)
End With
'deselects the shape and goes to the start bookmark
Selection.GoTo What:=wdGoToBookmark, Name:="start"

End Sub

It selects the shape correctly but fails to change it back to the RGB requested.

I can manually change it by selecting the shape then going to Format (Drawing Tools)>Shape Fill>More Fill Colors... followed by the Custom RGB.

As you can see above, I've experimented with changing BackColor.RGB to ForeColor.RGB but with no success.

I know I'm going wrong somewhere but I cannot spot the error.

Could I be pointed in the right direction, please?

macropod
12-15-2017, 06:17 PM
All you need to change the background colour is:

Sub ChangeToGreen()
ActiveDocument.Shapes("Green Box").Fill.BackColor.RGB = RGB(67, 176, 42)
End Sub
No selecting, etc. is necessary. Whether you'll see any change depends on what patterning/gradient settings the shape has.