Microsoft Word Macro Help- Inserting shape located on selected line

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have been doing alot of macros in excel and am now trying to venture into making a few in Word. I recorded a macro that creates a rectangle with no fill and a red border. For some reason the shape is only appearing near the top of my page (where I recorded inserting it) instead of the line I am on. Is there any way to make the shape appear on the line I am on? Thanks!

code:

Code:
Sub Highlight_Box()

    ActiveDocument.Shapes.AddShape(msoShapeRectangle, 90#, 183#, 69.75, _
        18#).Select
    Selection.ShapeRange.Fill.Visible = msoFalse
    Selection.ShapeRange.Line.ForeColor.ObjectThemeColor = wdThemeColorAccent2
    Selection.ShapeRange.Line.ForeColor.TintAndShade = 0#
    Selection.ShapeRange.Line.Visible = msoTrue
    Selection.ShapeRange.Line.Weight = 2.25
    Selection.ShapeRange.Line.Visible = msoTrue
    Selection.ShapeRange.Line.Style = msoLineSingle
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try something along the lines of:
Code:
Sub Highlight_Box()
Dim Shp As Shape, sngTop As Single, sngLeft As Single, sngWdth As Single, sngHght As Single
With Selection.Characters
  sngHght = .First.Font.Size * 1.5
  sngTop = .First.Information(wdVerticalPositionRelativeToPage) - sngHght * 0.8
  sngLeft = .First.Information(wdHorizontalPositionRelativeToPage)
  sngWdth = .Last.Next.Information(wdHorizontalPositionRelativeToPage) - sngLeft
End With
Set Shp = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=sngLeft, Top:=sngTop, Width:=sngWdth, Height:=sngHght)
With Shp
  .Fill.Visible = msoFalse
  With .Line
    .ForeColor.ObjectThemeColor = wdThemeColorAccent2
    .ForeColor.TintAndShade = 0#
    .Visible = msoTrue
    .Weight = 2.25
    .Style = msoLineSingle
  End With
End With
End Sub
In general, the above should put a box around whatever you have selected. Do be aware that you don't really need a box for this - you could simply apply a border to the selected text.
 
Upvote 0
Thanks for your response Paul. I am getting an error (Object variable or With block variable not set) on the following line:

sngWdth = .Last.Next.Information(wdHorizontalPositionRelativeToPage) - sngLeft

Also, I did not really intend this macro to highlight words, as much as highlight screenshots for creating "Training Documents". My thought was I could click onto a line below a screenshot picture, run the macro to give me a highlight box on that line, and then I could drag the box onto the picture to wherever I wanted a highlight.
 
Upvote 0
The error was probably due to having nothing selected. Try:
Code:
Sub Highlight_Box()
Dim Shp As Shape, sngTop As Single, sngLeft As Single
With Selection.Characters
  sngTop = .First.Information(wdVerticalPositionRelativeToPage) - 12
  sngLeft = .First.Information(wdHorizontalPositionRelativeToPage)
End With
Set Shp = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=sngLeft, Top:=sngTop, Width:=72, Height:=18)
With Shp
  .Fill.Visible = msoFalse
  With .Line
    .ForeColor.ObjectThemeColor = wdThemeColorAccent2
    .ForeColor.TintAndShade = 0#
    .Visible = msoTrue
    .Weight = 2.25
    .Style = msoLineSingle
  End With
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,626
Messages
6,186,095
Members
453,337
Latest member
fiaz ahmad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top