Solved

How to print a screenshot of a form via code

Posted on 2011-09-16
13
745 Views
Last Modified: 2013-11-28
How can I print a screen shot of the current form, in landscape mode, on 8.5x11 paper,  with 0.2" margins, all automatically by clicking  a command button on the form?
0
Comment
Question by:Milewskp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 36549453

 Without some type of capture software like Snag-it or Fullshot, I don't believe you can.

 You can print a form in Access by selecting the record, then doing a Printout Action/Method.

Jim.
0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 36549508
try this

http://word.mvps.org/FAQs/MacrosVBA/PrtSc.htm

How to do a screen capture using VBA

Or: How to invoke the print screen command

Article contributed by Lutz Gentkow

Using SendKeys
Unfortunately, you can't invoke the print screen command directly – SendKeys "{PrtSc}" is not supported in VBA. But as it so frequently does, like a knight in shining armour, WordBasic rides the rescue:

Sub PrintScreen()
    WordBasic.SendKeys "{1068}"
End Sub

(For some strange reason, SendKeys "{1068}" does not work).

The above code works internationally, but if you don't mind your code being language-specific, then for English versions of Word you can use:.

Sub PrintScreen()
    WordBasic.SendKeys "{prtsc}"
End Sub

0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 36549517
Note that print screen would be all windows. Perhaps you could modify to get the ALT Print screen which is the window of focus.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Randy Downs
ID: 36549524
Actually the latter part of the article looks better & provides for the ALT Print screen


Using API calls
Most serious programmers try to avoid using SendKeys if they can, because it has a reputation for unreliability. If you would prefer to avoid using SendKeys, or if you are not using a German version of Word but want to be able to capture the active Window, you can use API calls to capture either the screen or the current window, as follows. The following code sample is a much simplified version of the code posted in Microsoft Knowledge Base article Q240653:

To capture the screen

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 Const VK_SNAPSHOT = &H2C

Sub PrintScreen()
    keybd_event VK_SNAPSHOT, 1, 0, 0
End Sub

To capture the active window

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 Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12

Sub AltPrintScreen()
    keybd_event VK_MENU, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
End Sub
0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 250 total points
ID: 36555374
<How can I print a screen shot of the current form, in landscape mode, on 8.5x11 paper,  with 0.2" margins, all automatically by clicking  a command button on the form?>

Printscreen will not allow you any control over the margins, Paper Size or the Orientation.
All Printscrenn does is take a "SnapShot/Photo" of the *Entire Screen*(as the name implies), not just the form.
(the screenshot will include *everything* on the screen, as if someone literally took a camera and snapped a picture of your monitor (taskbar, Start button, ...etc)
In other words, it has nothing to do with "Printing"

It sound to me like you just want to print the current form record.
Is this correct?...

If so then:
Open the Form, then go into Print Preview
Click "Page Setup"
Set your margins, orientation and paper size here.
Save the form.
Put a button on the form
On the click event of this button, insert code like this:
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.PrintOut acSelection


Also remember that if the form is not "Designed" to print with those specific settings, it may not look the way you want.


JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36555383
.....Jim,

I just now read your post in it's entirety immediately after I made my post above...

Sorry for the dupe post...

My post was basically the same as yours, just with more " splain'in "

So points to you if this is what the OP wanted...

;-)

Jeff
0
 
LVL 57
ID: 36559983
Jeff,

<<.....Jim,

I just now read your post in it's entirety immediately after I made my post above...
>>

  No problem.

<<My post was basically the same as yours, just with more " splain'in "

So points to you if this is what the OP wanted...
>>

  and for that reason, it's a far better answer then mine<g>.  

Jim.
0
 
LVL 1

Author Comment

by:Milewskp
ID: 36561248
Thanks Jim and Jeff,
I found that DoCmd.RunCommand acCmdPrint seems to do the same thing as DoCmd.PrintOut (except that the former gives you a print dialog box, which may be helpful in some situations and an annoyance in others).

One thing that I can't figure out: in my case I have a form with a Tab control. On some of the tabs there is a datasheet-view subform. I find that sometimes  DoCmd.RunCommand acCmdPrint  will print a screen shot of the subform, and in others it will print all of the records of the subform (even the ones that you need to scrol down to see).
0
 
LVL 57
ID: 36561874
<<I find that sometimes  DoCmd.RunCommand acCmdPrint  will print a screen shot of the subform, and in others it will print all of the records of the subform (even the ones that you need to scrol down to see). >>

 I believe it prints records that are currently selected.  Maybe that's why your seeing the difference.

Jim.
0
 
LVL 1

Author Comment

by:Milewskp
ID: 36562970
Hi Jim,
It doesn't seem to depend on that, in fact I haven't been able to figure out any pattern. But, it's not a major problem.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36563137
...This is why Reports are most times a better choice for printing...

What info is in this form that can't be on a report?
0
 
LVL 1

Author Comment

by:Milewskp
ID: 36563169
Hi Jeff,
You're right. I used this quick and dirty approach to get the job done quickly, since the requirement for this report may be temporary; if it isn't, then I'll have to create a reprot.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36563495
k
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question