Print Screen? (SOLVED)

dowingc

Board Regular
Joined
Oct 27, 2002
Messages
77
I have a userform that I would like to save as a jpg or bmp. Do you know of code to do a print screen and save as jpg or bmp? Basically, I want to be able to view this userform over the web as a web page. Thanks!

Chris
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If you want a snapshot of just the screen, Printkey 2000 is a free program that will do the job nicely.
 
Upvote 0
Depending on what you are trying to do exactly, you might have a couple options.

One is by pressing down the Shift key and clicking on the Edit menu. A special option will appear named "Copy Picture". When I selected a few cells and ran the macro recorder, here's what I got:

Range("A1:I27").CopyPicture Appearance:=xlScreen, Format:=xlPicture

Another possible option is with an API keyboard event. Saw this on Google once..unfortunately, author unknown. Insert a new VBA module and paste this in...worked great when tested:


Option Explicit

Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Public Const VK_SNAPSHOT = &H2C

Sub PrintScreen()
keybd_event VK_SNAPSHOT, 0, 0, 0
End Sub
 
Upvote 0
Print Screen

Isn't it much simpler than all the above!
Just press print screen (PrtScr)
Then open a graphics program (Paint will do)
and paste (as a new image)
 
Upvote 0
Lewis, yes that is easy and obvious but not what was asked. What was asked was VBA code to do automatically what you just described manually. Try recording a macro while doing the steps you just gave and see what code you end up with. There will be absolutely nothing in the Sub.
 
Upvote 0
I hate to ask this but I am almost stuck again. The module portion works great. The current screen shot is copied to the clipboard. I just can't figure out how to get the clipboard contents to be copied to "screenshot.jpg or bmp". If you can help me, I would appreciate it! Remember, in VBA code please :)

Chris
 
Upvote 0
Well, first let me apologize for only answering your question half-way. Putting the screen shot on the clipboard was as far as I read. Sorry.

I hope I'm wrong, and if so that someone corrects me. I believe that the screen shot image needs to be saved into a program for that purpose, such as (but not exclusively) Paint or Photo Editor, in order to be saved as a jpg or bmp or gif.

Now, if instead of a screen shot, you wanted to copy a chart or some other Shape object, it would be much easier. But at the point of being on the clipboard, a screen shot is in limbo and so I believe it would need an object model that can be accessed through VBA. I looked through the Library just now and did not see an available reference for Paint or Editor. For applications that do have an object model, it should work, but I could not locate one.

There are alternatives but you might not like them. One is to paste the image into a Word document and save as html. But the image would not be as sharp or clear as a jpg.

I know you said you want VBA so I won't try to sway you away from that. If anyone knows of a way to do it, I'm sure they will chime in. And again, I hope I am corrected about the hurdle(s) necessary to accomplish what you want...I just don't know.

In case anyone else is curious about a non-VBA approach, in addition to Mr. Phantom's suggestion, I found a nice downloadable product for screen shots and the like at www.hyperionics.com
 
Upvote 0
I modified this code a while ago from Ivan... didn't even dream of putting my name there because I added very simple things !!

It uses the PastePicture method that you can find at Stephen Bullen's site (www.bmsltd.ie), it IS necessary to keep things in VBA entirely.

This code was used to take a screenshot of the Userform when it was clicked, but can be modified to do what you want...

Also, it saves the image as a BMP, got the best resolution using that one, but it can save as JPG too.

Option Explicit

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Private Const WM_CLOSE = &H10
Private Const VK_MENU = &H12
Private Const VK_SNAPSHOT = &H2C
Private Const KEYEVENTF_KEYUP = &H2

Public Sub SaveImageActiveWindow()
'////////////////////////////////
'// Ivan F Moala //
'// http://www.xcelfiles.com //
'// For my friend Colo //
'////////////////////////////////

Dim Altscan As Double
On Error GoTo ErrHandler

'// Copy picture into the clip board.
Altscan = MapVirtualKey(VK_MENU, 0)
keybd_event VK_MENU, Altscan, 0, 0

keybd_event VK_SNAPSHOT, 0, 0, 0
keybd_event VK_MENU, Altscan, KEYEVENTF_KEYUP, 0

Application.Wait Now + TimeSerial(0, 0, 1)
SavePicture PastePicture(xlBitmap), "C:\Test.bmp"
Exit Sub

ErrHandler:
'// Error handling
MsgBox Err.Number & ":= " & Err.Description

End Sub

Private Sub UserForm_Click()
SaveImageActiveWindow
MsgBox "Done !"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top