【问题标题】:BeforePrint Event is Firing without PrintingBeforePrint 事件在不打印的情况下触发
【发布时间】:2023-03-30 03:12:01
【问题描述】:

我有下面的代码。这让我想知道为什么即使我没有打印任何东西,工作簿代码中的 BeforePrint 事件也会被触发。工作簿绝对不是空白的。错误在于创建 PDF 文件。

该文件将工作表保存为 PDF 格式,其中包含工作表的名称、工作簿的文件路径以及工作表中的一些详细信息。

我缺少什么?我对 VBA 并不陌生,但今天这让我很困扰。我在 Windows 7 Ultimate 上使用 MS Excel 2016。

编辑:我尝试删除以下代码,但问题仍然存在:

IgnorePrintAreas:=False, _
OpenAfterPublish:=True

代码如下:

Option Explicit

Public Sub createpdffile()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim sheetname As String, sheetcode As String
Dim iRow As Long
Dim openPos As Integer
Dim closePos As Integer

'temporarily disable error handler so that I can see where the bug is.
'On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
wbA.Save
'get last row of sheet and set print area to last row with L column
iRow = wsA.Cells(Rows.Count, 1).End(xlUp).Row
wsA.PageSetup.PrintArea = wsA.Range("A1:L" & iRow).Address

'just checking name in sheet and removing needed characters
sheetname = wsA.Name
openPos = InStr(sheetname, "(")
closePos = InStr(sheetname, ")")
sheetcode = Mid(sheetname, openPos + 1, closePos - openPos - 1)

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'create default name for saving file
strFile = sheetcode & " No. " & wsA.Cells(11, 9) & " - " & wsA.Cells(8, 3) & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
'THIS IS WHERE THE ERROR IS LOCATED
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file" & vbNewLine & _
    "Please complete the details needed!", vbOKOnly + vbExclamation, "Error Saving as PDF"
    Resume exitHandler
End Sub

来自Foxfire and Burns and Burns'想法的解决方案:

我在主子之前添加了一个公共声明。

Option Explicit

'added line
Public myboolean as Boolean 

Public Sub createpdffile()

myboolean = True


....

然后我在 BeforePrint 事件中添加了一行内容:

If myboolean = True Then Exit Sub

现在,当调用虚拟 PDF 打印机时,它会绕过 BeforePrint 事件。

【问题讨论】:

  • 很高兴为您提供帮助! :)
  • 顺便说一句,这个 myboolean 变量的默认值是 TRUE,不是吗?
  • 不,默认值为 0,即为 false。为什么?因为通常布尔数据在满足条件时“激活”。最常见的情况是,默认情况下,当您初始化代码时,不满足该条件,因此布尔变量默认为 false
  • 非常感谢。 :)

标签: vba excel


【解决方案1】:
wsA.ExportAsFixedFormat 

该行激活了 BeforePrint 事件。实际上,您正在打印 PDF 文件。它可以用作虚拟 PDF 打印机。

【讨论】:

  • 是这样的吗?我的天啊!有没有建议的解决方法?我想强制我的客户保存 PDF,因此我限制他直接在工作簿中打印。
  • 不确定我是否理解正确。您希望您的客户将文件导出为 PDF,但您不希望执行 BeforePrint 事件的代码?
  • 是的,这就是我想要的。将文件导出为 PDF 是否完全将其打印为 PDF?如果是这样的话,那我的想法是不可能的。 :(
  • 不确定它是否完全像打印,但它确实激活了 BeforePrint 事件,所以我会说,是的。删除 BeforePrint 事件中的代码,或使用仅在该客户端打开工作簿时才激活的公共变量布尔类型。类似 IF MyBooleanVariable=true 然后退出 sub 并将其添加到 BeforePrint 事件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多