VBA Macro image stored in Clipboard output as an *.png?

ael005
NewLounger
Posts: 1
Joined: 04 Feb 2022, 23:14

VBA Macro image stored in Clipboard output as an *.png?

Post by ael005 »

Hi all,

I am trying to write a VBA Macro, where the code stores an image stored in Clipboard and I need to output it as a *.png?

Will this code allow me to do that in a program that does not have a command function to output this image? And, if so, is it restricted to only run in some applications?

Code: Select all

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

Set pptApp = CreateObject("PowerPoint.Application")
Path$ = ActiveDocument.Path & Application.PathSeparator
File$ = "Graphic " & myDate$ & ".png"
Set pptPres = pptApp.Presentations.Add(msoFalse)

Set oRange = Selection.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
MsgBox "All done! Check the folder containing this document for a file called '" & File$ & "'."
End Sub
Also, besides adding the reference library for PowerPoint, what else do I need to add to make this code run?

Thank you for your time in this matter.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA Macro image stored in Clipboard output as an *.png?

Post by HansV »

Welcome to Eileen's Lounge!

The line

Code: Select all

Path$ = ActiveDocument.Path & Application.PathSeparator
suggests that this code is to be run from Word. Is that correct?
Best wishes,
Hans