View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Sandy V Sandy V is offline
external usenet poster
 
Posts: 24
Default Export picture as gif in Excel

I think www.irfanview.com does all you need, one of the
best freeware app's out there. It can Batch convert file
types and resize at the same time (see batch/advanced
options), and much else. Although for batch resizing you
might first need to sort portrait/landscape, which you
could do in code (unless you have hundreds probably not
worthwhile).

Some ideas for you to play with to tidy up your code:

You can add an embedded chart, correctly sized and
referenced in one go. Here's a snippet of my adaptation of
Robin's original -

Dim chObj As ChartObject
'get 'lWidth & lHeight dimensions of your picture per
Robin's original or resized to your needs. Add extra
border to each (say 4)

Set chObj = Worksheets("Sheet2"). _
ChartObjects.Add(10, 10, lWidth, lHeight)
'Don't need to set any source data.
With chObj.Chart
.Paste 'the previously copied picture
.ChartArea.Border.LineStyle = 0
.ChartArea.ClearContents
.Export strLocation, "GIF", False
End With
chObj.Delete

Rather than inserting the image direct to the chart, try
first inserting it to a sheet (if it's not already there),
resize it as required, copy it, and paste it to
the "sized" chart as above.

Perhaps change the file address before exporting:
StrLocation = strLocation & "_MyMod"

With Robin's original I found it necessary to put the
chart not on the activesheet. In the above it is
elsewhere in the activeworkbook, although for my purposes
I put it in another workbook. Requires a bit of
referencing and switching workbooks. But for what you are
currently doing this is not relevant.

I still think IrfanView is better!

Regards,
Sandy

PS Your newsreader appears not to like plus's and some
other characters

-----Original Message-----
Robin et al,

Have put your efforts to good effect and expanded on

them. As I got to work
on my problem, I realised that I needed to work with the

original photo
before it got onto the spreadsheet and also take account

of portrait photos
as well as landscape ones, and the aim was to reduce the

overall size of the
spreadsheet by making a small gif of the original photo,

to end up with a
usable databse of data and accompanying photos. The

resultant code helps to
show what I came up with, which, as a novice, I am quite

chuffed with!
Hopefully the notes make sense and this code can be of

use for others. If
anyone can tidy it up and make a neater job of it, I am

all ears!

Regards

Joe90

+++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

+++++

Sub ConvertToGif()

'Converts a base picture to gif using chart export
'and pastes gif to spreadsheet, also copes with

portrait/landscape pictures,
sizing to height only

'fname relates to a compiled filename on the spreadsheet

in column 2 of the
active row

'picture size is set to fit a cell 8 columns in from the

left, 105w x 150h
in points and allows for a border of 4 around it
'as pasting a picture into the chart forces a border on

the top and left

'working sheet is called "data", and the range is "a100"

which I know is
blank, to create a blank chart

'"Picture 17.gif" allows for a default picture in my main

application,
incase a picture is not available
'you can delete the IF/Else part for "filetoopen" if you

want

'thanks to Robin Hammond for the starting point on this!


Dim lWidth As Long
Dim lHeight As Long
Dim chtname
Dim chtnametrim
Static strlocation As String

filetoopen = Application.GetOpenFilename("Image Files

(*.gif;*.jpg;*.bmp),
*.gif;*.jpg;*.bmp")
fname = ActiveCell.Offset(0, -ActiveCell.Column 2)
strlocation = ThisWorkbook.Path & "\" & fname & ".gif"

If filetoopen = ThisWorkbook.Path & "\Picture 17.gif" Then
ActiveSheet.Pictures.Insert filetoopen
Else
Charts.Add
ActiveChart.SetSourceData Source:=Sheets("data").Range

("a100")
ActiveChart.Location Whe=xlLocationAsObject,

Name:="Data"
With ActiveChart
.Pictures.Insert filetoopen
.ChartArea.Border.LineStyle = 0
.ChartArea.ClearContents
If .Shapes(1).Width .Shapes(1).Height Then
.Shapes(1).LockAspectRatio = msoFalse
ActiveChart.Shapes(1).Width = 142
ActiveChart.Shapes(1).Height = 97
Else
ActiveChart.Shapes(1).Height = 97
End If
lWidth = ActiveChart.Shapes(1).Width
lHeight = ActiveChart.Shapes(1).Height
End With
chtname = ActiveChart.Name
chtnametrim = Mid(chtname, 6, 20)
ActiveSheet.Shapes(chtnametrim).Width = lWidth 8
ActiveSheet.Shapes(chtnametrim).Height = lHeight 8
ActiveChart.Export strlocation, "GIF", False

ActiveChart.ChartArea.Select
Selection.Clear
With Sheets("data")
ActiveCell.Offset(0, -ActiveCell.Column 9).Select
..Pictures.Insert strlocation
End With
End If
End Sub
+++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

+
Snip