0

I am trying to assemble a macro to bring up a dialog to choose your printer, then print a specific named range with specific properties. I started with a small test statement that works fine.

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

Then I moved into the bigger guns and I am not able to get it to work properly. I included the MsgBox in the macro to see if any of the items were triggering.

No errors, no MsgBox popup. Any thoughts?

EDIT This is my new and current code. It works fine, buuut as soon as I uncomment the printrowtiles, it breaks. This is code it gives directly from recording a macro.

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

printtitlerows何度も調べた後、私はどういうわけか、以下に示すように、私が書いた別のUDFと相互作用していることに気付きました。

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

このUDFは、数式を含むセルを強調表示する条件付き形式に関連付けられていました。これを使用して、手動で変更/入力する必要のある情報を簡単に認識します。UDFが問題を引き起こした理由を説明することはできませんが、UDFを削除すると、マクロは正常に実行されました。数式マクロを再度追加した後、コードの最初と最後に追加Application.ScreenUpdating = Falseしました。Application.ScreenUpdating = Trueこれで問題が修正されました。

于 2013-02-20T15:03:26.617 に答える
0

If you don't get any MsgBox popups, then you don't even get past the first few lines. My suspicion is that something goes wrong right at the start:

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...

I would recommend adding an On Error GoTo ErrorHandler statement as the first line in your sub; then add (typically just before the end of the sub)

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

this may take a bit more sleuthing on your part to solve...

于 2013-02-13T03:53:33.370 に答える