Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried the following code copied from various websites but I am no programmer. I want the image to load at the right hand side of the screen in Word opposite the cursor position.
Currently it loads at the top of the document or the centre of the first page if I use
.Top = wdShapeCenter

The code is as follows:
VB
Sub Apic()
'
' APic Place Picture at Cursor
'
Dim openDialog As Office.FileDialog
Dim shp As Shape

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
  openDialog.Filters.Clear
  openDialog.Filters.Add "JPEG Files", "*.jpg"
  openDialog.Filters.Add "Gif Files", "*.gif"
  openDialog.Filters.Add "PNG Files", "*.png"
  openDialog.Filters.Add "All Files", "*.*"
  If openDialog.Show Then
    imageName = openDialog.SelectedItems(1)
  End If

  Set shp = ActiveDocument.Shapes.AddPicture( _
     FileName:=imageName, _
       SaveWithDocument:=True, _
      Anchor:=ActiveDocument.Paragraphs(1).Range)
    With shp
    .Name = "PictureInsert"
    .LockAspectRatio = True
    .WrapFormat.AllowOverlap = False
    .WrapFormat.Side = wdWrapTight
    .WrapFormat.Type = 1
    .RelativeHorizontalPosition = _
         wdRelativeVerticalPositionMargin
    .RelativeVerticalPosition = _
        wdRelativeVerticalPositionMargin
    .Left = wdShapeRight
'    .Top = wdShapeCenter
  End With
End Sub


What I have tried:

Searching this site and also a lot of time searching web examples for VBA
Posted
Updated 8-Aug-16 21:37pm
v2
Comments
99Freddo 9-Aug-16 21:02pm    
Thanks for the reply. I tried that but get a compile error - syntax error on:
Set shp = ActiveDocument.Shapes.AddPicture( _
FileName:=imageName, _
SaveWithDocument:=True, _
Anchor:=Selection.Range
How do I change the original code to insert the image on the right hand side of the page opposite the current cursor position?
Thanks

1 solution

Replace:
VB
Anchor:=ActiveDocument.Paragraphs(1).Range

with
VB
Anchor:=Selection.Range

in AddPicture method to insert image in place where cursor blinks.

More: Selection Object (Word)[^]
 
Share this answer
 
v2
Comments
99Freddo 10-Aug-16 5:42am    
Thanks Thomas that did the trick - I also changed tha last section to read

With shp
.WrapFormat.Type = wdWrapTight
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
.Left = wdFrameRight
End With

now works fine. Fred
Maciej Los 10-Aug-16 10:08am    
You're very welcome.
BTW: My name is Maciej, not Thomas ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900