0

我正在尝试编写一个宏,它将在 Word 文档中插入文本框,并使用 in-line-with-text 文本换行对其进行格式化。

到目前为止,这是我的代码:

Sub Example()
    Dim newTextbox As Shape
    For I = 1 To 10
        Set newTextbox = ActiveDocument.Shapes.AddTextbox _
        (Orientation:=msoTextOrientationHorizontal, _
        Left:=0, Top:=0, Width:=100, Height:=50)
        newTextbox.WrapFormat.Type = wdWrapInline
        newTextbox.TextFrame.TextRange = I
    Next
End Sub

我遇到的问题是,不是每个文本框都被添加到文档的开头,就像目前正在发生的那样,我需要将它添加到末尾。我知道在我给出的示例中,我可以简单地使用For I = 10 To 1 Step -1. 但是,由于我在我正在处理的实际项目中使用了文本框,这是不可能的。

我已经花了几个小时玩代码,但一直无法弄清楚。提前感谢您的帮助。

乔希。

4

1 回答 1

0

约书亚,这是一个最终的工作代码:

Sub InsertInlineTextBox()

' Move all the text after the cursor to a new paragraph
' and jump to the start point of this paragraph
Selection.InsertParagraphAfter
Selection.MoveDown Unit:=wdParagraph, count:=1

Dim aShape As Shape

' Insert the shape at the current cursor position +1 point down in vertical
' direction to prevent automatic moving the shape to the previous paragraph
' during 'inlining'
Set aShape = ActiveDocument.Shapes.AddTextbox( _
    msoTextOrientationHorizontal, _
    Selection.Information(wdHorizontalPositionRelativeToPage), _
    Selection.Information(wdVerticalPositionRelativeToPage) + 1, 400, 60)

With aShape
    .TextFrame.MarginBottom = 0 ' adjust text margins
    .TextFrame.MarginLeft = 0
    .TextFrame.MarginRight = 0
    .TextFrame.MarginTop = 0
    .Line.Visible = msoFalse    ' don't show the border

    ' converting to InlineShape will place
    ' the shape at the start point of paragraph
    .ConvertToInlineShape
End With

' Remove carriege return before the shape
Selection.EndOf Unit:=wdParagraph, Extend:=wdMove
Selection.MoveLeft Unit:=wdCharacter, count:=1
Selection.Delete Unit:=wdCharacter, count:=1
End Sub

我还使用这个宏来禁用文本框中的拼写检查,因为它们通常包含一堆 C++ 示例代码:

Sub NoSpellCheck()
  Selection.Range.SpellingChecked = True
  Selection.Range.NoProofing = True
End Sub
于 2014-12-08T13:51:12.913 回答