Home » excel » vba – How can remove border of image when export from excel?

vba – How can remove border of image when export from excel?

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is my code export image from excel to file

For Each oShape In ActiveSheet.Shapes
    strImageName = oShape.TopLeftCell.Row & "_" & oShape.TopLeftCell.Column
    If oShape.Type = msoPicture Then
        oShape.Select
        'Picture format initialization
        Selection.ShapeRange.PictureFormat.Contrast = 0.5: Selection.ShapeRange.PictureFormat.Brightness = 0.5: Selection.ShapeRange.PictureFormat.ColorType = msoPictureAutomatic: Selection.ShapeRange.PictureFormat.TransparentBackground = msoFalse: Selection.ShapeRange.Fill.Visible = msoFalse: Selection.ShapeRange.Line.Visible = msoFalse: Selection.ShapeRange.Rotation = 0#: Selection.ShapeRange.PictureFormat.CropLeft = 0#: Selection.ShapeRange.PictureFormat.CropRight = 0#: Selection.ShapeRange.PictureFormat.CropTop = 0#: Selection.ShapeRange.PictureFormat.CropBottom = 0#: Selection.ShapeRange.ScaleHeight 1#, msoTrue, msoScaleFromTopLeft: Selection.ShapeRange.ScaleWidth 1#, msoTrue, msoScaleFromTopLeft
        '/Picture format initialization
        Application.Selection.CopyPicture
        Set oDia = ActiveSheet.ChartObjects.Add(0, 0, oShape.Width, oShape.Height)
        Set oChartArea = oDia.Chart
        oDia.Activate
        With oChartArea
            .ChartArea.Select
            .Paste
            .Export ("D:\images\" & strImageName & ".jpg")
        End With
        oDia.Delete 'oChartArea.Delete
    End If
Next

Original Image not exist border, but in result file exist a border outsize of image:

enter image description here

How can keep original image when export from excel?

How to&Answers:

The border is on the Chart that you are putting the image in for the export, not on the Image itself. So:

    Set oChartArea = oDia.Chart
    'No need to Activate the ChartObject
    With oChartArea
        .ChartArea.Format.Line.Visible = msoFalse 'No Outline
        .ChartArea.Format.Fill.Visible = msoFalse 'No Background
        .ChartArea.Paste 'No need to use Select
        .Export ("D:\images\" & strImageName & ".jpg")
    End With
    oDia.Delete

You can see this more clearly by just running ActiveSheet.ChartObjects.Add 0, 0, 100, 100 in the Immediate Window (Ctrl+G in the VBE) and observing the default settings on the ChartObject – including the outline.

Answer:

Would this help? (untested)

'Your code....
oDia.Activate
Activesheet.Shapes(oDia.name).Line.Visible = msoFalse
With oChartArea
'Rest of code....