VBA: using SendKeys to print the screen

JaredSorensen

Board Regular
Joined
Aug 23, 2007
Messages
160
I would like to emulate hitting the Alt+Ctrl+"Print Screen" button on the keyboard with the following code. I know there are some restrictions around using the PrintScreen with the SendKeys, but I'm hoping I can still figure out how to make this work.

Following is the first part of a larger macro I've set up. If I can just get this to work, that would be perfect.

Code:
Sub PrintTheScreen()
Application.SendKeys "^%" & "PRTSC"
End Sub

Thanks.

Jared
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Jared

What is it you are actually trying to achieve?

Using SendKeys for anything is notoriously flaky.

Perhaps there's another method you could use.
 
Upvote 0
Specifically, I need code to copy the image on the screen, as you could manually do with "Ctrl+PrintScreen" for the whole screen or Ctrl+Alt+PrintScreen for the smaller view (in case there is a dialog box up, it only copies the image of the dialog box).

Thanks, Norie.

Jared
 
Upvote 0
Worked like a charm. Thanks VOG II!

That's why I love posting to this forum. I would have never known that. Thank you.

Jared
 
Upvote 0
One more question. When I plugged your code into my complete macro (which is supposed to function as a way to automatically print the screen your viewing, screen size calibrated to my 22'' monitor) the ActiveSheet.paste line errors out. Do you know if there is something about the DoEvents line that could cause a problem? I've never used that before and so am unfamiliar with the nuances of it.

The code worked fine before adding the ApplicationSendKeys line, it simply required that I do the Ctrl+Alt+PrtScr before executing it. Thanks.

Code:
Sub Print_Screen()
' Macro recorded 3/6/2008 by Jared Sorensen
Application.SendKeys "(%{1068})"      'SendKeys contributed from VOG II on mrexcel.com
DoEvents

Application.ScreenUpdating = False
    Sheets.Add
    ActiveSheet.Paste
    
    Columns("AA:AA").ColumnWidth = 2.86
    Rows("61:61").RowHeight = 7.5
    
    ActiveSheet.PageSetup.PrintArea = "$A$1:$AA$61"
    
    With ActiveSheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.25)
        .RightMargin = Application.InchesToPoints(0.25)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.25)
        .CenterHorizontally = True
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
   
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
          'Turn off messages
        Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
        Application.DisplayAlerts = True
Application.ScreenUpdating = True  'to turn back ON (at end of macro)

End Sub

Jared
 
Upvote 0
I don't know why DoEvents would have that effect but I just retested my code without DoEvents and it still works so try taking it out.
 
Upvote 0
Jared

I know Vog seems to have given a solution but I've got to ask why you are doing this in the first place.

Am I missing something?:confused:
 
Upvote 0
Because my PrintScreen key doesn't actually *print* the screen, it just copies the image to the clipboard. At home I have a computer program that actually *prints* when I hit printscreen, but I can't load that program here.

This code is intended to simulate that functionality in Excel. To do the entire process manually takes about 30-40 sec, and I'd rather have the code do it in 3 sec.

Jared
 
Upvote 0
Jared

But why do you want to print the screen in the first place?

I really must be missing something.:confused:
 
Upvote 0

Forum statistics

Threads
1,216,477
Messages
6,130,862
Members
449,600
Latest member
inborntarmac

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