We have created a spreadsheet where the cell's colours etc are conditional based on the state of an external source (PLC tags using RSLinx). We have written a macro to take a screen shot of a specific range and save it as a JPG. We have then created a website to display an image of the spreadsheet that will update every 30 seconds or so to give a 'live' representation of whats going on. The macro is shown below:
Option Explicit
Private Sub SaveRngAsJPG(Rng As Range, FileName As String)
Dim Cht As Chart, bScreen As Boolean, Shp As Shape
bScreen = Application.ScreenUpdating
Application.ScreenUpdating = False
Set Cht = Workbooks.Add(xlChart).Charts(1)
Cht.ChartArea.Clear
Rng.CopyPicture xlScreen, xlPicture
Cht.Paste
With Cht.Shapes(1)
.Left = 0
.Top = 0
.Width = Cht.ChartArea.Width
.Height = Cht.ChartArea.Height
End With
Cht.Export FileName, "PNG", False
Cht.Parent.Close False
Application.ScreenUpdating = bScreen
End Sub
Sub TestIt2()
Dim Rng As Range, Fn As String
Set Rng = Range("A3:AQ40")
Fn = "C:\Users\Desktop\Website\LHImage.png"
SaveRngAsJPG Rng, Fn
End Sub
The problem we have is that the image is saved with the dimensions 981x659. This distorts the image and when I try to alter the size in the .width and .height lines it will resize the subject of the image but leave white space around it to a full size of 981x659.
Is there any way I can alter the size of the image to keep its aspect ratio prior to saving it?
Any help would be greatly appreciated!