Format Picture Macro

  • Thread starter Thread starter Raymond O'Neill
  • Start date Start date
R

Raymond O'Neill

Hi!

I'm having trouble recording a macro that formats pictures inserted from
file.
The macro stops and the debugger takes me to this line:
Selection.InlineShapes(1).Left = 0#

Also, how can I make a macro to insert picture from file that allows the
user to select a file and then continues with the macro inserting and
formating the picture?

Thanks!
 
Raymond said:
Hi!

I'm having trouble recording a macro that formats pictures inserted
from file.
The macro stops and the debugger takes me to this line:
Selection.InlineShapes(1).Left = 0#

Also, how can I make a macro to insert picture from file that allows
the user to select a file and then continues with the macro inserting
and formating the picture?

Thanks!

Hi Raymond,

The trouble with the code you quoted is that an InlineShape is treated like
a single character, and therefore it doesn't have a .Left property. You need
to insert the picture as a floating object, a Shape, instead of an
InlineShape. This isn't something you can record.

To answer your second question, the preferred method is to call Word's
built-in Insert Picture dialog (see
http://word.mvps.org/FAQs/MacrosVBA/WordDlgHelp.htm). If the user selects a
picture and clicks OK instead of Cancel, then you can insert the picture as
a Shape and format it. Have a look at this code:

Sub Macro1()
Dim strFile As String
Dim oShape As Shape

' use the built-in dialog to let user
' choose the picture
With Dialogs(wdDialogInsertPicture)
If .Display <> -1 Then Exit Sub
strFile = .Name
End With

' insert the picture floating with the anchor
' at the top-left corner of the paragraph that
' contains the cursor
Set oShape = ActiveDocument.Shapes.AddPicture _
(FileName:=strFile, _
Anchor:=Selection.Range)

' format the picture as desired
With oShape
.WrapFormat.Type = wdWrapSquare
.Left = 0 ' at left margin
.Width = InchesToPoints(2.25)
' etc...
End With
End Sub
 
Back
Top