如何在Microsoft Visual Basic Express 2010中创建和删除(或移动)自由形状

时间:2015-05-16 19:59:53

标签: vb.net excel-vba basic vba excel

我已经设法通过一组VBA命令在Microsoft Excel中创建和删除多个自由形状,并且希望对Microsoft Visual Basic Express 2010执行相同的操作,但实际上无处可去! Excel中的创建代码类似于:

With Sheet1.Shapes.BuildFreeform(msoEditingAuto, triXArray(1), triYArray(1))
    For cnt = 2 To 4
        .AddNodes msoSegmentLine, msoEditingAuto, triXArray(cnt), triYArray(cnt)
    Next
End With

triXArray()和triYArray()是点的X和Y坐标的数组,cnt是循环项目的计数器(它是一个三角形)。

1 个答案:

答案 0 :(得分:2)

这是从VB.Net

在Excel中创建和删除自由形状的方法

您需要在.ConvertToShape()之后将自由形式指定给形状。这样,你可以用它来工作(删除,移动)

Imports Excel = Microsoft.Office.Interop.Excel
Imports MsoEd = Microsoft.Office.Core.MsoEditingType
Imports MsoSg = Microsoft.Office.Core.MsoSegmentType

Public Class Form1
    '~~> Define your Excel Objects
    Dim xlApp As New Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As Excel.Worksheet
    Dim shpFF As Excel.FreeformBuilder
    Dim Shp As Excel.Shape

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '~~> Add a New Workbook
        xlWorkBook = xlApp.Workbooks.Add
        '~~> Work with first sheet
        xlWorkSheet = xlWorkBook.Sheets(1)

        '~~> Create the Freeform shape
        shpFF = xlWorkSheet.Shapes.BuildFreeform(MsoEd.msoEditingCorner, 360, 200)

        With shpFF
            '~~> Add the nodes. You can use your array method as well
            .AddNodes(MsoSg.msoSegmentCurve, MsoEd.msoEditingCorner, _
             380, 230, 400, 250, 450, 300)
            .AddNodes(MsoSg.msoSegmentCurve, MsoEd.msoEditingAuto, 480, 200)
            .AddNodes(MsoSg.msoSegmentLine, MsoEd.msoEditingAuto, 480, 400)
            .AddNodes(MsoSg.msoSegmentLine, MsoEd.msoEditingAuto, 360, 200)

            '~~> Convert it to shape and assign it
            Shp = .ConvertToShape()

            '~~> Display Excel
            xlApp.Visible = True
        End With

        MessageBox.Show ("Wait")

        '~~> Delete the shape
        Shp.Delete()
    End Sub
End Class