The most important thing is that the code is not optimised! What it means?
1) if the part of procedure is used more than one time, you need to use this as a separatly function/procedure or to send it as a parameter!
The procedures
TestInsertPictureInRange
and
Export_Range_Images
should have as an input parameter the name of image, as you use it in
InsertPictureInRange
procedure.
Sub TestInsertPictureInRange (ByVal PictureFileName AS String)
End Sub
Sub Export_Range_Images (ByVal PictureFileName AS String)
End Sub
2) if the code has no context, the effect of its job can bring unexpected results!
For example, try to run this procedure:
Sub Test
Range("A1") = "Hello Kitty!"
End Sub
for 3 opened workbooks and for each sheet in them.
Please, see my code (with error-handler):
Option Explicit
Sub Test()
InsertImageInRange "F:\Download\Saalbach009.jpg", Format(Date, "yyyyMMdd")
InsertImageInRange "F:\Download\Saalbach009.jpg", Format(Date, "yyyyMMdd"), "A1:I29"
End Sub
Sub InsertImageInRange(ByVal PictureFileName As String, ByVal DefaultSheetName As String, Optional ByVal DefaultTargetRange As String = "G10:N30")
Dim wsh As Worksheet, rng As Range, pic As Shape
On Error GoTo Err_ExportRangeAsImage
Set wsh = ActiveWorkbook.Worksheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))
wsh.Name = DefaultSheetName
Set rng = wsh.Range(DefaultTargetRange)
Set pic = wsh.Shapes.AddPicture(PictureFileName, msoFalse, msoCTrue, rng.Left, rng.Top, _
rng.Offset(0, rng.Columns.Count).Left - rng.Left, rng.Offset(rng.Rows.Count, 0).Top - rng.Top)
Exit_ExportRangeAsImage:
On Error Resume Next
Set wsh = Nothing
Set rng = Nothing
Set pic = Nothing
Exit Sub
Err_ExportRangeAsImage:
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_ExportRangeAsImage
End Sub
More you'll find at:
http://www.jpsoftwaretech.com/export-excel-range-to-a-picture-file/[
^]
http://dmcritchie.mvps.org/excel/xl2gif.htm[
^]
http://www.ozgrid.com/forum/showthread.php?t=65781[
^]