Solved

How to print a screenshot of a form via code

Posted on 2011-09-16
13
757 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 58

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 30

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 30

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 30

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 58
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 58
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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