views:

952

answers:

2

hi can you show a vb code for excel-2007 format->recolor->set transparent color on an a image inserted...

by the way, forgot to mention that excel-2007 record macro does not record this stuff otherwise i would not ask it here... :)

A: 

I recorded a macro in Excel 2003, and this is what I got:

Selection.ShapeRange.PictureFormat.TransparentBackground = msoTrue
Selection.ShapeRange.PictureFormat.TransparencyColor = RGB(5, 95, 209)
Selection.ShapeRange.Fill.Visible = msoFalse

I think this will work in Excel 2007 also, since everything tends to be forwards-compatible.

Gary McGill
i did the following it does not work? foreach (Excel.Shape sh in ws.Shapes) { sh.PictureFormat.TransparentBackground = Microsoft.Office.Core.MsoTriState.msoTrue; sh.PictureFormat.TransparencyColor = ColorTranslator.ToOle(Color.FromArgb(5,95,209)); sh.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse; }
I don't see why not, since RBarry's answer uses essentially the same method. Was it just that the actual color wasn't there in the picture? My sample picture was randomly-chosen, and so the color was too.
Gary McGill
that could be, that's why I used black (probably the most common rgb color).
RBarryYoung
A: 

OK, here is a Macro that I wrote in Excel 2007 that works:

Sub Macro3()
    Dim NewSheet As Worksheet, oldws As Worksheet
    Set oldws = ActiveWorkbook.ActiveSheet

    Dim i As Integer, obj As Shape
    Dim picFmt As PictureFormat

    Set NewSheet = Worksheets.Add
    NewSheet.Range("A1").Value = oldws.Name
    i = 3
    NewSheet.Range("A2").Value = "Name"
    NewSheet.Range("B2").Value = "Link Type"
    For Each obj In oldws.Shapes
        NewSheet.Cells(i, 1).Value = obj.Name
        NewSheet.Cells(i, 2) = obj.Type
        Set picFmt = obj.PictureFormat
        With picFmt
            NewSheet.Cells(i, 3) = .TransparencyColor
            'set Black as the Transparent color'
            .TransparencyColor = RGB(0, 0, 0)
        End With
        i = i + 1
    Next
End Sub
RBarryYoung