VBScript to Print PDF files

Discussion in 'Scripting' started by cs, Apr 30, 2008.

  1. cs

    cs Guest

    I have a script to print PDF files and check to see when the document has
    spooled to the printer before renaming the file. I have 4 test PDF files.
    One of the documents was created by someone else. When trying to rename the
    file using the routine below I get an error 70 permissions denied error. I
    can manually rename the file but from the script it gets this error. Any
    ideas on what I can try?
    The script is currently running under my account with Admin privileges.

    '*********************************************************
    '* Move_File OR Rename_File
    '* Input : Old File Name, New File Name
    '*********************************************************
    Sub Move_File(strOldFileName, strNewFileName)
    Dim WshShell, fso, DoRename
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    DoRename = TRUE
    IF NOT fso.FileExists(strOldFileName) then
    WshShell.Popup("Source File Not Found: " & strOldFileName), 5, "File
    Rename/Move Error"
    ShowDebug "Source File Not Found: " & strOldFileName, "File Rename/Move
    Error"
    DoRename = FALSE
    End if
    IF fso.FileExists(strNewFileName) then
    WshShell.Popup("Destination File Exists: " & strNewFileName), 5, "File
    Rename/Move Error"
    ShowDebug "Destination File Exists: " & strNewFileName, "File Rename/Move
    Error"
    DoRename = FALSE
    end if
    IF DoRename then fso.MoveFile strOldFileName, strNewFileName
    If Err.Number <> 0 then
    WshShell.Popup("Error in Rename/Move! ErrNumber=" & Err.Number & "
    ErrDescription=" & Err.Description & vbCRLF & _
    "Src: " & strOldFileName & vbCRLF & "Dest: " & strNewFileName), 10, "File
    Rename/Move Error"
    ShowDebug "Error in Rename/Move!" & vbCRLF & "Src: " & strOldFileName &
    vbCRLF & "Dest: " & strNewFileName, "File Rename/Move Error"
    End if
    End Sub

    'The rest of the script is included below for reference

    strComputer = "."

    TargetFolder = "C:\temp\test"
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(TargetFolder)
    Set colItems = objFolder.Items
    For Each objItem in colItems
    if instr(objItem.Name, "PDF") > 0 or instr(objItem.Name, "pdf") > 0 then
    set objShell = CreateObject("WScript.Shell")
    iErrorCode = objShell.Run("AcroRd32.exe /p /h " & objitem.path)
    'iErrorCode = objShell.Run("C:\Program Files\Adobe\Reader
    8.1\Reader\AcroRd32.exe /p /h " & objItem.path,TRUE)
    NewFileName = objitem.name & ".Printed"
    count = 0
    bolFoundDocumentPrintJob = false
    DO While (bolFoundDocumentPrintJob = false)
    wscript.sleep 1000
    count = count + 1
    ' wscript.echo "objitem.name=" & objitem.name & vbcrlf &
    "NewFileName=" & newfilename & vbcrlf & "objitem.path=" & objitem.path
    bolFoundDocumentPrintJob = FoundDocumentPrintJob(objItem.Name)
    if count > 99 then
    exit do
    end if
    Loop
    if Count > 99 then
    wscript.echo "PrintJob Not Found after 100 seconds"
    wscript.quit
    else
    ' wscript.echo "Found Document " & vbcrlf & "objItem.name=" &
    objitem.name & " NewFileName=" & NewFileName
    move_file objitem.name, NewFileName
    end if
    end if
    Next
    'wscript.echo "Done!"
    wscript.quit

    Function FoundDocumentPrintJob(strPrintJobDocument)
    FoundDocumentPrintJob = false
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_PrintJob")
    For Each objPrintJob in colItems
    If objPrintJob.Document = strPrintJobDocument then
    ' wscript.echo "objPrintJob.Document=" & objPrintJob.Document & " " &
    "strPrintJobDocument=" & strPrintJobDocument
    FoundDocumentPrintJob = true
    Exit For
    End if
    Next
    End Function
     
    cs, Apr 30, 2008
    #1
    1. Advertisements

  2. I had no problem when running the script below:

    01. Move_File "d:\temp\vbs1.tmp", "d:\temp\vbs1.vbs"
    02.
    03. '*********************************************************
    04. '* Move_File OR Rename_File
    05. '* Input : Old File Name, New File Name
    06. '*********************************************************
    07. Sub Move_File(strOldFileName, strNewFileName)
    08. Dim WshShell, fso, DoRename
    09.
    10. Set fso = CreateObject("Scripting.FileSystemObject")
    11. Set WshShell = WScript.CreateObject("WScript.Shell")
    12. DoRename = True
    13.
    14. If Not fso.FileExists(strOldFileName) Then
    15. WshShell.Popup("Source File Not Found: " & strOldFileName), 5, _
    16. "File Rename/Move Error"
    17. ShowDebug "Source File Not Found: " & strOldFileName, "File Rename/Move
    Error"
    18. DoRename = False
    19. End If
    20.
    21. If fso.FileExists(strNewFileName) Then
    22. WshShell.Popup("Destination File Exists: " & strNewFileName), 5, "File
    Rename/Move Error"
    23. ShowDebug "Destination File Exists: " & strNewFileName, "File
    Rename/Move Error"
    24. DoRename = False
    25. End If
    26.
    27. If DoRename Then
    28. On Error Resume Next
    29. Err.Clear
    30. fso.MoveFile strOldFileName, strNewFileName
    31. If Err.Number <> 0 Then
    32. WshShell.Popup("Error in Rename/Move! ErrNumber=" & Err.Number &
    "ErrDescription=" & Err.Description & VbCrLf & _
    33. "Src: " & strOldFileName & VbCrLf & "Dest: " & strNewFileName), 10,
    "File Rename/Move Error"
    34. ShowDebug "Error in Rename/Move!" & VbCrLf & "Src: " & strOldFileName
    & VbCrLf & "Dest: " & strNewFileName, "File Rename/Move Error"
    35. End If
    36. On Error Goto 0
    37. End If
    38. End Sub

    Note that I moved the "On Error" statement to Line #28. In your current
    code it occurs much sooner, which makes debugging very difficult.
    I suggest you comment it out altogether while testing your script so
    that you can find out where the problem occurs.
     
    Pegasus \(MVP\), Apr 30, 2008
    #2
    1. Advertisements

  3. cs

    cs Guest

    Thanks for the response. I am still getting errors in the Move_File routine.
    I am wondering if it is because the "For each objItem in colItems" which
    would be the file handles. Would this cause the move/rename of the file to
    not work? I tried changing the {NewFileName = "Printed\" & objitem.name} to
    actual move the file to a sub folder named "Printed". When I run tests on
    the script I get inconsistent results. 2 of the files moved successfully and
    2 did not move. The printer queue which I pause always gets the 4 files in
    the queue.
     
    cs, Apr 30, 2008
    #3
  4. cs

    cs Guest

    Update - I just added a {wscript.sleep 5000} before the move file and now
    everything works. Not sure why the delay is needed, unless the file is not
    fully in the print queue.
     
    cs, Apr 30, 2008
    #4
  5. Attempting to rename a locked file would certainly
    explain your problem. You might want to duplicate
    it by running a batch file to perform the rename - it
    would give you an explicit error message.
     
    Pegasus \(MVP\), Apr 30, 2008
    #5
  6. cs

    sumar

    Joined:
    Sep 8, 2010
    Messages:
    1
    Likes Received:
    0
    Command Line PDF Printing

    You can have problems with closing acrobat with diferent versions.
    There is a command line tool CLPrint for pdf printing that is much easyer to configure.
    www.commandlinepdf.com
     
    sumar, Sep 8, 2010
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.