excel - 限制打印份数

标签 excel printing vba

我有 5 台计算机和两台打印机通过 LAN 连接。在其中一台计算机(未共享)中有一个特定的 excel 文档,我希望将可打印的副本数量限制为 4。这意味着用户不能打印超过 4 个该文档的副本。

我知道影印(和更多)漏洞,但我仍然希望打印副本以受控或有限的数量出现。

我浏览了一些打印控制软件的功能,但我了解到它们都有一个“配额”系统,用户在超出限制后必须支付打印费用。恐怕这对我不起作用。

我还阅读了此处发布的类似问题的答案,Set number of copies per worksheet

值得庆幸的是,这个答案对我很有帮助,除了我不知道如何限制或限制用户打印超出指定数量的内容。

我也阅读了很多答案,说限制副本数量几乎是不可能的,但我仍然希望寻求帮助 - 也许会出现一些解决方案。

我对计算机/打印机编程没有太多深入的了解。虽然不是专业人士,但我对 excel vba 有点熟悉。

请让我知道是否有任何解决方案,

一旦我找到东西,我会在这里发布。

非常感谢您的帮助。

最佳答案

这是一个粗略的解决方案,但这会增加打印数量的一些限制......

置入ThisWorkbook :

Private Sub Workbook_BeforePrint(Cancel As Boolean)    
    Cancel = True  
    If Cancel = True Then
        MsgBox "Please use the print button on Sheet1."
    End If   
End Sub

添加 CommandButton并将其重命名为 PrintButton然后将这个子程序(和附带的函数)插入到 Module
Private Sub PrintButton_Click()
  On Error Resume Next
    Application.EnableEvents = False

        If (CanWePrint(4)) Then
           ActiveSheet.PrintOut
        Else
            MsgBox ("Sorry this is the maximum number of prints for this document!")
        End If

    Application.EnableEvents = True
    On Error GoTo 0
End Sub


Function CanWePrint(ByVal MaxPrintVal As Integer) As Boolean

    Dim CurrentPrintCount As String, SecretFile As String

    'PLEASE CHANGE TO YOUR "SECRET" TXT FILE NAME AND LOCATION!
    SecretFile = "C:\Users\Matt\Documents\countPrint.txt"

    CurrentPrintCount = GetCount(SecretFile)

    If (CurrentPrintCount < MaxPrintVal) Then
        Call UpdatePrintCount(CurrentPrintCount, SecretFile)
        CanWePrint = True
    Else
        CanWePrint = False
    End If

End Function

Function GetCount(ByVal SecretFile As String) As Integer

   Dim nSourceFile As Integer
   Dim sText As String

   Close

   nSourceFile = FreeFile

   Open SecretFile For Input As #nSourceFile
    sText = Input$(LOF(1), 1)
   Close

   GetCount = CInt(sText)

End Function

Sub UpdatePrintCount(ByVal CurrentVal As Integer, ByVal SecretFile As String)

    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer

    iFileNum = FreeFile
    Open SecretFile For Input As iFileNum

    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close iFileNum

    sTemp = Replace(sTemp, CurrentVal, CurrentVal + 1)

    iFileNum = FreeFile
    Open sFileName For Output As iFileNum

    Print #iFileNum, sTemp

    Close iFileNum

End Sub

这是做什么的

此代码将禁用 Excel 中该工作簿的标准打印选项。通过添加 CommandButton您创建一个手动打印选项,它将检查存储在 .txt 中的打印计数文件,这意味着文档可以关闭并重新打开,但仍然只能打印 4 次。

你需要做什么
  • 在与本文档相同的机器上创建一个新的 txt 文件,并在 CanWePrint 中更新上面代码中的路径.

  • 缺点

    就像我说的那样,这是一个粗略的解决方案,有很多方法可以解决这个问题:
  • 手动更改 .txt 中的值文件
  • 不使用 VBA 保存工作簿
  • 禁用 VBA
  • 关于excel - 限制打印份数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34737900/

    相关文章:

    c# - 使用C#.net直接向LPT并口发送打印命令

    excel - VBA函数: return string with newline

    excel - 使用 openpyxl 保存现有工作簿会重置现有图表格式

    excel - 按组列的 Power Query 总和作为新列

    excel - 如何将查询导出到 Excel 而不将备忘录截断为 255 个字符?

    excel vba需要控制来检查是否有空单元格

    mysql - 为什么连接字符串只能在开发机器上工作?

    windows - 你能摆脱 "Microsoft Office Excel has encountered a problem"对话框吗?

    Python列表打印

    php - 为什么 print 和 echo 在 "for"循环中表现不同