Results 1 to 20 of 22

Thread: VBA Word msoBringToFront problem with inserting image at cursor's position

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #19
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Sometimes what seems simple with Microsoft just isn't. This is actually harder in concept than it looks-- you have to insert as an inline shape first in order to get any sort of cursor position information. And I am dubious as to how well this will work, however here is my code (which is really not that much different, substance-wise, than David's).

    The difference with my code is that it will move the picture upward and to the right (which is what I think you want), as well as deal with multiple selection scenarios (if you have multiple paragraphs selected and run David's code, I think those paragraphs will get deleted-- something I accidentally ran across while writing the code.

    Public Sub InsertImageAtCursor()
      Dim sImagePath As String
      Dim shp As Shape
      Dim shpInline As InlineShape
      Dim rngWhere As Range
    'where your image is
      sImagePath = "F:\Consulting\Development\m.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)
    'convert it to a floating shape
      Set shp = shpInline.ConvertToShape
      'lock the anchor, so that it stays associated with the paragraph it was inserting with
      shp.LockAnchor = True
    '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
      shp.Top = shp.Top - 55
    'and to the right
      shp.Left = shp.Left + 25
    End Sub
    However, I think this could all be simplified and not be a custom macro at all if you were to look up AutoText and QuickParts. And with the added benefit of having it more easily accessible from the User Interface under the Quick Parts gallery.

    Learning about autotext will help you with a lot of "macro" problems.
    Last edited by Frosty; 02-22-2011 at 10:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •