Excel Shape.TextFrame.Characters.Insert 在 Excel 2007 上是否损坏?
我得到了这段代码,我一直在使用 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 下不起作用。有两个问题。
- 插入不能在字段末尾插入,它可以在现有字符处插入。所以
.Characters(.Character.Count).Insert
会起作用,即使这不是我想要做的。 - 它的操作方式是覆盖,而不是插入。无论我在何处应用“插入”,它都会覆盖现有字符。因此,在
(.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.
- 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. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意,你的代码在 2003 年可以工作,但在 2007 年就失败了。但令我惊讶的是,它居然还能工作。尝试引用
.Characters
集合中的“one more”字符应该抛出一个错误 - 如果你打破了上面的行,正如我认为你指出的那样在第 1 点中,您会看到具体是.Characters(.Characters.Count + 1)
做到了这一点。失败的不是方法,而是不存在的成员,这对我来说似乎是正确的。下面的代码可以在 2003 年和 2007 年实现您想要的功能。
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.