PDA

View Full Version : [SOLVED:] Inserting pictures and text wrap in VBA



Roderick
11-04-2017, 10:19 AM
I want to insert a number of pictures around my document after various paragraphs.

This is the code I have at the moment:


Dim Shp as Shape
Set Shp = Selection.InlineShapes.AddPicture(fileName:= strPicFile, _
LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
With Shp
.WrapFormat.Type = wdWrapTight
End With
With Selection
.Collapse wdCollapseEnd
.TypeParagraph
End With


It does the job perfectly.

However, when I move to another blank paragraph further down the document and run this procedure again, it actually finds the first figure and places the new picture on top of it!

How can I get the new picture to be added where my cursor is located, please?

gmayor
11-04-2017, 10:32 PM
Your code does not indicate the filename of the image, but the following should insert the named wrapped image at the cursor location


Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 05 Nov 2017
Dim olShp As InlineShape
Dim oShp As Shape
Dim oRng As Range
Dim strPicFile As String
strPicFile = "C:\Path\picturename.jpg" 'Put your picture name here
Set oRng = Selection.Range
Set olShp = oRng.InlineShapes.AddPicture _
(FileName:=strPicFile, _
LinkToFile:=False, SaveWithDocument:=True)
Set oShp = olShp.ConvertToShape
With oShp
.WrapFormat.Type = wdWrapTight
End With
lbl_Exit:
Set oRng = Nothing
Set oShp = Nothing
Set olShp = Nothing
Exit Sub
End Sub

Roderick
11-05-2017, 01:35 PM
Thanks very much. It works a treat!

And thanks for the lesson, Graham.