Results 1 to 13 of 13
  1. #1
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post Print external PDF without opening it via string entered in a textbox

    Hello,

    I just want to thank you for reading this post first of all

    I want to print a pdf file located in a external (P:\) drive, without opening up the file.

    I have a userform consisting of 2 buttons and a textbox for user input, a PRINT button and a Load button. lets work with PRINT button for now, aka button1

    The user will enter a tex in the textbox field. and click on button1, the code will store that text entered as a string and add the extension .PDF then find that file within the P:\ and send it to the default printer.

    Here is what I have so far...



    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim psi As New ProcessStartInfo
    
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = "P:\pdffilename.pdf"  ' Need to replace this with a string based on the textbox1 input and add .pdf to the string and the location of it for printing.
    
            Process.Start(psi)
    
        End Sub
    Not sure where this part of code will go, but I'm assuming it will save the text enter in textbox1 as string.

    Code:
     Dim pdffilename as String
    pdffilename = Textbox1.Text
    The pdffilename is the text the user will enter and with the click of button1 it will store that as string and should add P:\ AND .pdf extension at the end of it and should replace that part of psi.filename = "P:\pdffilename.pdf"


    Sorry if i seem too repetitive but I really want a way to print pdf files, (hundreds of them) via entering the name of the file in a textbox and click on a print button (button1). rather than searching for the file, opening the file, and clicking print, and clicking "ok" to finally print.

    Thanks,

    Steve.
    Last edited by kingpain; 2013-12-08 at 17:58.

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    I'm not sure if you are tied to a programming language or not but if you just want to print all of the .pdf files is a given directory here's a PowerShell script that will prompt for a drive:\path and then use the default pdf viewer (in my case NitroPDF Reader 3) to print all the .pdf files it finds.
    Code:
    $DriveToPDFs = Read-Host "Enter the Drive:\path to your PDF Files"
    
    Dir "$DriveToPDFs\*.pdf" | `
      Foreach-Object { Start-Process -FilePath $_.FullName –Verb Print -PassThru | `
                       %{sleep 1;$_} | kill}
    Simply paste the code above into Notepad and then save to a file with the .ps1 file type (be sure to enclose the entire name in "" so notepad doesn't add a .txt!)

    If you haven't used PowerShell before you'll need to:
    1. Start Powershell by typing that value in the Search box and selecting Windows PowerShell ISE from the search list.
    2. In the left window type: Set-ExecutionPolicy RemoteSigned.
    3. Verify by typing: Get-ExecutionPolicy (it should return RemoteSigned).
    4. You can now load your previously saved file using the Icons and then pressing the Green Arrow on the toolbar. {BTW: You can test the script by doing the listed steps 1-3 and then just pasting the code from this post into the Right window.}


    Notes:

    The Sleep 1 command above is a delay, in seconds, you may need to adjust this for your PDF reader.

    You can create a Shortcut using the following Target:
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell.exe d:\path\PrintAllPDFS.ps1
    replacing colored text with the appropriate information.

    If you want the program to also print the .pdf files in the sub-directories simply change
    Dir "$DriveToPDFs\*.pdf" to Get-ChildItem "$DriveToPDFs\*.pdf" -Recurse
    making sure you leave the | ` characters in place!

    HTH
    Last edited by RetiredGeek; 2013-12-07 at 15:18.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    kingpain (2013-12-07)

  4. #3
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you very for your reply, But i'm afraid my goal is not to print ALL the pdf at one go. I simply want to enter the name of the PDF in the textbox, and have the code find it and print it. But i will keep your post as reference for future projects I may have, I'm familiar with PowerShell to some degree. but I don't think it is capable of doing what I want via user interface program.

    PS, I'm some what a novice at VB .net and I prefer that platform for my project.

    Thank you,

    Steve
    Last edited by kingpain; 2013-12-08 at 17:56.

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    When you say you want the code to search for it do you mean you do NOT want to provide a location but have the code find the file? If so the PS code can be easily modified to do just that.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  6. The Following User Says Thank You to RetiredGeek For This Useful Post:

    kingpain (2013-12-07)

  7. #5
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Deleted some useless post here.

    Steve
    Last edited by kingpain; 2013-12-08 at 17:56.

  8. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    Ok here's a version that will search the specified drive or drive/path (currently to P:\ you can adjust at line 103 if necessary) for the file NAME (.pdf not needed) and then offer the user the option to print if found. It is fully error trapped and user status messages provided. Let me know what you think.

    FYI: I highly suggest you setup a desktop shortcut as mentioned in an earlier post to the thread. Just make sure to use the name of the file you extract from the attached zip file.



    FindAndPrintPDF.zip
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  9. The Following User Says Thank You to RetiredGeek For This Useful Post:

    kingpain (2013-12-07)

  10. #7
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Very Impressive work my friend, I'm impressed that you made such an awesome program with Powershell.

    I tested it and it doesn't find the pdf. I changed the path from P:\ to C:\temp that is where I have my test files. the P: is the path I use for work.

    Still does not find the PDF. but non the less this is very impressive.

    I will still try to code with VB . net.

    I highly think this will work at home, but at my job all the computers are not own by us, and not everyone can change the policy. So powershell is very impressive but I will still need to make a program of some sort using VB. If you're familiar with VB. or can help me out. I will gladly compensate you. In box me for my contact information.

    Also I notice your signature says "VBA Rules!" I'm thinking maybe I can use Excel macro to accomplish my task. I would much rather VB .net.

    Thanks mate.

  11. #8
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Code:
    Imports System.IO
    Imports System.Drawing
    
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pdffilename As String
            pdffilename = TextBox1.Text
            Dim filepath = "C:\temp\" & TextBox1.Text & ".pdf"
            Dim psi As New ProcessStartInfo
            psi.UseShellExecute = True
    
            psi.Verb = "print"
    
            psi.WindowStyle = ProcessWindowStyle.Hidden
    
            'psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    
            psi.FileName = ("filepath") ' This works if i give the full location of my pdf file instead of the string "filepath"
            ' the messagebox will be removed this is just for testing.
            MessageBox.Show(filepath) ' to comfirm that I'm indeed finding the file at C:\temp\test01.pdf 
    
            Process.Start(psi) ' This is where the code fails. 
    
    
        End Sub
    
      
    
        Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
        End Sub
    
    
    End Class
    What am i doing wrong? it fails to print. giving the error Win32Exception was unhandled "The system cannot find the file specified"

    Failed at Process.Start(psi)

  12. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    When you entered the value for the pdf did you include the .pdf? If so, that is the problem you only need the file name the .pdf is added by the program. If you didn't include it I'm at a loss as to why it didn't find the file as it works perfectly on my system even finding files in sub folders.
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  13. #10
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Nope, I didn't enter the .pdf, I simply enter the file name and it didn't work, came up with an error saying it cannot find the file.

    But this is a very impressive piece of work. I'm very close to figuring it with VB .net, however, I've hit a snag at one line in the coding. see above post if you can help.

  14. #11
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    Sorry I don't know or use VB.net.

    Here's an updated version of the PowerShell code.
    1. Removed unecessary code.
    2. The Enter & Esc keys now work for OK & Cancel Respectively.
    3. Program will accept an Argument for the Base drive directory ex:

      C:\WINDOWS\system32\WindowsPowerShell\v1.0\powersh ell.exe "G:\BEKDocs\Scripts\FindAndPrintPDF.ps1 -SearchPath C:"

      and you can set the Default value for this in the code if no argument is supplied.

      param (
      [Parameter(Mandatory=$False)]
      [string]$SearchPath = "G:\BEKDocs" #Drive\Path as base for file search
      )
    4. It will now also find Hidden files. It will not however search certian directories on the C: drive although I have suppressed the error messages this would print int he PS window.

    As I'm still learning PowerShell and the Windows Presentation Framework figuring this out has been a great exercise. I don't know if it will solve your problem with it but I had fun and learned a lot doing it.
    FindAndPrintPDF.zip
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


  15. #12
    New Lounger
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you mate, I'm very grateful for your work. I will use your code and I will also learn more about VB and powershell AND Java.

    I got my code to work, I was able to do the things I wanted to be done. Whats funny is, 2 weeks ago I had no experience with VB, after a few hours of lessons on youtube, I was able to implement the codes I found all over the place and study it. and in the end I was able to get what I want.

    Knowledge is indeed power. I will present this project at my job and gain reputation and a days pay.
    I will also show our head engineer your code, He wants to learn powershell also.

    THANK YOU my friend

  16. #13
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    6,158
    Thanks
    200
    Thanked 781 Times in 715 Posts
    Steve,

    Glad you got your code to work!
    May the Forces of good computing be with you!

    RG

    VBA Rules!

    My Systems: Desktop Specs
    Laptop Specs


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •