Avatar of Steve B
Steve B
Flag for United States of America asked on

Using vbscript to print documents in ascending order

Please see the vbscript below.  How can I modify this script so that it prints out documents in ascending order?  I have 6 Word/Excel documents that need to print to comprise a packet so I would like them to print in order. I have each document prefixed with a # to sort, i.e. 1 - Documentation.docx ... 2 - FAQ.docx etc.  Right now, it will just print in random order. My goal is to have each document print in the correct order for the number of copies requested since it is a packet.


Option Explicit

Dim NumberOfCopies

NumberOfCopies = GetNumberOfCopies()
PrintDocuments NumberOfCopies

Function EndsWith(textToTest, endText)

  On Error Resume Next

  Dim result

  result = False
  result = Rigth(textToTest, Len(endText)) = endText
  EndsWith = result

End Function

Function GetNumberOfCopies()

  On Error Resume Next

  Dim inputText
  Dim result

  inputText = InputBox("Input the number of copies you would like")
  result = 0
  result = CLng(inputText)
  GetNumberOfCopies = result

End Function

Sub PrintDocuments(numberOfCopies)

  Dim currentNumberOfCopies
  Dim currentPath
  Dim files
  Dim currentFile
  Dim shApp
  Dim shFolder

  If numberOfCopies < 1 Then
    Exit Sub
  End If

  Set shApp = CreateObject("Shell.Application")
  currentPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
  Set shFolder = shApp.NameSpace(currentPath)
  Set files = shFolder.Items()
  For Each currentFile in files
      currentNumberOfCopies = numberOfCopies
      Do While currentNumberOfCopies > 0
        
        currentFile.InvokeVerbEx ("Print")
        currentNumberOfCopies = currentNumberOfCopies - 1
WScript.Sleep 5000
      Loop

  Next 

End Sub

Open in new window

VB Script

Avatar of undefined
Last Comment
Steve B

8/22/2022 - Mon
ste5an

Use a Scripting.Dictionary. It is sorted per se, you just need a lexical sortable key. E.g.

Option Explicit

Dim NumberOfCopies
Dim Documents

NumberOfCopies = GetNumberOfCopies()
If numberOfCopies > 0 Then
  Set Documents = GetDocuments()
  PrintDocuments Documents, NumberOfCopies
End If

Function GetDocuments()

  Dim currentFile
  Dim currentPath
  Dim files
  Dim index
  Dim result
  Dim shApp
  Dim shFolder

  Set shApp = CreateObject("Shell.Application")
  currentPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
  Set shFolder = shApp.NameSpace(currentPath)
  Set files = shFolder.Items()
  Set result = CreateObject("Scripting.Dictionary")
  For Each currentFile in files
    index = InStr(currentFile.Name, "-")
    If index > 0 Then
      index = Rtrim(Left(currentFile.Name, index - 1))
      result.Add LeftPad(index, 4, "0"), currentFile
    End If
  Next

  Set GetDocuments = result

End Function

Function GetNumberOfCopies()

  On Error Resume Next

  Dim inputText
  Dim result

  inputText = InputBox("Input the number of copies you would like")
  result = 0
  result = CLng(inputText)
  GetNumberOfCopies = result

End Function

Function LeftPad(stringToPad, length, characterToPad)

  Dim charactersNeeded

  charactersNeeded = 0
  If Length > Len(StringToPad) Then
    charactersNeeded = length - Len(stringToPad)
  End If

  LeftPad = String(charactersNeeded, characterToPad) & stringToPad

End Function

Sub PrintDocument(document, ByVal numberOfCopies)

  WScript.StdOut.Write document.Name
  Do While numberOfCopies > 0
    WScript.StdOut.Write "."
    document.InvokeVerbEx ("Print")
    WScript.Sleep 500
    numberOfCopies = numberOfCopies - 1
  Loop

  WScript.Echo ""

End Sub

Sub PrintDocuments(documents, numberOfCopies)

  Dim currentFile
  Dim documentKey

  For Each documentKey in documents.Keys
    PrintDocument documents.Item(documentKey), numberOfCopies
  Next

End Sub

Open in new window

Steve B

ASKER
That worked like a charm.  One last thing ... is there a way to have it print the selection of documents I am working with in order each time?  The current version of the above scrpt prints each document X number of times so I have to manually collate them in a packet.  If it could run through and print each document once and then loop back through X number of times (numberOfcopies), that would be awesome.  Each of the documents makes up a packet of information.  Thanks for the help since this will be a lifesaver.  
ASKER CERTIFIED SOLUTION
ste5an

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Steve B

ASKER
Thank you very much.  I am learning, thanks to you.  :)
Your help has saved me hundreds of hours of internet surfing.
fblack61