You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA复制文本框至指定位置失败:粘贴对象不可见无报错

问题原因及解决方案

你的代码无法显示粘贴对象的核心问题是:Shape对象没有Paste方法,原代码中bonmb3a.Paste(550, 140, 47, 21)属于无效调用,虽然不会报错,但无法正确生成新的文本框对象。

以下是两种可靠的实现方式:

方式1:使用剪贴板粘贴+手动调整位置

通过Shapes.Paste方法粘贴剪贴板内容,再设置新文本框的位置和大小:

Dim bonmb3a As Shape, bonmb3b As Shape
Set bonmb3a = bow.Shapes.AddTextbox(msoTextOrientationHorizontal, 14, 333, 47, 21)
With bonmb3a
    .Name = "Building Overview Box 3A"
    With .Line
        .ForeColor.RGB = RGB(255, 192, 0)
        .Weight = 3
    End With
    With .TextFrame2
        .TextRange.Characters.Text = "3"
        .TextRange.ParagraphFormat.Alignment = msoAlignCenter
        With .TextRange.Characters.Font
            .Bold = msoTrue
            .Size = 12
        End With
    End With
    .Copy
End With
' 将剪贴板内容粘贴到当前画布的Shapes集合
Set bonmb3b = bow.Shapes.Paste(1) ' 参数1表示将新对象置于顶层
' 设置新文本框的目标位置和尺寸
With bonmb3b
    .Left = 550
    .Top = 140
    .Width = 47
    .Height = 21
    .Name = "Building Overview Box 3B"
End With

方式2:使用Duplicate方法(更简洁)

直接复制原文本框生成新对象,无需依赖剪贴板:

Dim bonmb3a As Shape, bonmb3b As Shape
Set bonmb3a = bow.Shapes.AddTextbox(msoTextOrientationHorizontal, 14, 333, 47, 21)
With bonmb3a
    .Name = "Building Overview Box 3A"
    With .Line
        .ForeColor.RGB = RGB(255, 192, 0)
        .Weight = 3
    End With
    With .TextFrame2
        .TextRange.Characters.Text = "3"
        .TextRange.ParagraphFormat.Alignment = msoAlignCenter
        With .TextRange.Characters.Font
            .Bold = msoTrue
            .Size = 12
        End With
    End With
    ' 直接复制当前文本框,返回新的Shape对象
    Set bonmb3b = .Duplicate
End With
' 调整新文本框的位置和尺寸
With bonmb3b
    .Left = 550
    .Top = 140
    .Width = 47
    .Height = 21
    .Name = "Building Overview Box 3B"
End With

内容的提问来源于stack exchange,提问作者Geographos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:37:12