Insert picture as specific location?

K

Kenny Bones

Hi, I've finally figured out how to use a macro to insert an image as an
oShape and not inline shape.

However, I don't quite know how to figure the last bit out.
Preferrably, I would like to have a simple shape in the document.
And have a macro fire when the shape is clicked with the mouse.
Not sure if this is even possible.

But the thing is, I'm actually trying to create a picture placeholder for
Word 2003.
And I want the picture to replace the placeholder. I could use a button
though, but that would mean that the button would appear on the print of the
document.
I can't have that, so I would like to have a shape or something instead
which would not appear on the print, either by coloring it white or
something.

My current macro looks like this:

Code:
Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Dim strFilePath As String
Dim oDoc As Document
Set oDoc = ActiveDocument

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then
GoTo Endofcode
End If

Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath,
LinkToFile:=False, SaveWithDocument:=True)
With oShape
ImageWidth = .Width
ImageHeight = .Height
NewHeight = 30
NewWidth = 40
.Height = NewHeight
.Width = NewWidth
End With
Endofcode:
End Sub

Suggestions?
 
J

Jean-Guy Marcil

Kenny Bones was telling us:
Kenny Bones nous racontait que :
Hi, I've finally figured out how to use a macro to insert an image as
an oShape and not inline shape.

However, I don't quite know how to figure the last bit out.
Preferrably, I would like to have a simple shape in the document.
And have a macro fire when the shape is clicked with the mouse.
Not sure if this is even possible.

But the thing is, I'm actually trying to create a picture placeholder
for Word 2003.
And I want the picture to replace the placeholder. I could use a
button though, but that would mean that the button would appear on
the print of the document.
I can't have that, so I would like to have a shape or something
instead which would not appear on the print, either by coloring it
white or something.

My current macro looks like this:

Code:
Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Dim strFilePath As String
Dim oDoc As Document
Set oDoc = ActiveDocument

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then
GoTo Endofcode
End If

Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath,
LinkToFile:=False, SaveWithDocument:=True)
With oShape
ImageWidth = .Width
ImageHeight = .Height
NewHeight = 30
NewWidth = 40
.Height = NewHeight
.Width = NewWidth
End With
Endofcode:
End Sub

Suggestions?

I would use a MACROBUTTON field.
Insert a MACROBUTTON field as a placeholder (Insert > Fields) with the
following field code:

MACROBUTTON InsertPix Double Click to Insert a Picture

Make sure that the field sits alone in the paragraph.

If you want a floating picture, try this:

Code:
Sub InsertPix()

Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub

Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Anchor:=rgePlace)
With oShape
.Height = 30
.Width = 40
End With

rgePlace.Fields(1).Delete

End Sub

If you want the picture to actually sit where the field is, use an
InlineShape instead:

Code:
Sub InsertPix()

Dim oShape As Word.InlineShape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range
'To remove the ¶ from the range:
rgePlace.MoveEnd wdCharacter, -1

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub

Set oShape = rgePlace.InlineShapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=rgePlace)
With oShape
.Height = 30
.Width = 40
End With

rgePlace.Fields(1).Delete
Selection.Collapse wdCollapseStart

End Sub
 
K

Kenny Bones

Is it possible to convert an inline shape to an oShape?

Jean-Guy Marcil said:
Kenny Bones was telling us:
Kenny Bones nous racontait que :
Hi, I've finally figured out how to use a macro to insert an image as
an oShape and not inline shape.

However, I don't quite know how to figure the last bit out.
Preferrably, I would like to have a simple shape in the document.
And have a macro fire when the shape is clicked with the mouse.
Not sure if this is even possible.

But the thing is, I'm actually trying to create a picture placeholder
for Word 2003.
And I want the picture to replace the placeholder. I could use a
button though, but that would mean that the button would appear on
the print of the document.
I can't have that, so I would like to have a shape or something
instead which would not appear on the print, either by coloring it
white or something.

My current macro looks like this:

Code:
Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Dim strFilePath As String
Dim oDoc As Document
Set oDoc = ActiveDocument

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then
GoTo Endofcode
End If

Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath,
LinkToFile:=False, SaveWithDocument:=True)
With oShape
ImageWidth = .Width
ImageHeight = .Height
NewHeight = 30
NewWidth = 40
.Height = NewHeight
.Width = NewWidth
End With
Endofcode:
End Sub

