excel - Pagesetup.printquality 导致类型不匹配错误

标签 excel vba

我录制了一个用于页面设置的宏,它生成了一整套要设置的属性。然后我运行了这个宏,处理了一些信件,去了洗手间,喝了一杯咖啡,然后坐下来等待页面设置宏完成。不用说,处理速度非常慢。

我做了一些谷歌搜索,发现它的运行速度非常慢,并发现了三个建议。

  1. 使用内置 VBA 4 宏来执行大部分页面设置属性,因为它运行速度更快。
  2. 限制您要更改的属性
  3. 仅更改需要更改的属性

3 与 2 有点相似,这是我决定采取的路线。基本上我会通过 if 语句运行每个属性更改。

IF .property <> wanted.value then .property = wanted.value

因此,通过一些文本操作,我快速转换了录制的宏,其中它将所有属性值设置为 IF THEN 检查,然后再设置相应的值。

当我运行代码时,出现错误“类型不匹配”。它没有带我到任何特定的线路。然而,当我单步执行代码时,它在以下行崩溃了:

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

最佳答案

来自 PageSetup.PrintQuality 文档,

Horizontal print quality (1) or vertical print quality (2). Some printers may not support vertical print quality. If you don't specify this argument, the PrintQuality property returns (or can be set to) a two-element array that contains both horizontal and vertical print quality.

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

关于excel - Pagesetup.printquality 导致类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58067153/

相关文章:

python - 使用 xlwt 将超链接添加到带有文本的单元格

c# - 在 C# 中访问打开的 Excel 工作簿

arrays - 查找字符串数组中字符串的索引

excel - 如何仅获取 Apache POI 中未隐藏的工作表数量

excel - VBA 中的简单脚本,用于从标记的单元格中获取值、添加它们并显示总数

vba - 打开、保存和关闭任何文件

excel - 范围公式最后一行

vba - VBA : Search, save and replace by rows according to conditions

excel - 当我创建新的 UDF 并将其分配给单元格时,为什么 Excel 会在相同和不同的工作簿中重新执行函数?

vba - Excel vba 进度条不工作 : invalid property value