【问题标题】:Add Textbox at the cursor position in a table在表格中的光标位置添加文本框
【发布时间】:2022-01-02 09:14:28
【问题描述】:

我想通过 MS Word 中的 VBA 在当前光标位置添加一个文本框。

这可行,但如果光标位于表格内(无论是什么单元格),文本框就会被添加到错误的位置。

Sub AddTextBox()

Dim oShape As Shape
Dim x As Long
Dim y As Long

'get Cursorposition
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
    
Set oShape = ActiveDocument.Shapes.AddTextBox( _
    Orientation:=msoTextOrientationHorizontal, _
    Left:=x, Top:=y, Width:=200, Height:=12)

With oShape
    With .TextFrame
        With .TextRange
            .Text = "TEXT"
            .Font.Name = "Segoe Script"
            .Font.Size = 10
            .Font.ColorIndex = wdRed
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
        End With
        
        .MarginBottom = Application.CentimetersToPoints(0.15)
        .MarginTop = Application.CentimetersToPoints(0.15)
        .MarginLeft = Application.CentimetersToPoints(0.1)
        .MarginRight = Application.CentimetersToPoints(0.1)
        .WordWrap = False
        .AutoSize = True
    End With

    .LockAnchor = False
    .WrapFormat.Type = wdWrapNone
    
    .Fill.Visible = msoFalse
    .Line.Visible = msoFalse
    .Height = Application.CentimetersToPoints(0.8)
End With
End Sub

当光标放在表格中时,如何将 TextBox 放在光标位置?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    添加形状时,位置默认为相对于边距,因此您需要在添加形状后设置所需的相对位置。

    Sub AddTextBox()
    
        Dim oShape As Shape
        Dim x As Long
        Dim y As Long
    
        'get Cursorposition
        x = Selection.Information(wdHorizontalPositionRelativeToPage)
        y = Selection.Information(wdVerticalPositionRelativeToPage)
        
        Set oShape = ActiveDocument.Shapes.AddTextBox( _
            Orientation:=msoTextOrientationHorizontal, _
            Left:=x, Top:=y, Width:=200, Height:=12, Anchor:=Selection.Range)
    
        With oShape
            .LeftRelative = wdRelativeHorizontalPositionPage
            .TopRelative = wdRelativeVerticalPositionPage
            .LockAnchor = False
            .WrapFormat.Type = wdWrapNone
        
            With .TextFrame
                With .TextRange
                    .Text = "TEXT"
                    .Font.Name = "Segoe Script"
                    .Font.Size = 10
                    .Font.ColorIndex = wdRed
                    .ParagraphFormat.Alignment = wdAlignParagraphLeft
                End With
            
                .MarginBottom = Application.CentimetersToPoints(0.15)
                .MarginTop = Application.CentimetersToPoints(0.15)
                .MarginLeft = Application.CentimetersToPoints(0.1)
                .MarginRight = Application.CentimetersToPoints(0.1)
                .WordWrap = False
                .AutoSize = True
            End With
            .Fill.Visible = msoFalse
            .Line.Visible = msoFalse
            .Height = Application.CentimetersToPoints(0.8)
        End With
    End Sub
    

    【讨论】:

    • @Fritz - 如果这回答了您的问题,请参阅What should I do when someone answers my question?
    • 行 .LeftRelative / .TopRelative 对我没有帮助。文本框不会更精确地定位到光标位置。尽管如此,选项 Anchor:=Selection.Range 部分有助于减少问题。谢谢
    • @Fritz - Selection.Information(wdHorizontalPositionRelativeToPage) 没有给出光标位置。在表格中,它返回单元格左边缘的位置。在一行文本中,它返回该行的左侧。这是您将获得的最精确的位置。
    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多