3
votes

Good day to VBA experts out there,

I'm trying to write a code that search a shape color and then position it into somewhere.

example

If shape.Fill.ForeColor.RGB = RGB(210, 210, 210) Then 

With shape
        .Width = 700 
        .Height = 20
        .Top = 80
        .Left = 30
        .Name = "TitleTextBox"
        .Fill.Visible = msoFalse
        .Fill.Transparency = 1.0  '(somehow when I type 1.0 it will become 1#, not sure why on this also)
End With

End if

How I use my code:

I using this code to add a fill color Grey = RGB(210,210,210) to certain shapes then clear the color and reposition the shape to where I wanted

However, when I run this code again, the shape that has added the Grey color will get reposition again, even though it doesn't have any fill to it.

Somehow, I felt the shape has remembered the color apply to them, which isn't what I wanted.

Appreciate if any one can provide me some insight on how I can overcome this problem.

Thanks

1
Vityata's answer should do the job for you. The reason you're having problems is that even though the shape's fill isn't visible, its fill color attribute is still the original grey color that you're searching for, so it gets found again. - Steve Rindsberg
Hi Thanks Steve for your reply. I'm trying to clear the original grey color. when I try to fill the color manually with "No Fill" it works, is there include the code as VBA ? because I doesn't want to replace the shape with another color as even with white, it might overlay other text on slide and cover them. - Gerald Tow

1 Answers

1
votes

Try like this:

Option Explicit
Public Sub TestMe()

    Dim sh      As Shape
    Dim sld     As Slide

    Set sld = Application.ActiveWindow.View.Slide

    For Each sh In sld.Shapes
        If sh.Fill.ForeColor.RGB = RGB(210, 210, 210) Then
            With sh
                .Fill.ForeColor.RGB = RGB(0, 0, 0)
                .Width = 700
                .Height = 20
                .Top = 80
                .Left = 30
                .Name = "TitleTextBox"
                .Fill.Visible = msoFalse
                .Fill.Transparency = 1#
            End With
        End If
    Next sh
End Sub

Once you find the grey shape, make sure that you change its color to RGB(0,0,0). Thus, it will not be grey any more and will not be taken into account, if you run the code again. I have done it with this line:

        .Fill.ForeColor.RGB = RGB(0, 0, 0)