Macro: How to leave a Textbox

Posted on 2005-04-19
Last Modified: 2011-10-03

Hello Experts,


As you can see in my history I know a bit about VBA in Excel. Now I need some VBA for Word and I hope you can help me out!

I have a file with a UserForm in it. In the UserForm the user selects for whitch company he is working and for whitch company he is going to write a letter (1 out of 6 company's). Each company has different addresses. Because I want the adress of the sender of the letter only to appear on the first page, I need a macro that puts a Textbox with the senders adress in the file.

I recorded the macro that puts a Textbox in a file. I populated the Tekstbox with the value Amsterdam ( in the orriginal it should be adress data). After I've populated the Textbox I want to go back to the normal text to go on and finish my letter.

But while recording the macro, I can't leave the Textbox and start to fill the file with data. The example of creating the textbox is shown below.
If it's possible at all, how can I leave the Textbox by VBAcode??? If it's really not possible, please let me know as well!!!

I hope I've explained it well enough, otherwise, let me know!!

Thanks in advance!


Sub Macro1()

    ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 376.85, _
        79.85, 108#, 216#).Select
    Selection.TypeText Text:="Amsterdam"

End Sub
Question by:Marin
    LVL 6

    Accepted Solution

    Well, it depends where you want to be. You can make the selection go to the start of the document with


    for example, or you can work with the document without moving the selection at all.
    LVL 76

    Assisted Solution


    Text boxes
    To avoid confusion you should know that there are three very different types of text box that can be placed on a Word document.
    There is the 'floating' Shape type that you are using.
    There is a FormField type, used with Word (document) Forms. Here the bulk of the document is protected and the user can only enter data into form fields such as this type of text box.
    Lastly, it is possible to place an ActiveX control such as a TextBox, that is normally found on a UserForm, on the document. This practice is not well documented and is much less common than in Excel.

    Macro Recorder.
    If you want to do something quite simple in your macro you can record it, but it several limitations.
    Mainly, you cannot use the mouse on the document and it always uses the Selection object as opposed to a Range object.
    This because because it works on the document through the user interface as opposed to working on the document directly.
    I think it's best treated as a learning aid. It will show the objects and properites involved if you are tryng something new.

    In Word, things that will appear on every document can be saved in a template, so that they don't have to be re-added. That could apply to the empty address text box. It could also apply to a lot of the body text. You could use bookmarks to know where to place the text.

    This will add the text box and fill it:

        Dim sh As Shape
        Set sh = ActiveDocument.Shapes.AddTextBox(msoTextOrientationHorizontal, 376.85, 79.85, 108#, 216#)
        sh.TextFrame.TextRange.Text = "Amsterdam"

        This will add text at a bookmark:

    Sub InsertAtBookmark(strText As String, strBookmarkName As String)
        Set rng = ActiveDocument.Bookmarks("strBookmarkName").Range
        rng.Text = strText
        ActiveDocument.Bookmarks.Add "strBookmarkName", rng 'replace the bookmark
    End Sub

    LVL 1

    Author Comment

    Hello Tony and Graham,

    Sorry for the late response from my side, to busy solving the problem! :)

    I found out that when I put the textbox in the header or footer of the page that I can return to the original text by the next line:

        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

    So you guys didn't give me the solution I hoped for... But with your answers you did give me some new views how to look at things and thats why I decided to split the points between the two of you!

    I hope to have informed you well and maybe I'm the one who can help you next time!



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
    I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
    This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
    Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now