选择.范围未在光标位置插入文本框



我正在编写一个宏,用于将包含预先格式化的表格的文本框插入到Microsoft Word文档中,我希望它将表格插入当前光标位置。使用我拥有的当前代码,文本框似乎插入在当前页面的开头或结尾,而不是光标位置。

这是我的代码:

Sub InsertTable()
    Dim shpTbox As Shape
    Dim rngTbox As Range
    Dim tblBox As Table
    Set shpTbox = ActiveDocument.Shapes.addtextbox( _
        Orientation:=msoTextOrientationHorizontal, _
        Left:=72, Top:=50, Width:=468, Height:=220, Anchor:=Selection.Range)
    shpTbox.TextFrame.TextRange.Tables.Add Range:=shpTbox.TextFrame.TextRange, NumRows:=8, NumColumns:=4, _
        DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
    shpTbox.TextFrame.TextRange.Tables.Item(1).Select
    shpTbox.TextFrame.TextRange.Tables(1).Style = ActiveDocument.Styles("Custom Table")
    Selection.InsertCaption Label:="Figure", _
        Title:=". Insert Caption Here", _
        Position:=wdCaptionPositionBelow
    shpTbox.Line.Visible = msoFalse
    shpTbox.WrapFormat.Type = wdWrapSquare
    shpTbox.WrapFormat.Side = wdWrapBoth
    shpTbox.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub

有没有办法做我想做的事情?有人可以解释为什么这不能做我想让它做的事情吗?谢谢!

默认情况下,Word 在页面上的位置。您需要另行通知,并在之后重新设置LeftTop属性。插入文本框后,指定相对水平和垂直位置。例如:

shpTbox.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
shpTbox.RelativeVerticalPosition = wdRelativeVerticalPositionLine
shpTbox.Left = 72
shpTbox.Top = 50

您可能需要测试一些WdRelativeHorizontalPositionWdRelativeVerticalPostition枚举值,以查看最适合您的情况的值。

最新更新