Solved

How to print a screenshot of a form via code

Posted on 2011-09-16
13
734 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now