0

我正在尝试组装一个宏来弹出一个对话框来选择您的打印机,然后打印具有特定属性的特定命名范围。我从一个可以正常工作的小测试语句开始。

Sub test()
' test Macro

    Application.Dialogs(xlDialogPrinterSetup).Show
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False

End Sub

然后我搬进了更大的枪,我无法让它正常工作。我将 包含MsgBox在宏中以查看是否有任何项目正在触发。

没有错误,没有MsgBox弹出窗口。有什么想法吗?

编辑这是我的新代码和当前代码。它工作正常,一旦我取消对 printrowtiles 的注释,它就会中断。这是它直接从录制宏中给出的代码。

Sub Print_20_Year()
'
' Print_20_Year Macro
'
'
'
'    Range("Print_20_Year").Select
    MsgBox "Step .4"
    Application.Dialogs(xlDialogPrinterSetup).Show
    MsgBox "Step .6"
    Application.PrintCommunication = False
   MsgBox "Step .7"
'    With ActiveSheet.PageSetup
'        .PrintTitleRows = "$4:$13"
    MsgBox "Step .8"
'        .PrintTitleColumns = ""
'    End With
    MsgBox "Step .9"
    Application.PrintCommunication = True
    MsgBox "Step 1"
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperTabloid
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = False
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
    MsgBox "Step 2"
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    MsgBox "Step 3"
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub
4

2 回答 2

0

如果您没有收到任何 MsgBox 弹出窗口,那么您甚至无法通过前几行。我的怀疑是一开始就出了问题:

Application.Dialogs(xlDialogPrinterSetup).Show
    ActiveSheet.PageSetup.PrintArea = "Print_20_Year"
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$4:$13"
        .PrintTitleColumns = ""    <<<< this worries me. What are you trying to do?
    End With
    MsgBox "Step 1"     <<<<< if you don't get here, one of the lines before this causes a silent error...

我建议On Error GoTo ErrorHandler在您的 sub 中添加一条语句作为第一行;然后添加(通常在子结束之前)

ErrorHandler:
    MsgBox "Oops - an error occurred. " & Err.Description

这可能需要您进行更多调查才能解决...

于 2013-02-13T03:53:33.370 回答
0

经过一番摸索,我意识到不知何故它printtitlerows正在与我在下面写的一个单独的 UDF 交互:

Function IsFormula(c)
IsFormula = c.HasFormula
End Function

此 UDF 与一种条件格式相关联,该格式突出显示其中包含公式的单元格。我们使用它来轻松识别我们必须手动更改/输入的信息。我无法解释为什么 UDF 会造成问题,但是当我删除它时,我的宏运行良好。再次添加我的公式宏后,我只是在代码的开头和结尾添加了Application.ScreenUpdating = False和。Application.ScreenUpdating = True这解决了这个问题。

于 2013-02-20T15:03:26.617 回答