Consulting

Results 1 to 7 of 7

Thread: New to Macros. Want to insert an image over text

  1. #1
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    2
    Location

    Question New to Macros. Want to insert an image over text

    I've been trying to avoid just asking somebody to write the code for me, but I'm new to this and searched to no avail for similar code that I code copy and modify to my needs.
    I want create macro so that users can insert .png image of their signature in between sincerely and their name. The .png has a transparent background, so I'd like image to go over the text and overlap like a natural signature would. I plan on having them just insert on the line where the signature would go.

    I've got the easy part:

    Selection.InlineShapes.AddPicture FileName:= _
    "C:\Users\user1\Pictures\sigfile.png", LinkToFile:=False, _
    SaveWithDocument:=True
    End Sub

    Can somebody help me with image being set to "wrap" in front of text?

    Thanks,

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    AJR,

    Think about it. An InlineShape is inline with text. There for it can't be in front of text. In your case you will have to convert it to a shape first:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oILS As InlineShape, oShp As Shape
    Set oILS = Selection.InlineShapes.AddPicture(FileName:= _
     "C:\Lin-F.jpg", LinkToFile:=False, _
     SaveWithDocument:=True)
    Set oShp = oILS.ConvertToShape
    With oShp
      .WrapFormat.Type = wdWrapBehind
      .Height = 30
      .Width = 100
    End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    2
    Location
    Greg,
    Thanks. Like I said, I'm new. My inability to think was based on a lack of prior knowledge, but what you posted is great and will help me think these out in the future.
    I'll be able to adapt that to fit into my needs.
    Alex

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    If you want the signature to start in Paragraph 20 e.g. on the page, you can use:

    Sub M_snb()
        ThisDocument.Shapes.AddPicture("G:\OF\signature.png", , , 0, ThisDocument.Paragraphs(20).Range.Information(8), 120, 50).WrapFormat.Type = 5
    End Sub

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    snb,

    Realizing it is spread over three lines, and considering the OP wants the image inserted at the selection and not paragraph 20, I think this would make more sense:

    Sub M_snb_Modified()
      ThisDocument.Shapes.AddPicture("C:\Lin-F.jpg", , , 0, _
        Selection.Paragraphs(1).Range.Information(wdVerticalPositionRelativeToTextBoundary), _
        120, 50).WrapFormat.Type = wdWrapBehind
    End Sub
    
    Other than it being fewer characters, what is the point of using the completely vague (to most) 8 and 5 when the constants work just as well?
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    did you know ?

        Selection.Information(8)=Selection.Paragraphs(1).Range.Information(wdVerticalPositionRelativeToTextBoundary)

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Not specifically no, but what is the point in writing code so concise that only you and perhaps a handful of others would readily understand what your code does?

    The version of your code that I modified isn't perfect or crystal clear because I chose not to list the parameters, still it is pretty clear what piece of information the code is after. What is the point of using the cryptic 8 when wdVerticalPositionRelativeToTextBoundary is not only more descriptive but also one of the autofill options?

    Besides Selection.Paragraphs(1).Range.Information could easily be changed to Selection.Paragraphs(2).Range.Information if the user wanted information on the second selected paragraph.
    Greg

    Visit my website: http://gregmaxey.com

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
  •