Consulting

Results 1 to 2 of 2

Thread: Filling a shape with color

  1. #1

    Filling a shape with color

    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?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •