-1

I have an image object on a userform. I want to save the picture from that image object into a file. I see many examples of how to load a picture into an image object, but none the other way around.

I tried stdole.SavePicture obj.Picture, strFilePath, but that only works for button objects.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Chuck
  • 203
  • 6
  • 16

3 Answers3

2

First create a Chart. Second place the picture in the Chart. Finally export the Chart.

EDIT#1

For sample code see:

Save Picture with VBA

Community
  • 1
  • 1
Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • @Chuck See my **EDIT#1** – Gary's Student Aug 10 '15 at 16:20
  • Sorry, I'm using WORD, not Excel. And the image object is on a form, not on the document (or on an Excel worksheet). – Chuck Aug 10 '15 at 16:38
  • Yes, yours would work for Excel, because it has the chart object that happens to do what I need. But Word doesn't have that. I've been searching for an answer, but a lot of Excel answers keep coming up. Thanks for your help! – Chuck Aug 11 '15 at 12:42
1

I don't know if this will help, but I tried casting the Picture object to an IPictureDisp object to pass it to the stdoleSavePicture function, and it worked for me.

Here's the code:

Dim pic As IPictureDisp

Set pic = myForm.Image1.Picture

stdole.SavePicture pic, "C:\myfile.jpg"
orange21
  • 81
  • 1
  • 1
0

I enlist PowerPoint to do this. Here's a macro from a recent project that exported a map of the world:

Public Sub ExportMap()
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim pptShapeRange As PowerPoint.ShapeRange
Dim Path$, File$
Dim oRange As Range

  Application.ScreenUpdating = False
  myDate$ = Format(Date, "m-d-yyyy")
  Set pptApp = CreateObject("PowerPoint.Application")
  Path$ = ActiveDocument.Path & Application.PathSeparator
  File$ = "WorldMap " & myDate$ & ".png"
  Set pptPres = pptApp.Presentations.Add(msoFalse)
  
  Set oRange = ActiveDocument.Bookmarks("WholeMap").Range
  oRange.CopyAsPicture
  
  Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
  On Error Resume Next
  With pptPres.PageSetup
    .SlideSize = 7
    .SlideWidth = 1150
    .SlideHeight = 590
  End With
  Set pptShapeRange = pptSlide.Shapes.PasteSpecial(ppPasteEnhancedMetafile, Link:=msoFalse)
  
  pptSlide.Export Path$ & File$, "PNG"
  
  pptApp.Quit
  
  Set pptPres = Nothing
  Set pptApp = Nothing
  Set pptSlide = Nothing
  Application.ScreenUpdating = True
  MsgBox "All done! Check the folder containing this template for a file called '" & File$ & "'."
End Sub
John Korchok
  • 4,723
  • 2
  • 11
  • 20