?
Solved

Macro: How to leave a Textbox

Posted on 2005-04-19
3
Medium Priority
?
706 Views
Last Modified: 2011-10-03

Hello Experts,

AN REALLY URGENT PROBLEM!!!!

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!

Marin



Sub Macro1()

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

End Sub
0
Comment
Question by:Marin
3 Comments
 
LVL 6

Accepted Solution

by:
TonyJollans earned 500 total points
ID: 13820425
Well, it depends where you want to be. You can make the selection go to the start of the document with

Activedocument.range(1,1).select

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

Assisted Solution

by:GrahamSkan
GrahamSkan earned 500 total points
ID: 13822774
Marin,

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.

Templates
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






0
 
LVL 1

Author Comment

by:Marin
ID: 14126438
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!

Regards,

Marin
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

809 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