PDA

View Full Version : [SOLVED:] New to Macros. Want to insert an image over text



AJR
03-19-2014, 08:21 PM
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,

gmaxey
03-19-2014, 09:22 PM
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

AJR
03-20-2014, 09:46 AM
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

snb
03-21-2014, 07:24 AM
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

gmaxey
03-21-2014, 11:52 AM
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(wdVerticalPositionRelativeToTextB oundary), _
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?

snb
03-21-2014, 03:24 PM
did you know ?


Selection.Information(8)=Selection.Paragraphs(1).Range.Information(wdVertic alPositionRelativeToTextBoundary)

gmaxey
03-21-2014, 03:58 PM
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.