【问题标题】:VBA: Printer listVBA:打印机列表
【发布时间】:2017-01-23 09:56:03
【问题描述】:

我想开发一个打印系统来检查每天要打印的文档列表,并且每小时执行一次。

到目前为止,我可以打印一个文档,但是当需要打印更多时,代码仅适用于第一个。

Sub printTag()

Dim strCommand As String
Dim filePath As String
Dim FileName As String
Dim printer As String
Dim numRefs As Integer
Dim x As Integer
Dim ref As String
Dim numFiles As Integer
Dim t As Integer
Dim difD As Long
Dim difH As Long
Dim difM As Long
Dim listDate As Date
Dim nowDate As Date

nowDate = ThisWorkbook.Sheets("Print").Range("B8")
printer = ThisWorkbook.Sheets("Print").Range("B2")
numRefs = WorksheetFunction.CountA(ThisWorkbook.Sheets("List").Columns("A"))
numFiles = WorksheetFunction.CountA(ThisWorkbook.Sheets("Relation").Columns("A"))

For x = 1 To numRefs
    On Error Resume Next
    listDate = ThisWorkbook.Sheets("List").Range("A" & x)
    difD = DateDiff("d", nowDate, listDate)
    If difD = 0 Then
    difH = DateDiff("h", nowDate, listDate)
    difM = DateDiff("n", nowDate, listDate)
        If difH = 0 Then
            If difM >= 0 Then
                For t = 1 To numFiles
                    If ThisWorkbook.Sheets("List").Range("B" & x) = ThisWorkbook.Sheets("Relation").Range("A" & t) Then
                        filePath = ThisWorkbook.Sheets("Print").Range("B1") & "\" & ThisWorkbook.Sheets("Relation").Range("B" & t)
                        ThisWorkbook.Sheets("Print").Range("B3") = strCommand
                        strCommand = "PRINT " & filePath & "/D:" & printer
                        Shell strCommand, 1
                    End If
                Next t
            End If
        End If
    End If
Next x

End Sub

【问题讨论】:

    标签: vba loops printing date-difference


    【解决方案1】:

    我想创建一个脚本,而不是在命令行中发送多个实例,并且效果很好。结果如下:

    Sub printTag()
    
    Dim strCommand As String
    Dim filePath As String
    Dim FileName As String
    Dim printer As String
    Dim numRefs As Integer
    Dim x As Integer
    Dim ref As String
    Dim numFiles As Integer
    Dim t As Integer
    Dim difD As Long
    Dim difH As Long
    Dim difM As Long
    Dim listDate As Date
    Dim nowDate As Date
    
    nowDate = ThisWorkbook.Sheets("Print").Range("B8")
    printer = ThisWorkbook.Sheets("Print").Range("B2")
    numRefs = WorksheetFunction.CountA(ThisWorkbook.Sheets("List").Columns("A"))
    numFiles = WorksheetFunction.CountA(ThisWorkbook.Sheets("Relation").Columns("A"))
    
    If Len(Dir$(ThisWorkbook.Path & "\list.bat")) > 0 Then
         Kill ThisWorkbook.Path & "\list.bat"
    End If
    intFile = FreeFile()
    Open ThisWorkbook.Path & "\list.bat" For Output As #intFile
    
    For x = 1 To numRefs
        On Error Resume Next
        listDate = ThisWorkbook.Sheets("List").Range("A" & x)
        difD = DateDiff("d", nowDate, listDate)
        If difD = 0 Then
        difH = DateDiff("h", nowDate, listDate)
        difM = DateDiff("n", nowDate, listDate)
            If difH = 0 Then
                If difM >= 0 Then
                    For t = 1 To numFiles
                        If ThisWorkbook.Sheets("List").Range("B" & x) = ThisWorkbook.Sheets("Relation").Range("A" & t) Then
                            filePath = ThisWorkbook.Sheets("Print").Range("B1") & "\" & ThisWorkbook.Sheets("Relation").Range("B" & t)
                            Print #intFile, "PRINT " & filePath & " /D:" & printer
                        End If
                    Next t
                End If
            End If
        End If
    Next x
    Print #intFile, "exit"
    Close #intFile
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2022-01-16
      • 2011-08-24
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多