0

如果在绘图画布中,我很难通过 VBA 访问文本框文本。

在其他地方搜索这个+显示我应该能够使用“TextFrame.TextRange”访问文本,但它失败了,并且调试显示不存在数据(我无法发布图像,因为我的反馈<10)

请试一试: - 制作一个新的 word 文档, - 添加一个绘图画布并在其中放置一个带有一些虚拟文本的文本框 - 尝试通过 VBA 访问/修改它 非常感谢 = )

For Each shp In ActiveDocument.Shapes
    If shp.Type = msoCanvas Then

        For Each canvasitem In shp.CanvasItems
            If canvasitem.Type = msoTextBox Then

                ' NONE OF THESE WORK - WHAT AM I MISSING?
                'Debug.Print canvasitem.TextFrame.TextRange.Text
                'Debug.Print canvasitem.TextFrame.TextRange.Characters.Text

                'If canvasitem.TextFrame2.HasText = True Then _
                 '   Debug.Print canvasitem.TextFrame2.TextRange

            End If
        Next

    End If
Next
4

2 回答 2

1

这似乎对我有用:

For Each shp In ActiveDocument.Shapes
    If shp.Type = msoCanvas Then

        For Each canvasitem In shp.CanvasItems
            If canvasitem.Type = msoTextBox Then
                Debug.Print canvasitem.TextFrame.TextRange.Text
            End If
        Next
    End If
Next
于 2015-07-28T16:37:13.070 回答
0

我蛮力解决了。它仅适用于绘图画布中的 Word 2010 文本框。它不适用于绘图画布之外的文本框。

解决方法=选择文本框,然后通过“选择”方法访问文本框(不是很好的代码,但它有效)。

MICROSOFT .. 为什么不能更好地记录文档!或者在发布前完成开发。Grrr。

我希望这对其他人有帮助

For Each shp In ActiveDocument.Shapes
  If shp.Type = msoCanvas Then

  For Each canvasitem In shp.CanvasItems
     If canvasitem.Type = msoTextBox Then
        ' Word 2003
        ' Debug.Print canvasitem.TextFrame.TextRange.Text

        ' Word 2010 - change the field in a Textbox (in a drawing canvas)
        canvasitem.Select
        For Each fld In Selection.Fields
           If InStr(fld.Code.Text, "STYLEREF 3 \s") _
           Or InStr(fld.Code.Text, "STYLEREF 3") Then _
               fld.Code.Text = "STYLEREF ""Heading 3"" "
        Next

     End If
   Next
   End If
Next
于 2015-07-28T17:42:01.850 回答