none
How do I insert a textbox between two paragraphs? RRS feed

  • Question

  • Hello,

    Apologies if this has been asked before, whenever I try to search the forums I get a blank screen...

    I am trying to set up a Word document with a series of questions and a textbox after each question for the answer. It needs to be generated from an Excel file using VBA.

    So far I can generate the text for the document and have managed to get it to insert a textbox after the first paragraph but when more text is added the textbox moves down the page. I've been through the help files, MSDN website and various forums but can't see where I'm going wrong.

    Any help would be greatly appreciated.

    Thanks,

    David

    Sub CaseDiscussion()
    Dim App As Word.Application
    Dim WdDoc As Word.Document
        Set App = CreateObject("Word.Application")
        Set WdDoc = App.Documents.Add
                    App.Visible = True
    With App
    'Title
    With .Selection.Font
        .Size = 14
        .Bold = True
        .Underline = True
    End With
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
                .Selection.TypeText (vbCrLf & "Document Title" & vbCrLf)
        .Selection.TypeParagraph
    'Questions
    With .Selection.Font
        .Size = 12
        .Bold = False
        .Underline = False
    End With
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 1")
        .Selection.TypeParagraph
        
        WdDoc.Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, Left:=50, Top:=.Selection.Information(wdVerticalPositionRelativeToPage), Width:=500, Height:=50, Anchor:=WdDoc.Paragraphs(2).Range
        With WdDoc.Shapes(1)
            .ConvertToInlineShape
            .TextFrame.AutoSize = True
            .Top = InchesToPoints(0.25)
            .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
        End With
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 2")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 3")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 4")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 5")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 6")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 7")
        .Selection.TypeParagraph
            .Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                .Selection.TypeText ("Question 8")
        .Selection.TypeParagraph
    End With
    End Sub

    Tuesday, June 11, 2013 2:45 PM

Answers

  • Hi David

    Why a textbox, specifically? Would it work with a two-column or two-row table?

    It is possible to stop the text box from moving (activate the "Move with text" checkbox in the advanced formatting dialog box), but a table is going to be a lot more stable...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 11, 2013 6:40 PM
    Moderator

All replies

  • Hi David

    Why a textbox, specifically? Would it work with a two-column or two-row table?

    It is possible to stop the text box from moving (activate the "Move with text" checkbox in the advanced formatting dialog box), but a table is going to be a lot more stable...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 11, 2013 6:40 PM
    Moderator
  • Hi Cindy,

    I was thinking the text box would be the simplest option, but I gave up on it and went for a table instead. It was much more straight forwards so thank you for the suggestion.

    I couldn't find any property in VBA that allows you to set the Move with text option for a text box.

    Thanks for your help.

    Cheers,

    David

    Monday, June 17, 2013 7:33 AM
  • Hi David

    Positioning and sizing of Shape objects in Word is fairly complex, but in essence what you were looking for is in the WdRelativeVerticalPosition Enumeration. wdRelativeVerticalPositionPage positions the Shape relative to the Page, for example, meaning it will stay in the same location on the page, rather than move with the text. Positioning relative to a margin is also static in this regard.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 17, 2013 3:26 PM
    Moderator