首页 > 解决方案 > 如何在 Excel VBA 中绘制带轮廓的楔形?

问题描述

我想在 Excel(Office 365 ProPlus 的桌面版本)中使用 VBA 绘制一个圆的扇区(即楔形)。下面的代码给了我正确的形状,但在弯曲部分只有一个轮廓:

Dim myArc as Shape
Set myArc = ActiveSheet.Shapes.AddShape(msoShapeArc, 200, 100, 100, 100)
With myArc
.Adjustments(1) = 0
.Adjustments(2) = 45
.Fill.Visible = msoTrue
End With

直边没有轮廓,只有曲线。 我怎样才能在形状周围得到一个轮廓? 我应该使用不同的形状msoShapeArc吗?我尝试使用下面的代码更改线条颜色、粗细和可见性,但它只影响弯曲边缘,而不影响直线边缘。

With myArc
.Line.ForeColor.RGB = RGB(0, 0, 0)
.Line.Weight = 1
.Line.Visible = msoTrue
End With

我已经能够找到有关一般形状属性的文档,但没有找到哪些属性适用于哪种形状,以及它们如何实际控制其外观。

标签: excelvbadrawing

解决方案


根据https://docs.microsoft.com/en-us/office/vba/api/excel.adjustments

由于每个可调整形状都有一组不同的调整,因此验证特定形状的调整行为的最佳方法是手动创建形状的实例,在打开宏记录器的情况下进行调整,然后检查记录的代码。

这对我来说创建了一个馅饼楔:

Sub WedgeTest()
    Dim ws As Worksheet, shp As Shape
    Set ws = ActiveSheet
    
    Set shp = ws.Shapes.AddShape(msoShapePie, 50, 50, 100, 100)
    With shp
        .Adjustments.Item(1) = -90 'degrees from 3 oclock
        .Adjustments.Item(2) = -45 'degrees from 3 oclock
        .Fill.Visible = msoFalse
        With .Line
            .Visible = msoTrue
            .ForeColor.RGB = RGB(255, 0, 0)
            .Transparency = 0
        End With
    End With
End Sub

请注意,从 3 点开始的度数是正数(顺时针)或负数(逆时针),但不能超过绝对值 180,因此顺时针 +190 度将设置为 -170(逆时针)。


推荐阅读