Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

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

  1. #1

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

    Hi.

    I've got this problem.

    I've done vba script which is supposed to insert an image at the position of the cursor AND bring this image to front.

    That's my solutions.

    If I do this :

    Sub Macro1()
        Selection.InlineShapes.AddPicture FileName:="C:\wetude\s.png", LinkToFile:=False, SaveWithDocument:=True
    End Sub
    The image is inserted at the current cursor's position


    And when I do this :

    Sub Macro2()
      With ActiveDocument.Shapes.AddPicture(FileName:="C:\wetude\f.png", LinkToFile:=False)
        .ZOrder msoBringToFront
    End With
    End Sub
    The image is brang in front, but at the top of the page.


    How can I mix those two ? I tried everything my noob brain can do. Each time, it's not working.

    Please help

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Welcome to VBA Express!

    You're adding two different shapes.

    Inlineshapes are on the Text layer while Shapes are on the Drawing layer.

    From my research, you can't Zorder a InlineShape; only Shapes. But you can change from one shape to the other.

    So, having stuck my neck out...

    Sub Macro1()
    With Selection.InlineShapes
        .AddPicture FileName:="C:\wetude\s.png", LinkToFile:=False, _
        SaveWithDocument:=True, Range:=Selection.Range
    End With
    With ActiveDocument
        .InlineShapes(.InlineShapes.Count).ConvertToShape
        With .Shapes(.Shapes.Count)
            .ZOrder msoBringToFront
        End With
    End With
    End Sub
    David

    David


  3. #3
    Thank you very much for such a quick answer, you really helped me understanding the problem.

    However, it persists. Indeed, the vba script works very weel when there is no image in the document... if they are some, that's what I get :

    Error of execution '-2147467259 (80004005)'
    The method 'ConvertToShape' of the 'Inline Shape' object have failed

    In fact, the script tries to convert every shape in the document, instead of converting the one inserted...

    Would you know what to do ?

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Can you explain you are trying to accomplish? I know you're trying to put in a shape, but maybe describe the logic a little.

    If you need a shape, then put a shape in and don't bother with Inlineshapes. I just wrote it that way to demonstrate options.

    Can you post a small sample file?

    David


  5. #5
    Hey !

    Thankyou for answer.

    In fact, it's much simpler than what you're asking.

    I'm just trying to write a macro, that insert an image at the cursor's position, and bring it to front.

    As I told you, and I don't know how to put those actions together.

    The code you wrote (and god, I'm thankful !) works fine, but if an image is already inserted somewhere in the word document, an error message appears.

  6. #6
    Any answer ? Anybody have an idea ?

  7. #7
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I'll take a crack at explaining:

    Macro 1 is inserting the picture into the text layer (where all the words are)

    Macro 2 is inserting the picture into the drawing layer (where most of the pictures are).

    You don't need to "bring to front" for macro 1... no more than you need to tell a word you've just typed to be "in front" of the word you typed just before that word.

    Macro 2 is totally different, as it does insert the picture into the drawing layer (which exists above and below the words), however, once you have a floating picture... it's no longer associated with some spot in between the words (i.e., your "cursor"). So you need to define where it is on the page.

    Which, without getting really complicated for all the possibilities... kind of requires one of two things

    1) you uploading some attachments so that we can figure out why you think the picture inserted from Macro 1 needs to be "brought to the front" (my guess is there is a *different* picture which needs to be "sent to back")

    2) you giving a more full description of what you're really trying to accomplish.

    Hope that helps.

  8. #8
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Sorry for the delay. Just had to add the range for the shape.

    Sub AddaPix()
        With Selection.InlineShapes
        .AddPicture FileName:="C:\wetude\s.png", LinkToFile:=False, _
        SaveWithDocument:=True, Range:=Selection.Range
        End With
    With ActiveDocument
        .InlineShapes(.InlineShapes.Count).ConvertToShape
        With .Shapes.Range(.Shapes.Count)
            .ZOrder msoBringToFront
        End With
    End With
    End Sub

    David


  9. #9
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I think your code is going to cause problems if your cursor isn't at the end of the document, David.

    You insert at the selection (which can be anywhere), and then convert whatever the last picture in the ActiveDocument.Shapes index is.

    There are a number of scenarios where that code will break. It will work in a blank new document, but that's about the only guaranteed location.

    I think the OP needs to give more info, or have a better understanding of coding to be able to use your solution.

  10. #10
    Thank you for those replies, you're really spending some time explaining me and I can only appreciate your efforts.

    I choose the second requierement, explaining in details.

    It's not so complicated. In fact, I'm using Word everyday at the office. And I have to sign each document which is sent to me. Hundreds of them.

    I just scanned my signature, erased the background, and now I'm trying to write this macro, that inserts it at the cursors position, and bring it to front, placing it above the text, and no next to it.

    I hope it's not too hard, I understand what you exaplained it it seems complicated.

    Thanks !

  11. #11
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    I have to do the very same thing, but I have four signature blocks.

    What I did was create a table, fixed the table row height, then inserted the inlineshape. The nice thing about the fixed height is that the graphic automatically resizes to fit the cell.

  12. #12
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Ah. You don't need something "in front" of something else... you need it above.

    So you want it to look like this:

    Sincerely,
    [My Signature]
    John Smith

    Instead of...

    Sincerely,
    [My Signature]John Smith

    Correct? I understand not wanting to upload an example document with your electronic signature for everyone to see.

    I think you'll do just fine with the following:

    Sub Macro1()
        Selection.InlineShapes.AddPicture FileName:="C:\wetude\s.png", LinkToFile:=False, SaveWithDocument:=True
    selection.InsertAfter vbcr
    End Sub
    Or you could just record the macro, stick with using inline shapes... but hit ENTER after the graphic is inserted, in order to "bring it above" your name.

    You're confusing layers with order... the "bring to front" and "send to back" concept have to do with looking *down* at the page, as if you're looking at the top of the building in a satellite image and trying to describe the roof vs. the basement.

    If, at this point, this is not what you need... try to upload a document with the signature block and just a fake graphic (scan an image of you writing out "hello", maybe).

    The table idea is also great for images, especially since they automatically re-size (as David pointed out).

    You can also look up the concept of "Autotext" or "Building Blocks" and look at just saving your electronic signature as that (depending on the version of Word you are using).

    Hope that helps.

  13. #13
    No problem I'll send you files by mp.

  14. #14
    I have to post 4 messages to send an mp

  15. #15
    Hate flooding, however... have to

  16. #16
    Only one post left... :

  17. #17
    Done.

    I'm REALLY SORRY... :

  18. #18
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    OK, I fired up my 2003 and this seems to work.

    This also corrects the problem Frosty addressed with multiple shapes in the document.

    Sub AddaPix()
    Dim MyInline As InlineShape
    Dim MyShape As Shape
    With Selection.InlineShapes
        Set MyInline = .AddPicture(FileName:="C:\wetude\s.png", LinkToFile:=False, _
        SaveWithDocument:=True, Range:=Selection.Range)
    End With
    With ActiveDocument
         Set MyShape = MyInline.ConvertToShape
         With MyShape
             .ZOrder msoBringToFront
         End With
    End With
    End Sub

    David


  19. #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.

  20. #20
    Thanks for quality replies !

    However, still, same error message for your two propositions. And on two different computers. The second time, it never works...

Posting Permissions

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