View Single Post
 
Old 05-31-2024, 01:13 AM
syl3786 syl3786 is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: Jan 2023
Posts: 97
syl3786 is on a distinguished road
Default Word Macro: Insert Circle Shape over selected text

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
I've noticed that the circle shape is not positioned correctly. It consistently appears on the left-hand side of the selected text but not over the selected text. Does anyone have any suggestions on how to improve the accuracy of its placement? Any help would be greatly appreciated!
Reply With Quote