Macro for drawing autoshapes and lines

C

courtesio99

I'm totally new to macros. I've tried recording a macro but with thi
option, I can't draw anything.

How do I draw shapes and lines at specific locations using code
 
W

Word Heretic

G'day courtesio99 <[email protected]>,

by learning the VBA - this aint flippant - its a statement of fact
that MS word recorder does not listen to mouse activity. See my last
post, this Newsgroup, for more information.

If you need specialist tutoring, I am available.

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


courtesio99 was spinning this yarn:
 
M

Mark Tangard

Hi courtesio99,

You can use the macro recorder and mouse to build shapes, but it's extremely
limited, and produces enormous thickets of code, because the recorder logs every
characteristic of the shape being created or modified, whether you change it or
not. It's also a big chore to use the recorder to do anything else noteworthy,
like modify an existing shape when your document has several.

In VBA you typically want to approach this by writing code rather than recording
it, which is a very different process with shapes -- though extremely powerful
once you master it. You may still find use for the macro recorder, but just to
record bits of code to help you discover what properties and methods to use for
certain tasks.

Usually in VBA, you build a shape by (1) defining an object variable as the
newly added shape, and then (2) setting its various properties. Step (1) is
what sets apart the code-writing method from the recording method, and what
gives it much of its power. For example, the macro below will draw a
4-inch-diameter circle with the upper left corner of its bounding rectangle 2"
from the top of the page and 2" from the left edge. Then it gives the shape it
a color ("fill") of red and a 12-point-thick yellow border ("line").

Dim sh As Shape
Set sh = ActiveDocument.Shapes.AddShape(Type:=msoShapeOval, _
Left:=144, Top:=144, Width:=288, Height:=288)
With sh
.Line.Weight = 12
.Line.ForeColor = vbYellow
.Fill.ForeColor = vbRed
End With

(Most graphics measurements in VBA are given in points, so a width of 288 is 4
inches. 1 inch = 72 points.)

I should pause here to point out that, if you're really *totally new to macros,"
building shapes in VBAA code may not be the greatest place to start. It can't
hurt, but don't be discouraged if it seems miles above your head. Once you get
comfortable with the more pedestrian uses of VBA, it won't look so weird.

Building the above shape by recording a macro would spit out over 30 lines of
code, specifying every parameter you could *possibly* have adjusted in the
dialogs you would have used. (Try it.) (The one thing it's handy for is
finding out what VBA calls the shape, e.g., msoShapeOval above, since some of
the more obscure shapes have names you can't always guess at.)

But that's not the main difference. Say you needed to create 20 shapes in a
document and then perhaps go back and change one of them later, you'd have a
horrible time writing code to find that one if you'd used recorded code to build
it. But when you *write* the code, you can NAME your shapes, and those names
will stick.

All shapes have a built-in name assigned by Word, but they're much less useful
than a name you assign. (To illustrate, select any manually drawn shape and run
a macro like:

MsgBox Selection.ShapeRange(1).Name

The red circle I built above is called "Oval 4." Bleah.)

But if we just add a line like:

.Name = "Mars"

to the macro first shown above, after the "With sh" line, it places a permanent,
easily remembered handle on the shape, which you can use later, even in
another macro at another time. So I could come back to this document and use
the macro below to re-color the shape and add an informative message to the
shape itself:

With ActiveDocument.Shapes("Mars")
.Fill.ForeColor = vbYellow
.TextFrame.TextRange.Text = "But I'm not Mars, I'm the Sun!"
End With

Hope this helps a bit and doesn't overwhelm.
 
W

Word Heretic

G'day Mark,

<Standing ovation from the wings>

Steve Hudson

Mark Tangard was spinning this yarn:
 
W

Word Heretic

G'day courtesio99 <[email protected]>,

Someone who is mates with the inside circle of MVPs. Vaguely you have
to hang out here, but it is quite irrelevant when push comes to shove.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


courtesio99 was spinning this yarn:
 
M

Mark Tangard

Thank ya. It's a caffeine thing.

About 25 years ago I planned to write an article on the benefits of
making examples entertaining as well as illustrative.

It's still just a plan.

MT
 
M

Mark Tangard

courtesio99 said:
Thanks a great deal!
by the way... whats the concept of MVP?

For some it's a status thing. For me, as I said earlier, it's more of a
caffeine thing.

Some MVPs probably hope to lick certain boots in Redmond, Washington.
Others -- including most Word MVPs in my experience -- do it because
they like helping other people understand the admittedly ornery and
frustrating programs the world is now pretty well stuck with. The MVP
designation is given to recognize the combination of (1) expertise and
(2) skill at conveying it to others.

A good longer explanation is at http://word.mvps.org/AboutMVPs/index.htm
 
W

Word Heretic

G'day Mark Tangard <Mark@NoMailPlease_Tangard.com>,

Well, it's fact. As a small demo, I redid the front page of
www.hitechmufflers.com.au to have some humourous stuff. Hit and
response rates skyrocketed, and no other changes there came close to
that effect.


Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: WordHeretic at tpg.com.au


Mark Tangard was spinning this yarn:
 

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