我记录了一个用于页面设置的宏,它生成了一大堆被设置的属性。然后,我跑了一个宏,说了几句书信,去洗手间,喝了一杯咖啡,然后坐下来,等待pagesetup宏完成。不用说它的处理速度非常慢。
我在Google上进行了一些搜索,发现运行起来非常缓慢,并遇到了三个建议。
3与2类似,这是我决定采用的路线。基本上,我将通过if语句运行每个属性更改。
IF .property <> wanted.value then .property = wanted.value
因此,通过一些文本操作,我快速转换了录制的宏,在该宏中,在设置相应的值之前将所有属性值设置为IF THEN检查。
运行代码时,出现错误“ Type Mismatch”。它并没有带我去任何特定的地方。但是,当我单步执行代码时,它在以下行崩溃了:
If .PrintQuality <> 600 Then .PrintQuality = 600
没有IF的行可以正常工作而没有错误
.PrintQuality = 600
IF THEN编码在此行的前几行工作。 引发错误的PRINTQUALITY检查有什么问题?
示例代码
Sub SetupPage(ByVal wks As Worksheet)
Select Case wks.Name
Case Worksheets(2).Name
'Set Page size margins etc
With wks.PageSetup
If .PrintTitleRows <> "$1:$12" Then .PrintTitleRows = "$1:$12"
If .PrintTitleColumns <> "" Then .PrintTitleColumns = ""
If .LeftHeader <> "" Then .LeftHeader = ""
If .CenterHeader <> "" Then .CenterHeader = ""
If .RightHeader <> "" Then .RightHeader = ""
If .LeftFooter <> "" Then .LeftFooter = ""
If .CenterFooter <> "Page &P of &N" Then .CenterFooter = "Page &P of &N"
If .RightFooter <> "" Then .RightFooter = ""
If .LeftMargin <> Application.InchesToPoints(0.236220472440945) Then .LeftMargin = Application.InchesToPoints(0.236220472440945)
If .RightMargin <> Application.InchesToPoints(0.236220472440945) Then .RightMargin = Application.InchesToPoints(0.236220472440945)
If .TopMargin <> Application.InchesToPoints(0.748031496062992) Then .TopMargin = Application.InchesToPoints(0.748031496062992)
If .BottomMargin <> Application.InchesToPoints(0.748031496062992) Then .BottomMargin = Application.InchesToPoints(0.748031496062992)
If .HeaderMargin <> Application.InchesToPoints(0.31496062992126) Then .HeaderMargin = Application.InchesToPoints(0.31496062992126)
If .FooterMargin <> Application.InchesToPoints(0.31496062992126) Then .FooterMargin = Application.InchesToPoints(0.31496062992126)
If .PrintHeadings <> False Then .PrintHeadings = False
If .PrintGridlines <> False Then .PrintGridlines = False
If .PrintComments <> xlPrintNoComments Then .PrintComments = xlPrintNoComments
****************************************************************************
If .PrintQuality <> 600 Then .PrintQuality = 600
****************************************************************************
If .CenterHorizontally <> False Then .CenterHorizontally = False
If .CenterVertically <> False Then .CenterVertically = False
If .Orientation <> xlLandscape Then .Orientation = xlLandscape
If .Draft <> False Then .Draft = False
If .PaperSize <> xlPaperLetter Then .PaperSize = xlPaperLetter
If .FirstPageNumber <> xlAutomatic Then .FirstPageNumber = xlAutomatic
If .Order <> xlDownThenOver Then .Order = xlDownThenOver
If .BlackAndWhite <> False Then .BlackAndWhite = False
If .Zoom <> False Then .Zoom = False
'set number of pages wide to 1 and length to as required
If .FitToPagesWide <> 1 Then .FitToPagesWide = 1
If .FitToPagesTall <> False Then .FitToPagesTall = False
If .PrintErrors <> xlPrintErrorsDisplayed Then .PrintErrors = xlPrintErrorsDisplayed
If .OddAndEvenPagesHeaderFooter <> False Then .OddAndEvenPagesHeaderFooter = False
If .DifferentFirstPageHeaderFooter <> False Then .DifferentFirstPageHeaderFooter = False
If .ScaleWithDocHeaderFooter <> True Then .ScaleWithDocHeaderFooter = True
If .AlignMarginsHeaderFooter <> False Then .AlignMarginsHeaderFooter = False
If .EvenPage.LeftHeader.Text <> "" Then .EvenPage.LeftHeader.Text = ""
If .EvenPage.CenterHeader.Text <> "" Then .EvenPage.CenterHeader.Text = ""
If .EvenPage.RightHeader.Text <> "" Then .EvenPage.RightHeader.Text = ""
If .EvenPage.LeftFooter.Text <> "" Then .EvenPage.LeftFooter.Text = ""
If .EvenPage.CenterFooter.Text <> "" Then .EvenPage.CenterFooter.Text = ""
If .EvenPage.RightFooter.Text <> "" Then .EvenPage.RightFooter.Text = ""
If .FirstPage.LeftHeader.Text <> "" Then .FirstPage.LeftHeader.Text = ""
If .FirstPage.CenterHeader.Text <> "" Then .FirstPage.CenterHeader.Text = ""
If .FirstPage.RightHeader.Text <> "" Then .FirstPage.RightHeader.Text = ""
If .FirstPage.LeftFooter.Text <> "" Then .FirstPage.LeftFooter.Text = ""
If .FirstPage.CenterFooter.Text <> "" Then .FirstPage.CenterFooter.Text = ""
If .FirstPage.RightFooter.Text <> "" Then .FirstPage.RightFooter.Text = ""
End With
Case "FOO"
With wks.PageSetup
'Set all the stuff above to some other values
End With
Case Else
With wks.PageSetup
'Set all the stuff above to some other values
End With
End Select
End Sub
答案 0 :(得分:2)
水平打印质量(1)或垂直打印质量(2)。某些打印机可能不支持垂直打印质量。如果不指定此参数,则PrintQuality属性将返回(或可以将其设置为)包含水平和垂直打印质量的两个元素的数组。
由于.PrintQuality <> 600
与数组的比较,Type Mismatch
返回了600
。
由于文档中提到如果打印机不支持,尝试设置垂直打印质量可能会失败,所以我想可能是这样的答案(但未经测试,因为我的打印机支持这两种方法):
On Error Resume Next
If .PrintQuality(1) <> 600 Then .PrintQuality(1) = 600
If .PrintQuality(2) <> 600 Then .PrintQuality(2) = 600
On Error GoTo 0