PDA

View Full Version : [SOLVED:] Signature



dodonohoe
11-13-2015, 04:09 AM
Hi all,

The code below inserts a Signature .png image into a word document at a bookmark called "signature". This works fine if the signature is the last shape in the document. If it is not the last shape then it selects the last shape instead. I would appreciate any direction as to how to resize the signature .png image as soon as it is added to the bookmark location.


Sub AddSignaturePNG()


Application.ScreenUpdating = False


'call the signature file

Call FillABookmarkX("signature", "c:\Signature\Signature.png")

'Resize the added signature
PicResizeX

Application.ScreenUpdating = True



End Sub


Sub FillABookmarkX(strBM As String, strText As String)


Dim j As Long
With ActiveDocument
.Bookmarks(strBM).Range _
.InlineShapes _
.AddPicture FileName:=strText


j = ActiveDocument.InlineShapes.Count
.InlineShapes(j).Select

.Bookmarks.Add strBM, Range:=Selection.Range

End With

End Sub


Sub PicResizeX()
Dim PecentSize As Integer


PercentSize = 7


If Selection.InlineShapes.Count > 0 Then
Selection.InlineShapes(1).ScaleHeight = PercentSize
Selection.InlineShapes(1).ScaleWidth = PercentSize
Else
Selection.ShapeRange.ScaleHeight Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
Selection.ShapeRange.ScaleWidth Factor:=(PercentSize / 100), _
RelativeToOriginalSize:=msoCTrue
End If
End Sub

gmayor
11-13-2015, 06:12 AM
7% seems very small? However

Option Explicit

Sub AddSignaturePNG()
Application.ScreenUpdating = False
'call the signature file
Call FillABookmarkX("signature", "c:\Signature\Signature.png")
Application.ScreenUpdating = True
End Sub


Sub FillABookmarkX(strBM As String, strText As String)
Dim oShape As InlineShape
With ActiveDocument
Set oShape = .Bookmarks(strBM).Range _
.InlineShapes _
.AddPicture(Filename:=strText)
PicResizeX oShape
oShape.Select
.Bookmarks.Add strBM, Range:=Selection.Range
End With
End Sub


Sub PicResizeX(oShape As InlineShape)
Dim PercentSize As Integer
PercentSize = 7
oShape.ScaleHeight = PercentSize
oShape.ScaleWidth = PercentSize
End Sub

gmaxey
11-13-2015, 06:31 AM
Private m_oILS As InlineShape
Sub AddSignaturePNG()
FillABookmarkX "signature", "D:\Signature.png"
With m_oILS
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Sub FillABookmarkX(strBM As String, strFile As String)
Set m_oILS = ActiveDocument.Bookmarks("signature").Range.InlineShapes.AddPicture(strFile)
ActiveDocument.Bookmarks.Add strBM, m_oILS.Range
End Sub

dodonohoe
11-13-2015, 07:01 AM
Hi Gmayor,

I owe you a pint, that worked perfectly thank you very much indeed! I am marking this thread as solved.

Des

dodonohoe
11-13-2015, 07:06 AM
Hi Greg,

First of all, thanks for the reply. I tried your solution also and it is throwing a variable not defined error for "m_oILS". What should I define "m_oILS" as?

Cheers,

Des



Private m_oILS As InlineShape
Sub AddSignaturePNG()
FillABookmarkX "signature", "D:\Signature.png"
With m_oILS
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Sub FillABookmarkX(strBM As String, strFile As String)
Set m_oILS = ActiveDocument.Bookmarks("signature").Range.InlineShapes.AddPicture(strFile)
ActiveDocument.Bookmarks.Add strBM, m_oILS.Range
End Sub

gmaxey
11-13-2015, 07:15 AM
m_oILS is declared at the module level as shown e.g.m Private m_oILS as InlineShape

It is declared at the module level because it is used in both procedures.

gmaxey
11-13-2015, 07:25 AM
There are all kinds of ways you can do this. Here is one that doesn't use any declared variables:


Sub AddSignaturePNG()
With fcnStuffPictureInBookmark("signature", "D:\Signature.png")
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Function fcnStuffPictureInBookmark(strBM As String, strFile As String) As InlineShape
Set fcnStuffPictureInBookmark = ActiveDocument.Bookmarks("signature").Range.InlineShapes.AddPicture(strFile)
ActiveDocument.Bookmarks.Add strBM, fcnStuffPictureInBookmark.Range
End Function

dodonohoe
11-13-2015, 07:46 AM
Hi Greg,

My bad, I completely missed that. Both of the solutions worked perfectly for me so marking this thread as solved.

Thanks again,

Des


m_oILS is declared at the module level as shown e.g.m Private m_oILS as InlineShape

It is declared at the module level because it is used in both procedures.

gmaxey
11-13-2015, 08:45 AM
You're welcome. Do I not get a pint?

dodonohoe
11-13-2015, 09:18 AM
Greg,

I will gladly buy you a creamy dreamie!

Cheers,

Des


You're welcome. Do I not get a pint?

dodonohoe
11-16-2015, 02:27 AM
I had marked this thread as solved but I was wondering if the code could be modified so that you didn't need to put a bookmark in but rather the signature would go where the cursor was active? No problem if I need to open a new thread I'm not sure of the protocol.

Thanks


There are all kinds of ways you can do this. Here is one that doesn't use any declared variables:


Sub AddSignaturePNG()
With fcnStuffPictureInBookmark("signature", "D:\Signature.png")
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Function fcnStuffPictureInBookmark(strBM As String, strFile As String) As InlineShape
Set fcnStuffPictureInBookmark = ActiveDocument.Bookmarks("signature").Range.InlineShapes.AddPicture(strFile)
ActiveDocument.Bookmarks.Add strBM, fcnStuffPictureInBookmark.Range
End Function

gmaxey
11-16-2015, 04:24 AM
Sub AddSignaturePNG()
With fcnStuffPictureAsSelection("signature", "D:\Signature.png")
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Function fcnStuffPictureAsSelection(strBM As String, strFile As String) As InlineShape
Set fcnStuffPictureAsSelection = Selection.Range.InlineShapes.AddPicture(strFile)
End Function

dodonohoe
11-16-2015, 04:37 AM
Greg,

That fits my needs perfectly. Thanks very much indeed.

Des



Sub AddSignaturePNG()
With fcnStuffPictureAsSelection("signature", "D:\Signature.png")
.ScaleHeight = 7
.ScaleWidth = 7
End With
End Sub

Function fcnStuffPictureAsSelection(strBM As String, strFile As String) As InlineShape
Set fcnStuffPictureAsSelection = Selection.Range.InlineShapes.AddPicture(strFile)
End Function