-
-
As suspected, much more complicated than it seems.
The code would not fail if you inserted anywhere but at the bottom of the page. Word is becoming confused when the *only* reason for a new page is the recently inserted graphic. I get around this by making the graphic very very small, and then restoring the size.
However-- this code can still break in some scenarios.
If the below does not work for you... you need to look up an autotext solution. Autotext would be a trivial solution for this problem, and it is a good skill to have.
Good luck.
[vba]
Public Sub InsertImageAtCursor()
Dim sImagePath As String
Dim shp As Shape
Dim shpInline As InlineShape
Dim rngWhere As Range
Dim lOrigHeight As Long
Dim lOrigWidth As Long
'where your image is
sImagePath = "C:\wetude\s.png"
'get the current selection
Set rngWhere = Selection.Range.Duplicate
'collapse the range, to prevent issues with a non-insertion cursor
rngWhere.Collapse wdCollapseEnd
'get our inline shape
Set shpInline = rngWhere.InlineShapes.AddPicture(sImagePath, , , rngWhere)
'store the original height/width
lOrigHeight = shpInline.Height
lOrigWidth = shpInline.Width
'make it really really tiny, to prevent page changing before the conversion
shpInline.Height = 0.1
shpInline.Width = 0.1
'convert it to a floating shape
Set shp = shpInline.ConvertToShape
'restore the height/width
shp.Height = lOrigHeight
shp.Width = lOrigWidth
'adjust to behind text (which I think you want, instead of infront of text)
'you can replace with msoBringInFrontOfText if desired
shp.ZOrder msoSendBehindText
'adjust it up (change this to move it around)
shp.Top = shp.Top - 55
'and to the right (move this around as needed)
shp.Left = shp.Left - 100
End Sub
[/vba]