Solved

How to print a screenshot of a form via code

Posted on 2011-09-16
13
739 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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