Results 1 to 8 of 8
  1. #1
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 2010 .png files won't insert at cursor even when behind text is set - VBA attached

    Note: The post title is incorrect: substitute "behind text" for "inline"

    My company recently switched from Word 2002 to Word 2010 64-bit. For signature stamps in 2002, .bmp files were used. For 2010, we switched to .png files for transparency. Even though Word's Options setting is set to insert the graphics "behind text," the .png stamps always appear at the top left on page instead of the desired spot at the cursor. When some people stamp their names 75 times a day, moving the .png stamp from top left down to the signature line on the form gets annoying and end up calling the Help Desk. What can I do with VBA to have the .png file stamp insert behind text at the cursor location?

    Thanks,
    Charlie
    charlie6067

    Here is the Word 2010 VBA:

    Sub Signature()

    On Error GoTo Errhandler

    With ActiveDocument.Shapes.AddPicture(Anchor:=Selection .Range, FileName:= _
    "\jakdc1613:\\E-Signature-DO NOT DELETE\ Signature.png", LinkToFile:=False, _
    SaveWithDocument:=True)
    .WrapFormat.Type = 3
    .ZOrder 5

    End With

    Exit Sub

    Errhandler:

    MsgBox ("ERROR: The Signature stamp can not be found." & Chr(13) & "Please report this error to the Help Desk.")

    End Sub
    Last edited by charlie6067; 2014-07-30 at 08:37.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,524
    Thanks
    3
    Thanked 143 Times in 136 Posts
    I don't know why your anchor command is failing but a workaround that should work is the following. It inserts the graphic as an inline shape which puts the graphic in the right paragraph and then converts it to a shape so the anchor stays put.
    Code:
    Sub Signature()
      Dim rng As Range
      On Error GoTo Errhandler
      Set rng = Selection.Paragraphs(1).Range
      rng.Collapse Direction:=wdCollapseStart
      With ActiveDocument.InlineShapes.AddPicture(FileName:="\jakdc1613:\\E-Signature-DO NOT DELETE\ Signature.png", LinkToFile:=False, SaveWithDocument:=True, Range:=rng)
        With .ConvertToShape
          .WrapFormat.Type = 3
          .ZOrder 5
          .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
          .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
        End With
      End With
      Exit Sub
    Errhandler:
      MsgBox ("ERROR: The Signature stamp can not be found." & Chr(13) & "Please report this error to the Help Desk.")
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Andrew - thank you so very much for adding the code and for your williness to help. It is much appreciated!!

    thanks again,
    Charlie
    charlie6067

  4. #4
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code worked perfectly.
    Thank you again, Andrew!

    Charlie
    charlie6067

  5. #5
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Andrew,

    This code ran perfectly in a document that contained only text. We have some forms that were build inside a large 1 cell table. When the code runs, the error handler kicks off the message and the signature graphic appears at the top left as an inline graphic. This pushes the surrounding text down. How would you change the code if the insertion point is in the middle of a table? It seems to run Ok if table has multiple rows, but doesn't run correctly if inserted inside of a large table cell. I've attached an example in Word 2010.

    Thank you again for getting me this far...
    Charlie
    charlie6067
    Attached Files Attached Files

  6. #6
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,524
    Thanks
    3
    Thanked 143 Times in 136 Posts
    Charlie

    I can't replicate the error you are getting but the error handler is only set up for one issue whilst there is plenty of things that could actually cause an error. Try this variation and see what is really causing your error.
    Code:
    Sub Signature()
      Dim rng As Range
      On Error GoTo Errhandler
      Set rng = Selection.Paragraphs(1).Range
      rng.Collapse Direction:=wdCollapseStart
      With ActiveDocument.InlineShapes.AddPicture(FileName:="\jakdc1613:\\E-Signature-DO NOT DELETE\Signature.png", LinkToFile:=False, SaveWithDocument:=True, Range:=rng)
        With .ConvertToShape
          .WrapFormat.Type = 3
          .ZOrder 5
          .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
          .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
        End With
      End With
      Exit Sub
    Errhandler:
      If Err.Number = 5152 Then
        MsgBox "ERROR: The Signature stamp can not be found." & Chr(13) & "Please report this error to the Help Desk."
      Else
        MsgBox "An error was encountered. Please report this error to the Help Desk: " & vbCr & Err.Number & vbCr & Err.Description
      End If
    End Sub
    I am not sure how your filename path is customised for each user but I would be exploring whether you can store the signature as a building block and use much simpler code to just insert that building block.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  7. #7
    Star Lounger
    Join Date
    May 2009
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Andrew - many thanks for your quick reply and code. We were thrown into Office 2010 with little time to investigate how it differs from Word 2002. I've heard of building blocks, but have not yet used them. I will find out how they work and thank you for the suggestion.

    Have a great weekend!
    Charlie
    charlie6067

  8. #8
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Madison, Wisconsin, Wisconsin, USA
    Posts
    1,701
    Thanks
    56
    Thanked 63 Times in 61 Posts
    Building Blocks are a much richer version of AutoText.

    http://www.addbalance.com/usersguide...utocorrect.htm

    http://gregmaxey.mvps.org/word_tip_p..._autotext.html

    Through use of Global Templates particular to each user, it is possible to have a building block with the same name be customized to each user.
    Last edited by Charles Kenyon; 2014-08-01 at 09:58.
    Charles Kyle Kenyon
    Madison, Wisconsin

Tags for this Thread

Posting Permissions

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