0

I'm trying to write a macro which will insert textboxes in a Word document, and format them with in-line-with-text text wrapping.

Here's my code so far:

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

The issue I'm having is that instead of each textbox being added to the start of document, as is currently happening, I need it to be added to the end. I understand that in the example I've given, I could simply use For I = 10 To 1 Step -1. However, due to my use of the textboxes in the actual project I'm working on, this is not possible.

I have spent a few hours playing with the code but just haven't been able to figure it out. Thanks in advance for any help.

Josh.

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 に答える