显示'文件 - >打印'菜单通过VBA

时间:2014-11-24 11:51:44

标签: excel-vba printing vba excel

使用VBA for Excel(2010)我可以同时调用打印对话框和打印预览,但我无法弄清楚如何调用文件 - >打印菜单。

Application.Dialogs(xlDialogPrint).Show ' Client - "not in keeping with the Excel 2010 experience"
ActiveSheet.PrintPreview (False)        ' Very slow to display

是否可以使用VBA显示此菜单选项?

1 个答案:

答案 0 :(得分:2)

这是你在尝试什么?

Sendkeys不可靠,因此在使用它们时必须非常小心。

确保从Developer Tab |中调用它宏而不是直接来自VBA编辑器。否则,您必须使用API​​将Excel窗口置于最前面,然后使用sendkeys。

Sub Sample()
    SendKeys "%fp"
End Sub

以下是从VBE调用它的示例

Private Declare Function SetForegroundWindow _
Lib "user32.dll" (ByVal hWnd As Long) As Long

Sub Sample()
    Dim CBC As CommandBarControl

    '~~> Bring the Excel window to the front.
    '~~> I am assuming that there is only one excel instance
    '~~> If there are more then you will have to use Findwindow,
    '~~> FindwindowEx API
    SetForegroundWindow ActiveWorkbook.Application.hWnd

    '~~> Closing the VBE
    On Error Resume Next
    Set CBC = Application.VBE.CommandBars.FindControl(ID:=752)
    On Error GoTo 0

    If Not CBC Is Nothing Then
        CBC.Execute
        DoEvents

        '~~> File --> Print
        SendKeys "%fp"
    End If
End Sub