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.
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
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?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
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 ofHi 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?