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