Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

How to print a screenshot of a form via code

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
Milewskp
Asked:
Milewskp
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:

 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
 
Randy DownsOWNERCommented:
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
 
Randy DownsOWNERCommented:
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!

 
Randy DownsOWNERCommented:
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
 
Jeffrey CoachmanCommented:
<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
 
Jeffrey CoachmanCommented:
.....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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
MilewskpAuthor Commented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<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
 
MilewskpAuthor Commented:
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
 
Jeffrey CoachmanCommented:
...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
 
MilewskpAuthor Commented:
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
 
Jeffrey CoachmanCommented:
k
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now