PDA

View Full Version : [SOLVED:] Pasting picture fails if insertion point is not at beginning of line



Firepig
01-18-2014, 05:10 AM
I use a macro to insert my signature as a .png graphic (in one of three formats chosen in a UserForm dialog box) into my document at the current insertion point, then send it Behind Text so it doesn't disrupt the layout of the document. The macro fails at the line
.AddPicture FileName:=PicturePath, LinkToFile:=False, SaveWithDocument:=True, Range:=rng if the insertion point is not at the beginning of a line. If the insertion point is at the beginning of a line in the document, the macro works perfectly. The Run Time Error message is attached - if I click End the graphic is inserted, but the macro stops so it does not get sent behind text.
What am I doing wrong? Thanks for any help.

Sub InsertChosenSignature()
'Once the Userform has filled the variable ChosenName, this Sub inserts the signature
'corresponding to the choice into the document and sends it behind text


Dim PicturePath As String
PicturePath = "Z:\Consultancy\Signatures\Signature-" & chosenname & "-66px.png"
Dim shPicture As Shape
Dim rng As Range
Set rng = Selection.Range
rng.Collapse Direction:=wdCollapseStart
With ActiveDocument.InlineShapes
.AddPicture FileName:=PicturePath, LinkToFile:=False, SaveWithDocument:=True, Range:=rng
Set shPicture = .Item(.Count).ConvertToShape
End With
With shPicture
.WrapFormat.Type = wdWrapNone
.ZOrder msoSendBehindText
.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
.RelativeVerticalPosition = 3 'relative to line
.Left = 0
.Top = 0
.LockAnchor = True
'position here
End With
End Sub

Jay Freedman
01-20-2014, 09:33 PM
In the .AddPicture command, try replacing the argument Range:=rng with


Range:=rng.Paragraphs(1).Range

If that works for you, you can remove the rng.Collapse statement.

Because you're going to position the Shape at the left margin, it doesn't make any difference whether the InlineShape is inserted at the actual insertion point or at the start of the paragraph that contains the insertion point.

Firepig
01-21-2014, 02:33 AM
Many thanks Jay; that works. I had hoped to position the graphic at the exact insertion point, but looking at my code it didn't do that anyway. It would be nice if it appeared at the line of the insertion point, rather than at the beginning of the paragraph, or perhaps below the end of the paragraph if that's easier, but in the vast majority of uses it will be a one-line paragraph (Yours Sincerely), and I usually drag the graphic a little for fine adjustment after running the macro anyway.
I have tweaked the code with Top = 12 so the signature appears below the line, and also brought the graphic to the front rather than sending it to the back, so it's easier to drag to reposition - the signature graphics have a transparent background so they don't obscure text if they overlay it.
Thanks again.