PDA

View Full Version : Solved: Selecting Specific AutoShapes



Belch
04-27-2006, 02:18 AM
Hi all,

I have some AutoShapes (rectangles) in the Header/Footer part of a document template, and I need to select specific ones and change the fill color and line width.

I have recorded a macro to get the basic code and taken the lines I need. However in terms of actually selecting the rectangles, the macro code selects them using the name "Freeform nnn" where nnn is a number (which seems quite random).

I was just wondering if there is any way of giving the rectangles explicit names rather than having to record a macro each time I want to find out what a rectangle is called.

My code for selecting and amending a rectangle is below:

Selection.HeaderFooter.Shapes("Freeform 449").Select
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192, 192, 192)
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoTrue
Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.Line.BeginArrowheadStyle = msoArrowheadNone
Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadNone

Any info appreciated,

fumei
04-27-2006, 04:17 AM
You must have used a LOT of shapes! Word uses a counter. If the fourth shape is an oval, the name is "Oval 4". If the fifth is a textbox it is "Text Box 5" - even if it is the only textbox. Further, even if the other shapes are deleted, the original name of the shape persists. AND the counter continues to increment. AFAIK there is no manual way to rename a shape. You CAN use VBA. Here is some code that may help.Sub mShapes()
Dim oShape As Word.Shape
Dim strShapeName As String
Dim response

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

For Each oShape In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes()
oShape.Select
response = MsgBox("The current selected shape has the name " & _
oShape.Name & ". Do you want to change this name?", vbYesNo)
If response = vbYes Then
oShape.Name = InputBox("Enter new name for current selected shape.")
End If
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End SubNOTE! Selecting the shape is a little tricky. A lot depends on the speed of your system. If you step through the code from the VBE it will always make the selection. HOWEVER, running the code from the document may not make the selection. You may need to add a system wait, in order to give Word enough time to catch up with actually making the selection.

The only reason the selecing is there is to see which shape the code is inreference to. You may not need it.

Anyway, that is the syntax to explicitly name autoshapes.

Belch
04-27-2006, 06:21 AM
Gerry,

Thanks for the code you provided. It works fine, but there are hundreds of shapes on my template so of course the loop takes a long time, and I only really want to explicitly name 3 or 4. So I pinched some of your code to try and explicitly name a currently selected shape, like so:

Selection.ShapeRange(1).Name = "newname"

With this, I just enter the name I want to give the currently selected shape in the code then run it in VBA.
This seems to work, as when I run my original code to amend the shape (with the "newname" name) it amends the correct shape. However I noticed that if I use the shape's old name, that works too (as if the shape now has two names?).

Anyway, I have only tested this briefly and it seems to work, is there any reason why it wouldn't?

Thanks,

fumei
04-27-2006, 03:08 PM
It has two names??? Oh boy...I'm going to have to test that! If true, that would be very very strange, and IMHO very very dumb. However, it would not be the worst thing in VBA.