Suggestions?

I would use a MACROBUTTON field.
Insert a MACROBUTTON field as a placeholder (Insert > Fields) with the
following field code:

MACROBUTTON InsertPix Double Click to Insert a Picture

Make sure that the field sits alone in the paragraph.

If you want a floating picture, try this:

Code:
Sub InsertPix()

Dim oShape As Word.Shape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub

Set oShape = oDoc.Shapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Anchor:=rgePlace)
With oShape
.Height = 30
.Width = 40
End With

rgePlace.Fields(1).Delete

End Sub

If you want the picture to actually sit where the field is, use an
InlineShape instead:

Code:
Sub InsertPix()

Dim oShape As Word.InlineShape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range
'To remove the ¶ from the range:
rgePlace.MoveEnd wdCharacter, -1

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub

Set oShape = rgePlace.InlineShapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=rgePlace)
With oShape
.Height = 30
.Width = 40
End With

rgePlace.Fields(1).Delete
Selection.Collapse wdCollapseStart

End Sub
 
J

Jean-Guy Marcil

Kenny Bones was telling us:
Kenny Bones nous racontait que :
Is it possible to convert an inline shape to an oShape?
Yes, but I do not understand how that is relevant to your situation.
You have code that inserts a picture, so if you prefer floating one, insert
it as a floating one, no need to convert...
 
K

Kenny Bones

Well, it is relevant since an inline shape gets inserted exactly where I want
it to. And an oShape is inserted at first page or whatever. That's why I need
to convert the shape to an oShape. I need to be able control where the
picture ends up.
 
C

Cindy M.

Hi Kenny,
Preferrably, I would like to have a simple shape in the document.
And have a macro fire when the shape is clicked with the mouse.
Not sure if this is even possible.
Barring the suggested macrobutton field, I see two possibilities

1. An ActiveX CommandButton (this is the same control as put in a Userform).
This can be formatted as a Shape

2. Use the WindowSelectionChange event. You'd need to test the Selection.Type,
if it's a Shape, then check the Name or some other property to be sure it's the
one you want, then execute the code.
But the thing is, I'm actually trying to create a picture placeholder for
Word 2003.
And I want the picture to replace the placeholder. I could use a button
though, but that would mean that the button would appear on the print of the
document.
I can't have that, so I would like to have a shape or something instead
which would not appear on the print, either by coloring it white or
something.

Actually, I'd consider using a TextBox or a Frame and insert the picture as an
InlineShape into that?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
J

Jean-Guy Marcil

Kenny Bones was telling us:
Kenny Bones nous racontait que :
Well, it is relevant since an inline shape gets inserted exactly
where I want it to. And an oShape is inserted at first page or
whatever. That's why I need to convert the shape to an oShape. I need
to be able control where the picture ends up.

I posted the code you would need to insert a floating picture anchored to
the paragraph that contains the MACROBUTTON field.
Did you try it?

In any case, you can try this variation, but when you convert an
InlineShape to a floating shape, it does move as well...
so the result are no better than what I initially posted, just different.

Sub InsertPix()

Dim oShape As Word.InlineShape
Dim Dlg As Office.FileDialog
Dim strFilePath As String
Dim oDoc As Document
Dim rgePlace As Range

Set Dlg = Application.FileDialog(msoFileDialogFilePicker)
Set oDoc = ActiveDocument
Set rgePlace = Selection.Range.Fields(1) _
.Result.Paragraphs(1).Range
'To remove the ¶ from the range:
rgePlace.MoveEnd wdCharacter, -1

With Dlg
.AllowMultiSelect = False
If .Show() <> 0 Then
strFilePath = .SelectedItems(1)
End If
End With

If strFilePath = "" Then Exit Sub

Set oShape = rgePlace.InlineShapes.AddPicture(FileName:=strFilePath, _
LinkToFile:=False, SaveWithDocument:=True, _
Range:=rgePlace.Fields(1).Result)
With oShape
.Height = 30
.Width = 40
.ConvertToShape
End With

rgePlace.Fields(1).Delete
Selection.Collapse wdCollapseStart

End Sub
 

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