1

I'm a bit new at this and I'm having problems mail merging into MS Word an image from a SQL Server table. This article.

I set a bookmark in Word as the placeholder for the image (unsure if this is best way).

Problem is the image is displaying in Word, but it is way too large.

Is there a way to resize the image before it gets to Word?

Here is my code:

Dim r As DataRow
r = dtLicence(0)

Dim MyPhoto As Object
MyPhoto = r("Photo")

myImage = MyPhoto 

Dim membits As New MemoryStream(myImage)

Dim WordPhoto As Image

WordPhoto = Image.FromStream(membits)
Clipboard.SetDataObject(WordPhoto)

word_server.Selection.GoTo( _
What:=Word.WdGoToItem.wdGoToBookmark, _
Name:="Photo")

With word_server.Selection
  .Paste()
End With

If anyone could provide some code sample for me to try I would appreciate it.

Bugs
  • 4,491
  • 9
  • 32
  • 41
Rodney
  • 13
  • 3

1 Answers1

0

You could try to resize the image with this function:

WordPhoto  = ScaleImage(Image.FromStream(membits), 300, 300)

And Here is the function I found:

Public Function ScaleImage(ByVal OldImage As Image, ByVal TargetHeight As Integer,_
                           ByVal TargetWidth As Integer) As System.Drawing.Image  
        Dim NewHeight As Integer = TargetHeight
        Dim NewWidth As Integer = NewHeight / OldImage.Height * OldImage.Width     
        If NewWidth > TargetWidth Then
            NewWidth = TargetWidth
            NewHeight = NewWidth / OldImage.Width * OldImage.Height
        End If  
        Return New Bitmap(OldImage, NewWidth, NewHeight)  
End Function

(Source)

Mederic
  • 1,949
  • 4
  • 19
  • 36