![]() |
|
#1
|
|||
|
|||
![]()
Hi everyone,
I am currently developing a word macro to insert a specific circle shape over selected text. Here's the code: Code:
Sub CircleSelectedText() Dim padding As Long Dim myRange As range Set myRange = Selection.range Dim x As Long Dim y As Long Dim myWidth As Long Dim myHeight As Long ' Calculate height myHeight = IIf(myRange.ParagraphFormat.linespacing > myRange.Font.Size, _ myRange.ParagraphFormat.linespacing, _ myRange.Font.Size) ' 120% of the font size for height ' Calculate width myWidth = Selection.Characters.Count * myRange.Font.Size * 1.15 ' More accurate width calculation ' Get the x and y positions relative to the page x = myRange.Information(wdHorizontalPositionRelativeToPage) y = myRange.Information(wdVerticalPositionRelativeToPage) ' Add padding to ensure the circle covers the selected text padding = Selection.Characters.Count x = x + padding * 3 y = y - padding myWidth = myWidth + padding * 2 myHeight = myHeight + padding * 2 ' Add the circle shape over the selected text With ActiveDocument.Shapes.AddShape(msoShapeOval, x, y, myWidth, myHeight) .Fill.Transparency = 1 ' Full transparency .Line.ForeColor.RGB = RGB(0, 112, 192) .Line.Weight = 1 .Line.Visible = msoTrue End With End Sub |
#2
|
||||
|
||||
![]()
I see you have been working on this problem for a while
![]() Would you consider a box rather than a circle? Code:
Sub Macro1() With Selection.Font.Borders(1) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
![]() Quote:
I don't want to rely on Word VBA adjustments of the position since such action shouls be customized each time. My original macro is designed to add a circle shape over selected text. The issue is that the character, font size, and line spacing of the selected text can vary greatly, making accurate calculation crucial. By the way, I did consider your suggestion of putting a circle shape in a text box and then adding the text box over the selected text. However, I think it would be much more complicated to calculate, so I'm exploring other options. Thank you again for your suggestion, and I appreciate your input. |
#4
|
|||
|
|||
![]()
Hi! Why don't you use the code proposed by Guessed in post
https://www.msofficeforums.com/181470-post2.html It seems to work correctly. |
#5
|
|||
|
|||
![]() Quote:
|
#6
|
||||
|
||||
![]()
I think it might be less of a language difference and more related to the text alignment.
Try the macro on text that is left aligned vs fully justified vs right aligned. Do you get consistent results for one alignment vs another alignment? On my Australian English text doc, I get a better result on Left Aligned text but not with other alignments. For some reason, centred text circles are wider than left aligned text of the same size.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
![]() Quote:
|
#8
|
|||
|
|||
![]()
I attempted to improve the circle's placement by adjusting the padding values and recalculating the x and y positions to better align with the selected text. Unfortunately, I was unable to achieve the desired accuracy. That's why i ask for solution here.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word Macro issues (Circle Shape) | syl3786 | Word VBA | 6 | 04-23-2024 06:06 AM |
![]() |
syl3786 | Word VBA | 5 | 01-21-2024 03:41 AM |
![]() |
mrplastic | Word VBA | 4 | 12-20-2019 01:25 PM |
![]() |
zillah | Word | 2 | 11-14-2019 03:27 AM |
Macro to insert different sets of text at bookmark depending on sequence of selected check boxes | chipper09 | Word VBA | 0 | 06-21-2018 01:49 PM |