Macro: How to leave a Textbox

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
Who is Participating?
TonyJollansConnect With a Mentor Commented:
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.
GrahamSkanConnect With a Mentor RetiredCommented:

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

MarinAuthor Commented:
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!


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.