Print a userform in landscape

TheBeefMan

New Member
Joined
Sep 20, 2007
Messages
7
Please can anyone help me print a userform automatically in landscape mode? I just can't seem to get the code right. I would appreciate any leads.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Assuming your userform is named "UserForm1", you might be able to get away with this simple one-liner to print the image of that userform:

UserForm1.PrintForm

In that example, the userform does not need to be open or active.



If you truly want to make sure the userform's image is printed in landscape, here is one way, which will involve said userform being open.

In a fresh new standard module such as the kind you put macros and UDFs in, paste in this code:


Code:
Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const VK_SNAPSHOT = &H2C


In the userform of interest's module (again, assuming it is named UserForm1 because you did not say), paste in this code which would be triggered when you click a command button named "CommandButton1" that you have placed on that userform:

Code:
Private Sub CommandButton1_Click()
   Application.ScreenUpdating = False
   keybd_event VK_SNAPSHOT, 1, 0, 0
   Workbooks.Add 1
   ActiveSheet.Range("A1").Select
   Application.Wait Now + TimeValue("00:00:01")
   ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
   With ActiveSheet
      .PageSetup.Orientation = xlLandscape
      .PageSetup.LeftHeader = "Userform1"
      .PrintOut
   End With
   ActiveWorkbook.Close False
   Unload Me
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
I can actually contribute something!!!

To print the userform centered, both horizontally and vertically, add the following:

.PageSetup.CenterHorizontally = True
.PageSetup.CenterVertically = True


Wow, I actually contributed something instead of always taking. Feels good!
 
Upvote 0
Assuming your userform is named "UserForm1", you might be able to get away with this simple one-liner to print the image of that userform:

UserForm1.PrintForm

In that example, the userform does not need to be open or active.



If you truly want to make sure the userform's image is printed in landscape, here is one way, which will involve said userform being open.

In a fresh new standard module such as the kind you put macros and UDFs in, paste in this code:


Code:
Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const VK_SNAPSHOT = &H2C


In the userform of interest's module (again, assuming it is named UserForm1 because you did not say), paste in this code which would be triggered when you click a command button named "CommandButton1" that you have placed on that userform:

Code:
Private Sub CommandButton1_Click()
   Application.ScreenUpdating = False
   keybd_event VK_SNAPSHOT, 1, 0, 0
   Workbooks.Add 1
   ActiveSheet.Range("A1").Select
   Application.Wait Now + TimeValue("00:00:01")
   ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, DisplayAsIcon:=False
   With ActiveSheet
      .PageSetup.Orientation = xlLandscape
      .PageSetup.LeftHeader = "Userform1"
      .PrintOut
   End With
   ActiveWorkbook.Close False
   Unload Me
   Application.ScreenUpdating = True
End Sub
Hi Tom. I tried inserting this code in my vba. I set up a new module (module2) to insert the function code and then in the userform1 code I inserted the rest of your code. I launched my application and with a control button called "print" I tried your code. Unfortunately the program gives an error at the pastespecial line in the main code. Its a 1004 error specifying that the "pastespecial method of worksheet class failed". I set up a new vba code program with only your code in it and it still returns same error. any suggestions?
 
Upvote 0
Hi Tom. I tried inserting this code in my vba. I set up a new module (module2) to insert the function code and then in the userform1 code I inserted the rest of your code. I launched my application and with a control button called "print" I tried your code. Unfortunately the program gives an error at the pastespecial line in the main code. Its a 1004 error specifying that the "pastespecial method of worksheet class failed". I set up a new vba code program with only your code in it and it still returns same error. any suggestions?
I am only guessing, but the 1004 for that particular line might be due to more time needed than 1 second for the new workbook to be added, which in turn depends on how fast your system processes the programming code such as its ram and processor capacities. Just a hunch but instead of
Application.Wait Now + TimeValue("00:00:01")
try
Application.Wait Now + TimeValue("00:00:05")
to see if 5 seconds is enough execution time, which it should be.

If no success, try instead of bitmap png or jpeg.

If still no success, never having seen your project so just in case there is other intervening or competing methods happening elsewhere, try immediately after
Application.ScreenUpdating = False
putting
Application.EnableEvents = False

and immediately before
Application.ScreenUpdating = True
put
Application.EnableEvents = True

These are the options that come to mind, given the type of error you saw, and the offending line of code.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top