如何在Excel工作表的特定位置添加文本框?

How to add a textbox at a specific position in an ExcelSheet?

您好,我尝试在 Excel 的特定位置添加文本框sheet。就像从 A34 到 J39。但我不知道该怎么做。

这是我尝试过的:

excelSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 100, 100, 200, 50);

有效,但文本框出现在 sheet 上的随机位置。

Microsoft.Office.Tools.Excel.Controls.TextBox
textBox1 = this.Controls.AddTextBox(
this.Range["A1", "B2"], "textBox1");
textBox1.Text = "Sample text";

这来自 MSDN 站点。但是不会工作,因为你需要一个新的方法,我不应该添加一个新的方法......

Range rng = UsedArea.Cells[rownum, cellnum];

txtbox = sheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, rng.Left, rng.Top, txt.Width / 2, rng.Height); 

这是来自 SO 但它说范围 属性 无法更改...... 也许它与办公室互操作 15 的东西有关。

因此,任何帮助或建议都将非常有用,感谢您的宝贵时间。

编辑: 在 excelsheet 的任何位置获取文本框的工作 C# 代码:

    Excel.Range rng = excelSheet.get_Range("A34:J39");
    float left = (float)rng.Left;
    float top = (float)rng.Top;
    float width = (float)rng.Width;
    float height = (float)rng.Height;

    excelSheet.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, left, top, width, height).Select();

使用VBA:

Sub CoverRange()
    Dim r As Range
    Dim L As Long, T As Long, W As Long, H As Long
    Set r = Range("A34:J39")
    L = r.Left
    T = r.Top
    W = r.Width
    H = r.Height
    With ActiveSheet.Shapes
        .AddTextbox(msoTextOrientationHorizontal, L, T, W, H).Select
    End With
End Sub

创建 Shape 后,您还可以重新调整其大小和位置。你能把它改编成你的代码吗??