Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract images from excel files 5

Status
Not open for further replies.

Sverre

Programmer
Feb 16, 2005
33
NO
I am working on a project with a lot of excel files, each containing some tables and one sigle picture. The picture is showing up as "picture 1" in the name box.

I would like to save this picture to an external file using vba.

Any ideas?



---------------------------
There are only 10 types of people in the world. Those who know binary numbers and those who don't
 

Hi,

The only object that you can EXPORT is a Chart Object.

The only option that you might have is copy 'n' paste into a new slide in Powerpoint. Then use the Powrepoint export slide method.
PP VBA Help said:
Export Method


Presentation object: Exports each slide in the presentation, using the specified graphics filter, and saves the exported files in the specified folder.

Slide or SlideRange object: Exports a slide, or range of slides, using the specified graphics filter, and saves the exported file under the specified file name.

Syntax 1

expression.Export(Path, FilterName, ScaleWidth, ScaleHeight)

Syntax 2

expression.Export(FileName, FilterName, ScaleWidth, ScaleHeight)

expression Required. An expression that returns a Presentation object (Syntax 1) or a Slide or SlideRange object (Syntax 2).

Path Required String. The path of the folder where you want to save the exported slides. You can include a full path; if you don't do this, PowerPoint creates a subfolder in the current folder for the exported slides.

FileName Required String. The name of the file to be exported and saved to disk. You can include a full path; if you don't, PowerPoint creates a file in the current folder.

FilterName Required String. The graphics format in which you want to export slides. The specified graphics format must have an export filter registered in the Windows registry. You can specify either the registered extension or the registered filter name. PowerPoint will first search for a matching extension in the registry. If no extension that matches the specified string is found, PowerPoint will look for a filter name that matches.

ScaleWidth Optional Long. The width in pixels of an exported slide.

ScaleHeight Optional Long. The height in pixels of an exported slide.

Remarks

Exporting a presentation doesn't set the Saved property of a presentation to True.

PowerPoint uses the specified graphics filter to save each individual slide in the presentation. The names of the slides exported and saved to disk are determined by PowerPoint. They're typically saved as Slide1.WMF, Slide2.WMF, and so on (for example). The path of the saved files is specified in the Path argument

Skip,
[sub]
[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue][/sub]
 
Thank you very much SkipVought, you mentioned the chartobject. Sometimes I need some small ideas to push me in the right (or at least a good) direction.

A chartobject dos not nescessary need to contain a chart. So I tried to make one, without anything in it. Then within this chart I pasted my picture, exported the chart and solved the problem.

For you or anybody else who might have a need for this, here is my sample code (works excellent in Excel 2000).

Code:
Sub ExtractImage()

Dim chrt As ChartObject
'getting width and height of picture so the chart can be sized correctly
'if the chart has an other size the picture will be scaled to fit inside the chart area
W = ActiveSheet.Shapes("Picture 1").Width
H = ActiveSheet.Shapes("Picture 1").Height
Set chrt = ActiveSheet.ChartObjects.Add(0, 0, W, H)
ActiveSheet.Shapes("Picture 1").Select
Selection.Copy
chrt.Border.LineStyle = 0 'no border around chart (and picture)
chrt.Select
ActiveChart.Paste
chrt.Chart.Export "c:\picture1.gif", "gif"
chrt.Delete
   
End Sub



---------------------------
There are only 10 types of people in the world. Those who know binary numbers and those who don't
 


Sverre,

A very innovative, solution! I learned something!

==> * :)

Skip,
[sub]
[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue][/sub]
 
It is an other trick using the chart export method:
Code:
Sub SavePictureFromSheet(ShName As String, PictPath As String)
Dim S As Object
 With Charts.Add
    .SetSourceData Source:=Worksheets(ShName).Range("A1")
     Sheets(ShName).Shapes("Picture 1").Copy
    .Paste
    .Export Filename:=PictPath, FilterName:="GIF"
 End With
 Application.DisplayAlerts = False
  For Each S In ActiveWorkbook.Sheets
    If S.Type = 3 Then S.Delete
  Next S
 Application.DisplayAlerts = True
End Sub
Sub Tests()
 SavePictureFromSheet "Test", "c:\test.gif"
End Sub
You need o file having a sheet "Test" with Picture 1 on it.
The problem is the size of the saved file. Maybe somebody will find a way to fit the dimensions...

Fane Duru
 
Sorry...
I did not see the last post of Swerre where everything was solved in this way.

Fane Duru
 
Another way to 'export' pictures from a workbook is to save it as an HTML file.

Of course, that saves all of them, whereas Sverre's code allows just the one picture (or a group of them) to be saved. For pictures that aren't suitable for saving in gif format, you could use png or jpeg.

Cheers
 

Well done Sverre [smile]

Star from me too.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Last update - I have moved the code to a VB 6 app. Working on it to use FilesystemObjects to get all the Excel files one by one from a selected folder. Pulling out all nescessary informatione from the sheets - adding it to new records in my DB table using ADO objects. Placing all the pictures in a folder using an ID from the DB as the name for the file. Later the db and the picture folder will be used in a asp-based solution on a website.



---------------------------
There are only 10 types of people in the world. Those who know binary numbers and those who don't
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top