1st Steps In VBA for Word (XP)

J

jon.souter

A very helpful guy by the name of Doug has given me some great pointers
and I'm already several steps further down the road I need to travel
after just an hour of reading!

However, after checking various sections of the VBA help system, I was
kind of expecting to be able to do something like this in code...

Sub test()

'// Create "Handle" Variable For A Shape Object //

Dim MyPicture As Shape

'// Create Picture (A Word "Shape" Object) & Assign To MyPicture //

MyPicture = ActiveDocument.Shapes.AddPicture("c:\photos\maple.png")

'// The Following Code Resizes The "MyPicture" Object //
MyPicture.ScaleHeight 100, True
MyPicture.ScaleHeight 100, True

End Sub

However, this generates errors - so I've either gotten the syntax
wrong, or my ideas about using objects are alien to VBA!

If I can't marry some kind of variable to an object, such as a picture,
how can I reliably manipulate this one object - as the use of the
"Items" collection looks a bit hap-hazard.

All help is most welcome,

Cheers,



Jon
 
J

Jay Freedman

Hi, Jon,

There's only one little thing missing: When you assign a value to an object
(as opposed to a variable of a simple data type such as Integer or String),
you have to start the statement with the Set keyword. This will work:

Set MyPicture = ActiveDocument.Shapes.AddPicture("c:\photos\maple.png")

In general, when you get an error message about "Object variable or With
block variable not set", it means you forgot the Set keyword.

BTW, it would be a Good Thing, when asking for help with syntax or runtime
errors, to quote the exact text of the error message and indicate which line
of code is highlighted when you click the Debug button. Otherwise, we're
left staring at the code or -- as I did here -- setting up a document and
pasting in the code to see what happens.

One last comment: I think you meant to change the second
MyPicture.ScaleHeight to MyPicture.ScaleWidth. You'll also find that 100 is
probably way too big -- it's a multiplication factor, not a percentage. If
you're trying to get the size equal to the original size, you want a value
of 1.
 
J

jon.souter

Jay,

Many thanks for your help with this. The first few steps on any decent
learning curve are always a little wobbly!

As you pointed out - the remaining code had some errors - but it was a
contrived example to try and highlight my reasons for assigning a
variable to the shape object in the first place.

I'll be sure to provide error messages in any future posts.

Thanks again,


Jon
 

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