Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 17th, 2005, 01:32 AM
ct ct is offline
Authorized User
 
Join Date: Aug 2005
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default print or printview a form

i want to create a button on which the button will print preview a form in excel...

how i want to write a coding to print preview a form in excel..
that's all.tq


 
Old August 17th, 2005, 03:20 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As such I dont know of a way to print preview a form, however, if you need to print the form use...

    UserForm1.PrintForm

sorry,

Matt

 
Old August 18th, 2005, 11:37 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Had some time to think and research this, to print preview the form you must use a workaround.

In the following example I have a form called "Userform1", on the form are 2 objects, 1) a check box called "CheckBox1" and 2) a button called "CommandButton1".
I also have a module called "module1".
In module 1 I have the following code:
    Sub mdl_ShowForm()
        UserForm1.Show (vbmodless)
    End Sub


in the code behind the form I have the following:

 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

 Private Sub CheckBox1_Click()
    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

 Private Sub CommandButton1_Click()
    Range("A1").Select
    ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
    ActiveWindow.SelectedSheets.PrintPreview
    Selection.Delete
 End Sub


Now one thing to stress is, DO NOT RUN A MODAL FORM AND THE PRINT PREVIEW AT THE SAME TIME, EXCEL WILL HANG UP.
The code must always be started by executing the mdl_ShowForm code first. This will define the form as modeless and hence allow print preview without hanging.

What is the process doing?
Well when the checkbox is selected, the process is doing the equivilent of the [Alt]+[Print Scrn] buttons on the keyboard, i.e. copy to clipboard only the active windows. When the user clicks the button on the form it pastes the contents of the clipboard into excel and print previews the sheet. When the user clicks close in the print preview, the image of the form is deleted.

hope this is helpful.

Cheers

Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
How centre a form for printin using Print Form VBProg VB.NET 2002/2003 Basics 0 October 23rd, 2006 03:12 PM
print a VB form Jee Beginning VB 6 0 December 13th, 2005 05:34 AM
Cant print when the form become subform yikchin Access 6 November 22nd, 2005 10:22 PM
need to print windows form GAF .NET Framework 1.x 0 November 17th, 2005 10:23 AM
print form damnnono_86 Access 2 November 16th, 2003 09:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.