Results 1 to 6 of 6
  1. #1
    Lounger
    Join Date
    Apr 2002
    Location
    Sydney, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Set Range To Text Box (VBA / Word 2000)

    I am using the following code to set a range to a specific shape object in a document:

    Dim rngDocEnd As Range
    Set rngDocEnd = ActiveDocument.Shapes(1)

    Why doesn't it work? I get a runtime error 13 (type mismatch), but I don't understand why. Can anyone help?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Set Range To Text Box (VBA / Word 2000)

    The mismatch is because on the right you are specifying a shape object, and it cannot be assigned to a range variable.

    I am not very familiar with shapes, but something like this should work if the stars line up properly:

    Set rngDocEnd = ActiveDocument.Shapes.Range(1)

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

    Re: Set Range To Text Box (VBA / Word 2000)

    Cathy, nothing in Word is that easy! Here is the tree that you want: Document > Shape > TextFrame > TextRange > Text. It could be worse and it is in PowerPoint! Here's a sample Word macro that hopefullt will explain it better. HTH --Sam
    <pre>Option Explicit
    Sub Macro1()
    Dim s As Shape
    Dim f As TextFrame
    Dim r As Range
    Dim str As String
    Set s = ActiveDocument.Shapes(1)
    MsgBox s.Name
    Set f = ActiveDocument.Shapes(1).TextFrame
    If f.HasText Then
    Set r = ActiveDocument.Shapes(1).TextFrame.TextRange
    str = r.Text
    MsgBox str
    End If
    End Sub</pre>

    This is just for understanding and can be replaced with the one-liner,
    If ActiveDocument.Shapes(1).TextFrame.HasText Then MsgBox ActiveDocument.Shapes(1).TextFrame.TextRange.Text
    Now that's real ugly, Bill Gates!
    <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>

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set Range To Text Box (VBA / Word 2000)

    > Now that's real ugly, Bill Gates!

    I guess it's a matter of taste ... but then, I like russian nesting dolls, too <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

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

    Re: Set Range To Text Box (VBA / Word 2000)

    I love <IMG SRC=http://www.mtl.com/sam/images/love.gif> Russian nesting dolls: they always fit together correctly! <img src=/S/bash.gif border=0 alt=bash width=35 height=39>
    <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
    Apr 2002
    Location
    Sydney, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Set Range To Text Box (VBA / Word 2000)

    Thanks for the code. I had arrived at the same code late Friday afternoon with some help from a colleague. It is long and ugly, but hey, it works!!

Posting Permissions

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