Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-31-2024, 01:13 AM
syl3786 syl3786 is offline Word Macro: Insert Circle Shape over selected text Windows 10 Word Macro: Insert Circle Shape over selected text Office 2019
Advanced Beginner
Word Macro: Insert Circle Shape over selected text
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Macro issues (Circle Shape) syl3786 Word VBA 6 04-23-2024 06:06 AM
Word Macro: Insert Circle Shape over selected text Seeking Assistance with Word Macro to Add Transparent, Blue Line Circle Shape syl3786 Word VBA 5 01-21-2024 03:41 AM
Word Macro: Insert Circle Shape over selected text Find and Replace Selected Text or Macro for finding selected text mrplastic Word VBA 4 12-20-2019 01:25 PM
Word Macro: Insert Circle Shape over selected text Word 2016 how to insert a text shape into a word doc ? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft