Excel Shape.TextFrame.Characters.Insert 在 Excel 2007 上是否损坏?

发布于 2024-10-27 08:55:06 字数 889 浏览 2 评论 0原文

我得到了这段代码,我一直在使用 Excel 2003,它模仿了 Microsoft 网站上不久前给出的解决方案。如何向形状的文本框架添加超过 255 个字符:

For i = 0 To Int(Len(myTxt) / 255)
    .Characters(.Characters.Count + 1).Insert Mid(myTxt, (i * 255) + 1, 255)
Next i

这在 Excel 2007 下不起作用。有两个问题。

  1. 插入不能在字段末尾插入,它可以在现有字符处插入。所以 .Characters(.Character.Count).Insert 会起作用,即使这不是我想要做的。
  2. 它的操作方式是覆盖,而不是插入。无论我在何处应用“插入”,它都会覆盖现有字符。因此,在 (.Character.Count) 处插入将删除最后一个字符。

现在,我已经内置了逻辑,可以根据您使用的 Excel 版本进行不同的操作。但我找不到与这个问题相关的任何内容。这是一个已知的错误吗?有办法解决这种行为吗?

(在相关说明中,我也无法设置 .Characters(x,y).Font.Underline = True。)

编辑 在我的特定示例中,包含上面的代码类似于 With myWorksheet.Shapes(1) ,其中形状是文本框。它里面已经有文本,我需要向其中附加 myTxt (长度超过 255 个字符的 string)。这段代码在 Excel 2003 上运行没有问题。我在其他地方看到过这个问题,但正在从 Stack Overflow 寻找关于它的正式声明...

I've got this piece of code I've been using Excel 2003 which mimics a solution given on the Microsoft website from a while back. How to add more than 255 characters to a shape's text frame:

For i = 0 To Int(Len(myTxt) / 255)
    .Characters(.Characters.Count + 1).Insert Mid(myTxt, (i * 255) + 1, 255)
Next i

This just doesn't work under Excel 2007. There are two problems.

  1. Insert can't insert at the end of the field, it has insert at an existing character. So .Characters(.Character.Count).Insert will work, even though it's not what I want to do.
  2. It's operating as an overwrite, not an insert. Wherever I apply Insert it overwrites the existing characters. So an Insert at (.Character.Count) will delete the final character.

Now I've built in logic to operate differently depending on the version of Excel you're using. But I couldn't find anything relating to this issue. Is it a known bug? Is there a way to fix this behaviour?

(On a related note, I cannot set .Characters(x,y).Font.Underline = True either.)

EDIT In my particular example, the code above is contained with something like With myWorksheet.Shapes(1) where the shape is a text box. It already has text inside it and I need to append myTxt (a string longer than 255 characters) to it. This code worked without problem on Excel 2003. I've seen this problem referred to elsewhere, but looking for a formal statement on it from Stack Overflow...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我同意,你的代码在 2003 年可以工作,但在 2007 年就失败了。但令我惊讶的是,它居然还能工作。尝试引用 .Characters 集合中的“one more”字符应该抛出一个错误 - 如果你打破了上面的行,正如我认为你指出的那样在第 1 点中,您会看到具体是 .Characters(.Characters.Count + 1) 做到了这一点。失败的不是方法,而是不存在的成员,这对我来说似乎是正确的。

下面的代码可以在 2003 年和 2007 年实现您想要的功能。

Public Sub Loop_InsertTest()

    Dim MyWks   As Excel.Worksheet
    Dim MyTxt   As Shape
    Dim MyFrme  As TextFrame
    Dim i       As Long

    Const StartText As String = "This is a very, very, very, very, very, very, very, very, very, very, very, " _
                              & "very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, " _
                              & "very, very, very, very, very, very, very, very, very, very, long piece of text."
    Const MaxIterations As Long = 1000

    Debug.Print Len(StartText)

    Set MyWks = ThisWorkbook.Worksheets(1)
    Set MyTxt = MyWks.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 1000, 1000)
    Set MyFrme = MyTxt.TextFrame

    'Debug.Print TypeName(MyTxt), MyTxt.Name'
    MyFrme.Characters.Text = StartText
    MyFrme.AutoSize = True

    For i = 1 To MaxIterations
        Insert_ThisText MyFrme, " Now it's even longer."
    Next i

End Sub

Private Sub Insert_ThisText(pFrme As TextFrame, _
                            pstrText As String)
    Dim strRight    As String
    Dim i           As Long

    With pFrme
        For i = 0 To Int(Len(pstrText) / 254)
            strRight = .Characters(.Characters.Count).Text
            .Characters(.Characters.Count).Insert strRight & Mid(pstrText, (i * 254) + 1, 254)
            'Debug.Print Len(pstrText), .Characters.Count'
        Next i
    End With

End Sub

I agree, your code works in 2003 but fails in 2007. What's surprising to me, though, is that it works at all. Trying to reference "one more" character in the .Characters collection should throw an error--and if you break up your line above, as I think you're pointing out in point 1, you'll see that it's specifically .Characters(.Characters.Count + 1) that does. It's not the method that fails, it's the non-existent member, which seems right to me.

Here's code that does what you want in both 2003 and 2007.

Public Sub Loop_InsertTest()

    Dim MyWks   As Excel.Worksheet
    Dim MyTxt   As Shape
    Dim MyFrme  As TextFrame
    Dim i       As Long

    Const StartText As String = "This is a very, very, very, very, very, very, very, very, very, very, very, " _
                              & "very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, " _
                              & "very, very, very, very, very, very, very, very, very, very, long piece of text."
    Const MaxIterations As Long = 1000

    Debug.Print Len(StartText)

    Set MyWks = ThisWorkbook.Worksheets(1)
    Set MyTxt = MyWks.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 1000, 1000)
    Set MyFrme = MyTxt.TextFrame

    'Debug.Print TypeName(MyTxt), MyTxt.Name'
    MyFrme.Characters.Text = StartText
    MyFrme.AutoSize = True

    For i = 1 To MaxIterations
        Insert_ThisText MyFrme, " Now it's even longer."
    Next i

End Sub

Private Sub Insert_ThisText(pFrme As TextFrame, _
                            pstrText As String)
    Dim strRight    As String
    Dim i           As Long

    With pFrme
        For i = 0 To Int(Len(pstrText) / 254)
            strRight = .Characters(.Characters.Count).Text
            .Characters(.Characters.Count).Insert strRight & Mid(pstrText, (i * 254) + 1, 254)
            'Debug.Print Len(pstrText), .Characters.Count'
        Next i
    End With

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文