Excel 显示';文件->;打印';VBA菜单

Excel 显示';文件->;打印';VBA菜单,excel,printing,vba,Excel,Printing,Vba,使用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显示此菜单选项?这就是您正在尝试的吗 Sen

使用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显示此菜单选项?

这就是您正在尝试的吗

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

确保从“开发人员”选项卡|宏调用它,而不是直接从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

谢谢你,这正是我需要的。你是指你用这个得到的屏幕:
application.commandbar.ExecuteMso“PrintPreviewAndPrint”