测试文本框值失败

发布于 2025-01-16 15:35:57 字数 1094 浏览 4 评论 0原文

所以这里(VBA Word):我选择一个特定的文本框并测试它的内容是否等于特定的字符串。所有这些都有效 - 但我总是得到错误的回应。当我发布 Debug.print 中的值时,显示“重要的事情”,这应该是真实的响应。触发文本的变化。从来没有。

(VBA Word 宏如下:)

     Sub testit()

        Dim WD as document
        Dim StringOne as string

        set WD = Application.ActiveDocument

        StringOne = "Something Important"
            'this line will always come back false    -  why?*
        If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne Then
                WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something More"
        Else
        ' This will say "Something Important"
                Debug.Print WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text
        '  And Just in case I typed something wrong let's enter it again into the textbox 
                WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something Important"
        End If

     End Sub

这是一个更复杂程序的简化。我将测试几个文本框,出于演示原因,我将在 WORD 文档中按名称调用这些文本框并进行相应更改。 IE:更改.fontsize、.bold。等等。相应地。根据 WORD 文档的布局,某些文本框会有不同的设置。无论如何,如果我无法测试文本框中文本的值......它永远不会改变。

So here goes (VBA Word): I'm picking a specific Text Box and testing to see if it's contents are equal to a specific string. All of this works - except I always get a false response. When I post the value in Debug.print is shows "Something Important" which should have been a true response. triggering a change in the text. Never does.

(VBA Word macro follows:)

     Sub testit()

        Dim WD as document
        Dim StringOne as string

        set WD = Application.ActiveDocument

        StringOne = "Something Important"
            'this line will always come back false    -  why?*
        If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne Then
                WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something More"
        Else
        ' This will say "Something Important"
                Debug.Print WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text
        '  And Just in case I typed something wrong let's enter it again into the textbox 
                WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = "Something Important"
        End If

     End Sub

This is a simplification of a more complex program. I will be tests several Text Boxes, Which I will call by name and change accordingly in the WORD document for presentation reasons. IE: Change the .fontsize, .bold. etc. Accordingly. Some textboxes will have different settings according to the layout of the WORD doc. Regardless, if I can't test the value of the text in a textbox... it will never change.

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

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

发布评论

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

评论(1

就像说晚安 2025-01-23 15:35:57

令人惊讶的是,这是一个字符映射问题。

If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne then

需要作为长度问题进行纠正

If Left(Wd.Shapes.Range(ShapeName).TextFrame.TextRange.Text, Len(StringOne)) = StringOne Then

在字符串末尾似乎有一些图形需要的空白或格式字符,您可以将其剪掉。

Surprisingly, it is a character mapping problem.

If WD.Shapes.Range("Text Box 2").TextFrame.TextRange.Text = stringOne then

needs to be corrected as a length issue

If Left(Wd.Shapes.Range(ShapeName).TextFrame.TextRange.Text, Len(StringOne)) = StringOne Then

There seems to be a few blank or formatting characters that the graphic needs at the end of the string which you can trim off.

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