Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select text in text box from within VBA (Word 97 SR2)

    Can anyone tell me how to use VBA to select a word that is in a text box? That's text box as in wdTextFrameStory, not a text box on a form. I am going nuts here. If anyone has a code sample, it would be greatly appreciated.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    Margot,

    Do a Search here on the keyword "TextFrame" - this will probably turn up some useful earlier posts.

    Gary

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    Well, it certainly helps to know what to call things! At least now I do know how to insert text in a text box from within a program.

    Here's my problem: thanks to Helen Feddema's earlier contributions to Woody's Access Watch, I have been able to come up with a macro that inserts data from an Access database into Word and applies certain formats to data from certain fields. It runs perfectly if I click inside my text box & then run the macro, but I can't figure out how to get this chunk of code to work from within Document_New. Searching on TextFrame did lead to this clue: ActiveDocument.Shapes(1).TextFrame.TextRange.Inser tAfter ("New Text") will insert text into a text box from within a program. However, I still can't figure out how to select *that* text in order to format it. If I generate it elsewhere, name the selection & insert the selection into the text box, all the text takes on a single style. If I insert it into the text box using InsertAfter, I can't figure out how to apply a style to each piece of text as I insert it. Yet if I have my insertion point within the text box and *then* run the macro, it works just fine.

    The problem seems to be that when Word does things to text boxes, it apparently does them without actually activating the text box or selecting the text it's working on. If I have Word do a find and exit the sub the instant the find is found, the insertion point lands in the main document & not the text box. My immediate plan is to open a doc based on this template from within Access, run a macro that will insert & format all the rest of the info throughout the document, and let the user double-click on a macrobutton within the text box to insert & format the text that goes in the text box. I am forced to use a text box because I need to use linked text boxes to get that part of the text to land in the right places.

    As you can probably tell, my day job isn't programming. I work in a Proposals department and we have recently been given a new and complex format to use on all our resumes -- and we have hundreds of them. My macrobutton workaround works OK, but the trouble is that I have begun to obsess about how to do this all in one fell swoop using Document_New & have been losing sleep staying up late trying "one more thing." Please let me know if you have any further suggestions. Thanks for the clue about TextFrame. I had no idea that's what these things were called.

  4. #4
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Select text in text box from within VBA (Word 97 SR2)

    Margot, the TextRange property returns a range object that contains all of the text within the textbox. So, if you collapse the range and insert the text, then you can format the range which will now only contain your text. Should look like this:<pre>Dim rng As Range
    Set rng = ActiveDocument.Shapes(1).TextFrame.TextRange
    rng.Collapse wdCollapseEnd
    rng.InsertAfter "New Text"
    rng.Font.Bold = True</pre>

    HTH! --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  5. #5
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Select text in text box from within VBA (Word 97 SR2)

    Just collapse the range each time that you want to change the format:<pre>Dim rng As Range
    Set rng = ActiveDocument.Shapes(1).TextFrame.TextRange
    rng.Collapse wdCollapseEnd
    rng.InsertAfter "Bold Text"
    rng.Font.Bold = True
    rng.Collapse wdCollapseEnd
    rng.InsertAfter "Red Text"
    rng.Font.ColorIndex = wdRed</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  6. #6
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    Thanks! When I get a chance, I hope to redo my macro using ranges.

    I want you to know that your posts have been an answer to prayer -- and I mean that literally, as in "If any of you lack wisdom, let him ask of God" (James 1:5).

  7. #7
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    To specify part of a range you just move the beginning or end to where you want them, so if you want to select the 3rd and fourth words of the current range you could use...

    ' move start of range to start of third word
    rng.Start = rng.Words(3).Start

    'move end of range to end of second word (after current starting point)
    rng.End = rng.Words(2).End


    you can also manipulate ranges with

    rng.Collapse (specify the direction as start or end)
    rng.Expand (handy for getting a whole paragraph)
    rng.Move (handy for stepping forward a word or paragraph at a time)
    rng.MoveEnd
    rng.MoveStart

    The other very useful range method is
    rng.Duplicate
    this creates a new range pointing to the same text, so you can manipulate it without changing the original

    StuartR

  8. #8
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    <P ID="edit" class=small>Edited by gwhitfield on 02-Feb-02 10:37.</P>Hyperlink added

    Sorry it took so long to get back to you. Deadline pressures intervened.

    The solution you sent me did in fact allow me to enter text in a given text box and format it, but I couldn't figure out how to give different words different formats, because I don't know how to specifiy a part of a range. If someone can tell me how to do this, I would appreciate it.

    I did find a solution that works in practice, but the author of the article I got it from couldn't figure out why. (Article is Moving to a Word Text Box with Keyboard or Macro at <A target="_blank" HREF=http://www.zdnet.com/pcmag/pctech/content/solutions/wp1622a.htm>http://www.zdnet.com/pcmag/pctech/content/...ons/wp1622a.htm</A>.) Basically what you do is hide a bookmark behind the textbox you want to go to, then have your code tell Word to go to that bookmark, do a page down & then a page up. Voila! Your selection mark is now within the text box and you can insert text using Selection.TypeText commands.

    I appreciate everyone's help. I have a lot more bells & whistles to add, but my database is up & running and we are already using it to generate formatted Word resumes.

  9. #9
    Lounger
    Join Date
    Jan 2001
    Location
    Kingwood, Texas, USA
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select text in text box from within VBA (Word 97 SR2)

    Thanks, Stuart! What I've got now works, but I don't feel comfortable about that Page Down, Page Up stuff. As soon as I get a chance, I hope to rewrite that part using ranges.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •