从Excel导出时如何去除图像的边框?

时间:2019-04-12 12:15:00

标签: excel vba

这是我的代码将图像从excel导出到文件

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

原始图像不存在边框,但是在结果文件中存在图像的边框大:

enter image description here

从excel导出时如何保留原始图像?

2 个答案:

答案 0 :(得分:1)

这会有所帮助吗? (未试用)

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

答案 1 :(得分:1)

边框位于要用于导出图像的图表上,而不是图像本身上。所以:

    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

您只需在即时窗口(VBE中的 Ctrl + G )中运行ActiveSheet.ChartObjects.Add 0, 0, 100, 100并观察默认设置即可清楚地看到这一点。 ChartObject-包括轮廓。