VB.Net: 在特定单元格中插入形状
VB.Net: Inserting Shape in Certain Cell
我正在尝试在特定单元格中插入一个形状,例如 (5,5)。我能够将形状变成 excel,但无法弄清楚如何将它放入 (5,5)。经过研究,我知道形状位于工作表中单元格的顶部。我还了解到 .Range
在这里可能会有帮助。
我只是不确定如何将这些拼图拼在一起才能使我的形状变为 (5,5)。
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, 17, 0, 15, 13)
此外,我是 vb.net 的初学者,所以如果您能把所有内容都简化一下,我将不胜感激!
编辑:
试过这段代码..但它把数字 7
放在 (5,5) 而不是形状中。
Dim aNew As MsoAutoShapeType = MsoAutoShapeType.msoShapeIsoscelesTriangle
xlWorkSheet.Cells(5, 5) = anew
也尝试过:
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Left, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top, 15, 13)
但收到错误
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred
编辑:有效的代码...
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, (xlWorkSheet.Cells(3, 5)).Left, (xlWorkSheet.Cells(3, 5)).Top, 25, 14)
使用 VBA 我会在 B2 中打印它,您可以使用高度和宽度来更改按钮的高度和宽度:
Dim button As Shape
Set button = ActiveSheet.Shapes("Button 1")
button.Top = Range("B2").Top
button.Left = Range("B2").left
button.Height = 50
button.Width = 100
或者在你的例子中:
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, Range("B2").left, Range("B2").Top, 15, 13)
类似的东西xlWorkSheet.get_range(xlWorkSheet.cells(5.5)).top
或 cells(5.5).top
发生 'System.Runtime.InteropServices.COMException' 类型的未处理异常
调试,用xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top
分解,so,xlWorkSheet
可以吗,xlWorkSheet.Cells(5, 5)
可以吗,xlWorkSheet.Range(xlWorkSheet.Cells(5, 5))
可以吗,哪里出错了,分解大语句,实际上从它们的组成部分开始,然后查看它们的 returns 并像您在此处所做的那样将它们链接在一起 xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Left, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top, 15, 13)
我们看不到您的 code/screen
我正在尝试在特定单元格中插入一个形状,例如 (5,5)。我能够将形状变成 excel,但无法弄清楚如何将它放入 (5,5)。经过研究,我知道形状位于工作表中单元格的顶部。我还了解到 .Range
在这里可能会有帮助。
我只是不确定如何将这些拼图拼在一起才能使我的形状变为 (5,5)。
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, 17, 0, 15, 13)
此外,我是 vb.net 的初学者,所以如果您能把所有内容都简化一下,我将不胜感激!
编辑:
试过这段代码..但它把数字 7
放在 (5,5) 而不是形状中。
Dim aNew As MsoAutoShapeType = MsoAutoShapeType.msoShapeIsoscelesTriangle
xlWorkSheet.Cells(5, 5) = anew
也尝试过:
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Left, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top, 15, 13)
但收到错误
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred
编辑:有效的代码...
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, (xlWorkSheet.Cells(3, 5)).Left, (xlWorkSheet.Cells(3, 5)).Top, 25, 14)
使用 VBA 我会在 B2 中打印它,您可以使用高度和宽度来更改按钮的高度和宽度:
Dim button As Shape
Set button = ActiveSheet.Shapes("Button 1")
button.Top = Range("B2").Top
button.Left = Range("B2").left
button.Height = 50
button.Width = 100
或者在你的例子中:
xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, Range("B2").left, Range("B2").Top, 15, 13)
类似的东西xlWorkSheet.get_range(xlWorkSheet.cells(5.5)).top
或 cells(5.5).top
发生 'System.Runtime.InteropServices.COMException' 类型的未处理异常
调试,用xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top
分解,so,xlWorkSheet
可以吗,xlWorkSheet.Cells(5, 5)
可以吗,xlWorkSheet.Range(xlWorkSheet.Cells(5, 5))
可以吗,哪里出错了,分解大语句,实际上从它们的组成部分开始,然后查看它们的 returns 并像您在此处所做的那样将它们链接在一起 xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Left, xlWorkSheet.Range(xlWorkSheet.Cells(5, 5)).Top, 15, 13)
我们看不到您的 code/screen