|
改进一下:
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_MENU = &H12 'Alt键
Const VK_SNAPSHOT = &H2C 'PrintScrn键
Const KEYEVENTF_KEYUP = &H2
Private Sub CommandButton1_Click()
On Error Resume Next
Dim alt_scan_code As Long, shp
' 模拟键盘按键Alt+PrtSc,复制当前窗口位图到剪贴板。
alt_scan_code = MapVirtualKey(VK_MENU, 0)
keybd_event VK_MENU, alt_scan_code, 0, 0 ' 模拟按Alt键
keybd_event VK_SNAPSHOT, 0, 0, 0 ' 模拟按PrintScrn键
DoEvents ' 转让控制权,以便让操作系统处理其它的事件。
keybd_event VK_MENU, alt_scan_code, KEYEVENTF_KEYUP, 0 '释放按键
' 粘贴剪贴板里图片到工作表,调整成横向打印后打印预览
With Workbooks.Add(xlWBATWorksheet).ActiveSheet
.Paste ' 粘贴图片到工作表
.PageSetup.Orientation = xlLandscape
.PageSetup.LeftMargin = Application.InchesToPoints(0)
.PageSetup.RightMargin = Application.InchesToPoints(0)
Application.PrintCommunication = True
For Each shp In .Shapes
shp.ScaleWidth 0.88, msoFalse, msoScaleFromTopLeft
shp.ScaleHeight 0.9, msoFalse, msoScaleFromTopLeft
shp.IncrementTop 78
Next
Unload Me ' 关闭当前窗口,如果是非模态窗口的话也许不用关闭.
.PrintPreview ' 预览贴了图片的工作表
End With
End Sub |
|