问:如何以横向格式打印 EXCEL / VBA 用户表单? [关闭]
Posted
技术标签:
【中文标题】问:如何以横向格式打印 EXCEL / VBA 用户表单? [关闭]【英文标题】:Q: How can I print an EXCEL / VBA Userform in landscape format? [closed] 【发布时间】:2020-05-24 16:00:02 【问题描述】:我已经创建了一个使用 VBA 的 Excel 文件的编年史,并使用 Userforms 来显示内容。通过用户表单中的按钮,我可以使用 Me.PrintForm 打印活动用户表单。结果是纵向格式的页面,但大约三分之一的用户表单没有打印。如何实现横向打印?硬件
【问题讨论】:
你能展示你的文件和用户表单的图片吗? 【参考方案1】:我找不到任何方法,最好的方法是使用截图工具(或键盘上的打印屏幕按钮)。
https://www.google.com/search?q=snipping+tool
【讨论】:
【参考方案2】:这只能使用技巧来完成。我的意思是,打印一张只有表格的空纸。我不是下一个代码的创建者。前段时间,我在互联网上找到了它,使用它并保存在我有趣的代码片段中。
复制表单模块顶部的下一行(在声明区域中):
Option Explicit
Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr))
Const VK_SNAPSHOT = 44
Const VK_LMENU = 164
Const KEYEVENTF_KEYUP = 2
Const KEYEVENTF_EXTENDEDKEY = 1
在表单模块中创建下一个子:
Private Sub PrintForm()
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
'added to force landscape
ActiveSheet.PageSetup.Orientation = xlLandscape
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 300
.CenterHorizontally = True
.CenterVertically = True
.Orientation = xlLandscape
.PaperSize = xlPaperA4
.Order = xlDownThenOver
.BlackAndWhite = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1
ActiveWorkbook.Close False
End Sub
从表单按钮调用上述Sub
。它将在您的默认打印机上打印...
Sub ButtonX_Click()
PrintForm
End Sub
【讨论】:
以上是关于问:如何以横向格式打印 EXCEL / VBA 用户表单? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章