PDA

View Full Version : PowerPoint VBA Not Outputting to Correct Size



nullpointer
07-17-2016, 05:54 PM
I use the following VBA in PowerPoint to save a slide/image to a png.

Sub png_600x400()
Dim opic As Shape
Dim sngW_Rat As Single
Dim sngH_Rat As Single
Dim Path As String
Const pix_W As Long = 600
Const pix_H As Long = 400
Set opic = ActiveWindow.Selection.ShapeRange(1)
sngW_Rat = ActivePresentation.PageSetup.SlideWidth / opic.Width * pix_W
sngH_Rat = ActivePresentation.PageSetup.SlideHeight / opic.Height * pix_H
'Old path
'Path = Environ("USERPROFILE") & "\Desktop\Temp\Transparent_Test_Image.png"
'New Path
Path = "C:\Temp\Transparent_Test_Image.png"
Call opic.Export(Path, ppShapeFormatPNG, sngW_Rat, sngH_Rat)
End Sub


The problem is the output for this code ends up being 802 x 535 which means I then need to open it in a graphics program and resize it to 600 x 400.
Is there a way to "persuade" the above code to output the right 600 x 400 size?
I appreciate any and all help!

Paul_Hossler
07-17-2016, 06:48 PM
Been a long time, and this is over simplified. Hopefully some of the more technical members will way in


Shape width = 600 points, with 72 points to the inch, or 8.33 inches

Screen resolution of 96 dots per inch (dpi) = 8.33 x 96 = 800 pixels

The 96 dpi varies with screen resolution also




Const pix_W As Long = 600 * (72/96)
Const pix_H As Long = 400 * (72/96)



So this might be the easiest workaround

nullpointer
07-17-2016, 06:58 PM
Paul,

Thanks for weighing in I really do appreciate it.

When added your code the resulting size was 601 x 401 - SO CLOSE!!!!

I replaced

Const pix_W As Long = 600
Const pix_H As Long = 400

with

Const pix_W As Long = 600 * (72/96)
Const pix_H As Long = 400 * (72/96)

John Wilson
07-18-2016, 01:16 AM
The original code is actually correct BUT MSFT broke it in 2016 version (and maybe in 2013 365 too)

If you have 2016 you can test for the bug by copying a shape and paste special as a WMF. It should be the same size but is probably about 125% larger. If you are adjusting your code to allow for this be aware that they are aware of the bug and MIGHT fix it so it could then fail again.

Try adjusting the width and height required by a factor of .7478 (nearly 72/96 but not quite) and use the nearest whole number. So for 600 x 400 try 449 x 299

nullpointer
07-18-2016, 07:40 AM
John,

I appreciate you taking the time to explain this - makes total sense.

I'll do some testing.