macro to manipulate graphic

F

fs

Can someone help me to create a macro to (a) select (any) graphic (b) bring
to front. I'm using Word 2003. Thanks for any help!
 
K

Ken Johnson

fs said:
Can someone help me to create a macro to (a) select (any) graphic (b) bring
to front. I'm using Word 2003. Thanks for any help!

Hi fs,

Are you able to set up a UserForm with a ListBox and two
CommandButtons?

A Sub Procedure can be used to load the ListBox with the names of the
Shapes in the ActiveDocument.Shapes Collection and Show the UserForm on
the screen.

The ListBox's Click Event Procedure can be coded so that when the user
clicks one of the Names in the ListBox, the Shape with that Name is
selected. If the user then clicks a different Name (the ListBox should
not be MultiSelect) then that Shape is selected, while the previous
selected Shape is deselected. If all the Shapes are visible on the
screen (not likely, but at least the Shapes that are of interest to the
user are likely to be visible) it will help them associate the name
with the Shape and they can be sure they are about to change the ZOrder
of the right Shape.

The ListBox's DblClick Event Procedure and one of the CommandButtons
Click Event Procedure could have the same code which brings the
selected shape to the front.

The second CommandButton could be coded to simply unload the UserForm
so that nothing then happens.

The best place to put the UserForm and the Sub Procedure that loads its
ListBox etc
is with the Normal Document Template. To get there press Alt + F11 to
get to the VBA Editor. In the VBA Editor, if the Project Explorer is
not visible then go View>Project Explorer. In the Project Explorer
click on Normal then go Insert>UserForm. When the UserForm is visible
so too should be the Toolbox, if it isn't then go View>Toolbox. Move
the mouse arrow over the Toolbox buttons until you find the ListBox
button (likely to be top right). Click that button then click on the
UserForm. You can then manipulate its size, position etc using any
combination of mouse movements, Format menu commands or Property Values
in its Properties dialog. Follow similar steps to get two
CommandButtons in place on the UserForm.

When I set this up on my computer I made the following changes to the
UserForm, ListBox and CommandButton Properties in their respective
Properties dialogs. When you select the Userform by clicking it you
should see a list of its properties in the Properties Window under the
heading "Properties - UserForm1". Go View>Properties Window if it
is not visible.
I changed the Name from UserForm1 to FmBringToFront and the Caption
from UserForm1 to Select the Shape to Bring to Front
Click the ListBox to view its properties.
I changed its Name from ListBox1 to LbAllShapes
Click one of the CommandButtons
I changed its Name from CommandButton1 to BtnBringToFront, its Enabled
from False to True it AutoSize from False to True (I also changed its
Font to Bold and larger size) and its Caption from CommandButton1 to
Bring to Front
Click on the other CommandButton
I changed its Name from CommandButton2 to BtnCancel, its AutoSize from
False to True (Font changes same as before) and its Caption from
CommandButton2 to Cancel

The following code will load LbAllShapes with the Names of all of the
Shapes in the ActiveDocument.Shapes Collection...

Public Sub BringToFront()
Dim Shp As Shape
If ActiveDocument.Shapes.Count <> 0 Then
For Each Shp In ActiveDocument.Shapes
FmBringToFront.LbAllShapes.AddItem Shp.Name
Next Shp
FmBringToFront.Show
Unload FmBringToFront
Exit Sub
End If
MsgBox "No Shapes Found in this Document!"
End Sub

To get the code in place...

1. Copy it
2. Go back to your Word Document
3. Press Alt + F11 to get into the VBA Editor
4. In the Project Explorer Click on Normal
5. Go Insert>Module
6. Paste the code into that new Module

Now the code for the different parts of the UserForm...

Private Sub BtnBringToFront_Click()
On Error Resume Next
With ActiveDocument.Shapes(LbAllShapes.Value)
..Select
..ZOrder msoBringToFront
End With
Unload FmBringToFront
End Sub

Private Sub Cancel_Click()
Unload FmBringToFront
End Sub

Private Sub LbAllShapes_Click()

ActiveDocument.Shapes(LbAllShapes.Value).Select
Application.ScreenRefresh
End Sub

Private Sub LbAllShapes_DblClick _
(ByVal Cancel As MSForms.ReturnBoolean)
With ActiveDocument.Shapes(LbAllShapes.Value)
..Select
..ZOrder msoBringToFront
End With
Unload FmBringToFront
End Sub

To get this in place...

1. Copy it
2. Go back to your Word Document
3. Press Alt + F11 to get into the VBA Editor
4. In the Project Explorer single click the FmBringToFront UserForm
icon then go View>Code
5. Paste the code into the Module that appears

After you have this all in place it would be useful to assign a
keyboard shortcut by going...

Tools>Customize...>Keyboard... (at the bottom)>Categories:=macros,
macros:=BringToFront, Save changes in:= Normal.dot then assign the key
combination of your choice.

Hope this helps, and that I haven't left anything out.

Ken Johnson
 
K

Ken Johnson

Hi fs,

You might be interested in this little macro that lets you easily
change the names of any selected shape in Word. Meaningful names make
working with Word's graphics a lot easier...

Public Sub BringToFront()
Dim Shp As Shape
If ActiveDocument.Shapes.Count <> 0 Then
For Each Shp In ActiveDocument.Shapes
FmBringToFront.LbAllShapes.AddItem Shp.Name
Next Shp
FmBringToFront.Show
Unload FmBringToFront
Exit Sub
End If
MsgBox "No Shapes Found in this Document!"
End Sub

Before running it you have to select all the shapes whose names you
want to change.
For each selected shape an input box appears with its current name as
the default input value, so if you change your mind about changing its
name you can just click OK and an input box for the next selected shape
will appear. Just edit the inputbox's textbox to change a shape's name
then click OK. However, a Shape's name will not change if that name has
already been used with another Shape.

Ken Johnson
 
K

Ken Johnson

Oops!
I copied the wrong bit of code:-/

Try this instead...


Public Sub ReNameShapes()
If Application.Selection.ShapeRange.Count <> 0 Then
Dim Shp As Shape
Dim I As Long
Dim ncShapes As New Collection
For Each Shp In Application.Selection.ShapeRange
ncShapes.Add Item:=Shp
Next
On Error Resume Next
For Each Shp In ncShapes
Shp.Select
Application.ScreenRefresh
Shp.Name = InputBox(prompt:="Current Name:=" & Shp.Name, _
Title:="Rename This Shape", Default:=Shp.Name)
Next Shp
Exit Sub
End If
MsgBox "You need to select the shapes before running this macro."
End Sub

Ken Johnson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top