Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 05-31-2024, 06:18 AM
Guessed's Avatar
Guessed Guessed is offline Word Macro: Insert Circle Shape over selected text Windows 10 Word Macro: Insert Circle Shape over selected text Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 05-31-2024, 08:04 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

Quote:
Originally Posted by Guessed View Post
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
I appreciate your response. I actually found a way to accurately place a circle shape over specific text based on an Excel sheet, but it's limited to specific characters, a set circle size, and some adjustments by Word VBA code. However, I want the macro to calculate the position and size of the circle shape automatically. The only part I'm struggling with is the x-axis position, which isn't accurate.

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.
Reply With Quote
  #4  
Old 05-31-2024, 01:06 PM
vivka vivka is offline Word Macro: Insert Circle Shape over selected text Windows 7 64bit Word Macro: Insert Circle Shape over selected text Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 06-02-2024, 06:39 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

Quote:
Originally Posted by vivka View Post
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.
That is intended for a different purpose, specifically to circle a specific text with a specific format. While the 'Guessed' version may work, I've found that when I run the test on my computer, the circle shape will fly over randomly. I suspect this issue may be due to a language difference.
Reply With Quote
  #6  
Old 06-02-2024, 06:22 PM
Guessed's Avatar
Guessed Guessed is offline Word Macro: Insert Circle Shape over selected text Windows 10 Word Macro: Insert Circle Shape over selected text Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 06-08-2024, 05:33 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

Quote:
Originally Posted by Guessed View Post
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.
I'm aiming to create a macro that can adapt to various situations through calculation. While I haven't attempted this yet, I'm looking to explore ways to make it flexible and applicable to different scenarios.
Reply With Quote
  #8  
Old 06-08-2024, 05:34 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

Quote:
Originally Posted by josenwily View Post
To improve the circle's placement, adjust the padding values and re-calculate the x and y positions to align more accurately with the selected text. Try refining the x and y calculations to better match the text's bounding box.
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.
Reply With Quote
Reply



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 02:55 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