Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Can I use VBA to apply corner radius on text box?

Posted on 2013-01-06
9
1,022 Views
Last Modified: 2013-01-17
Hi All

I have a textbox with a table in it (not sure if that it a terrible idea, but it seems to be working) and I would like to apply rounded corners to the text box such that a specific radius is maintained...

I tried to record a macro to see what happens... but this is all I got:

  ActiveDocument.Shapes.Range(Array("Rounded Rectangle 5")).Select

(changing the corner was not recorded, grr)

help on this much appreciated.

Oh BTW - if this is a terrible idea (I'm using cross references and headings in my table & textbox) please let me know...

Cheers, S
0
Comment
Question by:DrTribos
  • 3
  • 3
  • 3
9 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 38748227
Hi,

Please refer to http://excelusergroup.org/forums/t/2450.aspx

Public Sub AddRoundRect()
    Dim sht As Worksheet
    Dim rect As Shape
  
    Set sht = ActiveSheet
    Set rect = sht.Shapes.AddShape(msoShapeRoundedRectangle, 400, 110, 150, 75)
    With rect.TextFrame2
   
        .TextRange = "Some Text In Here"
        .VerticalAnchor = msoAnchorMiddle
        .TextRange.ParagraphFormat.Alignment = msoAlignCenter
    End With
End Sub

Open in new window

Regards
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38748375
Hi Steve,
This seems to work for me:
    Dim tb As Shape
    
    Set tb = ActiveDocument.Shapes(1)
   tb.AutoShapeType = msoShapeRoundedRectangle

Open in new window

0
 
LVL 15

Author Comment

by:DrTribos
ID: 38748420
Hi Rgonzo1971 & Graham

Thanks for the code.  As far as I can tell the parameters that can be passed to msoShapeRoundedRectangle are to position the shape on the page and do not specify the radius...

The problem for me is that the text box resizes as text is entered / removed and the radii on the corners changes...  I'd rather my (my users) radii to remain constant... (I have a similar problem with callouts... the tail changes size with the textbox... most annoying).

I tested msoShapeRoundedRectangle after dragging the shape around a bit and it does indeed put the radii back to some default... but this depends on height & width :-(

Cheers, S
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38748453
I don't think that there is any way to directly control the radii of the corners. They depend on the size of the shape, so that they stay in proportion.
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 38770028
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38773322
Hi Rgonzo -  I get an EE Unauthorized Access Error when I try to follow your link.  It says the question has not yet been closed...  

Thanks, S
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 38773471
Sorry

Not yet reviewed

Sub GetRadius()

    Dim oSh As Shape
    Dim sngRadius As Single ' Radius size in points
    
    Set oSh = ActiveWindow.Selection.ShapeRange(1)

    With oSh
        If .Width < .Height Then
            sngRadius = .Width * .Adjustments(1)
        Else 'oSh.Width >= oSh.Height
            sngRadius = .Height * .Adjustments(1)
        End If
    End With
    
    MsgBox sngRadius

    Set oSh = Nothing

End Sub
 

 
Sub SetRadius()

    Dim oSh As Shape
    Dim sngRadius As Single ' Radius size in points

    sngRadius = 40

    Set oSh = ActiveWindow.Selection.ShapeRange(1)

    With oSh
        If .Width < .Height Then
            .Adjustments(1) = sngRadius / .Width
        Else 'oSh.Width >= oSh.Height
            .Adjustments(1) = sngRadius / .Height
        End If
    End With

    Set oSh = Nothing

End Sub 

Open in new window


Regards
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38773681
Rgonzo1971,

I, for one, can thank you.

Previously, I couldn't find how to adjust the radius of the corners manually, so assumed that no such adjustment was possible. Your code prompted me to persevere.

The secret is to drag the yellow diamond shape near the top left of the selected shape. It is possible to do this while macro recording is on, so that the index in the Adjustments collection can be confirmed from the recorded code.

I hope that this information is in your article.
0
 
LVL 15

Author Closing Comment

by:DrTribos
ID: 38789478
Hi Rgonzo - thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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 …

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