Visual Basic for Applications (VBA)


Printing Userform


Hello
I have created a userform that takes data from several different sheets in a document and displays charts depending on certain inputs. I want to be able to print the whole form, but
A) I had to use a scroll bar to get everything I wanted onto the form and it only displays that visible portion when printing
B) The form is too wide, so using
userform.Printform
cuts off a significant portion.
Is there a better way to make this happen? I considered trying to export it somehow and print the image, but I don't know how to make that work or if it's even possible. I only started learning VBA about two weeks ago and I have very limited coding experience
even beyond that.
Thanks for any help you're able to provide.
From Tom Ogilvy, Excel MVP:
In a general VBA module:
'PtrSafe for 64 bit Windows
Declare PtrSafe 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 = 44
Public Const VK_LMENU = 164
Public Const KEYEVENTF_KEYUP = 2
Public Const KEYEVENTF_EXTENDEDKEY = 1
Sub Test()
UserForm1.Show
End Sub
'In the userform's codemodule:
Private Sub CommandButton1_Click()
' keybd_event VK_SNAPSHOT, 0, 0, 0
DoEvents
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + _
KEYEVENTF_KEYUP, 0
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + _
KEYEVENTF_KEYUP, 0
DoEvents
Workbooks.Add
Application.Wait Now + TimeValue("00:00:01")
ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, _
DisplayAsIcon:=False
ActiveSheet.Range("A1").Select
With ActiveSheet.PageSetup
.Orientation = xlPortrait
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1
ActiveWorkbook.Close False
End Sub
From Tom Ogilvy, Excel MVP:
In a general VBA module:
'PtrSafe for 64 bit Windows
Declare PtrSafe 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 = 44
Public Const VK_LMENU = 164
Public Const KEYEVENTF_KEYUP = 2
Public Const KEYEVENTF_EXTENDEDKEY = 1
Sub Test()
UserForm1.Show
End Sub
'In the userform's codemodule:
Private Sub CommandButton1_Click()
' keybd_event VK_SNAPSHOT, 0, 0, 0
DoEvents
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY + _
KEYEVENTF_KEYUP, 0
keybd_event VK_LMENU, 0, KEYEVENTF_EXTENDEDKEY + _
KEYEVENTF_KEYUP, 0
DoEvents
Workbooks.Add
Application.Wait Now + TimeValue("00:00:01")
ActiveSheet.PasteSpecial Format:="Bitmap", Link:=False, _
DisplayAsIcon:=False
ActiveSheet.Range("A1").Select
With ActiveSheet.PageSetup
.Orientation = xlPortrait
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1
ActiveWorkbook.Close False
End Sub
I'm sorry to bother you, but could you explain to me what this does exactly? As far as I can tell it puts the entire visible window of the userform into the clipboard, pastes it to a sheet, and then formats the printing from there. Is that in any way correct?
And how does it copy the userform to the clipboard?
You are correct - the code takes a snapshot of the userform and pastes it into a workbook, which allow the print-to-fit option to be used (which is not available with userforms). How exactly it does it is - magic ;-) Tom Ogilvy is a really smart
guy - he uses the system library files to control the useform's window and what gets copied to the clipboard.
Great, Thanks a lot!

Related Links

Macro to read GUI Application
How to fill in blank cells with correct data without overwriting the data above it?
rch for date and return other fields
How to paste clipboard content to a folder (as a file)
Copy Outlook Message body content into MS Excel
extract a CAB file with VBA
OMG my project has crashed
Assigning Range of Cells to Array
Create Code to Update Value in Record From Previous Data Record
MSComm -Having problem receiving data from modem
Exporting Microsoft Word Review Comments to Microsoft Excel - Error 91 ??
error 1004 application defined or object defined error
Change File Names Using SQL Query to get new UID
Listbox question (prevent refresh)
Listview.listitem.key Intermitent Failure
varEntryIDs = Split(EntryIDCollection, ",")

Categories

MSDN
Visual Studio Design a...
Commerce Server
Data Platform Development
Smart Device Development
Windows Server
Feedback and Discussions
Windows Hardware Devel...
Visio 2010
OneDrive Development
Internet of Things
Visual C#
MSBuild
Application Insights (AI)
Submitting and Publish...
Dev Center App
General Windows Deskto...
Windows Desktop Perfmo...
Development with the W...
SharePoint 2010 - Gene...
Workflow Manager 1.0
Cloud Services (Web an...
Azure Management Portal
Azure Media Services
Azure Marketplace
Azure DocumentDB
Azure Data Factory
Azure Stream Analytics
Azure Logic Apps
Azure Service Fabric
Azure DevTest Labs
Azure IoT Hub
Azure Functions
.NET Framework Class L...
Team Foundation Server...
Team Foundation Server...
BizTalk Server General
Internet Explorer Web ...
Microsoft Translator D...
ISV Open Discussions
Microsoft Robotics - S...
Data Mining
SQL Server Analysis Se...
SQL Server Tools
.NET Framework inside ...
SQL Server Security
SQL Server Documentation
SQL Server Spatial
Microsoft StreamInsight
SQL Server Migration
Power Pivot
Standard 2009: Tools
SQL Server 2012 Master...
Server Virtualization
Live Connect (Archived)
Windows Live Messenger...
WebsitePanel - Support
SharePoint Server Prot...
Office Protocols
Project Customization ...
Microsoft Atlas APIs: ...
Getting Started with S...
Programming Silverligh...
MVVM / ViewModel Patte...
Reference Source Serve...
Surface Application De...
Retired SharePoint Wor...
Retired SharePoint Soc...
.NET Framework 4 Appli...
.NET Framework 4: Wind...
Building Windows Store...
Building Windows Store...
Core Coding Experience...
General Troubleshootin...
Innovate on Office
LINQ to SQL (Pre-Release)
Microsoft Security Dev...
Microsoft Unified Comm...
POS for .NET
Search Server: Develop...
Search Server: Feature...
Search Server: Federation
Search, Organize, and ...
Server Core Developers
SQL Server Modeling
SQL Server 2014 Databa...
SQL Server 2014 Analys...
Team Foundation Server...
Team Foundation Server...
Visual Basic 2008 (Pre...
Visual Basic for Appli...
Visual Basic Interop a...
Visual C# IDE
Visual Studio Async CTP
Visual Studio Applicat...
Visual Studio Code Ana...
Visual Studio Editor i...
Visual Studio Online "...
Web Development in Vis...
Web Sandbox - Exploits
Web Sandbox - General
Windows Live Data Deve...
Building Windows Store...
Mango Update (Archived)
Expression Blend for W...
Ad Revenue, Payout & T...
Windows Containers

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile