Print userform to pdf

  • Put this in a new module and call the last sub with the inputs. Obviously, the userform should be showing before the call.


  • Or,
    In a Module:


    Use in a Userform command button such as:

    Code
    Private Sub CommandButton1_Click()
        TextBox1.SetFocus
        WindowToPDF Environ("temp") & "\test.pdf"
        Unload Me
        Shell "cmd /c " & Environ("temp") & "\test.pdf", vbNormal
    End Sub
  • Thank you [USER="28486"]kenneth[/USER] Hobson. I copied and pasted your code into a new module. An error message came up "The code in this module must be updated for use on 64-bit system."


    What should I do? Many thanks.

  • This should be close for the 64bit and 32bit version using post #2 method.


    The Sendkeys() method using one API in post #3 is simple. I like to avoid sendkey solutions but it is one of the more simple ones so it should be fairly reliable.

  • You did not have post #3 code in the file. The file's userform is too big to fit on my screen. Even so, on my 32 bit Excel version, it ran fine but did not show the whole userform in the pdf.


    API routines have to be at the top of a Module. When using someone else's API routines, it best to make a Module just for it. Add your custom code below it, in another Module, or userform as in your case.


    I will look into modifying code #7 code sometime. Doing it for 64 bit in VBA7, the PtrSafe and LongPtr is used for some routines. Even if that is right, one has to do the same when used in the other routines that call it. They need their variables to be of the same type. When trying to make code that works on 32 bit and 64 bit, code almost doubles.


    After I added the module with #3 code, and change the textbox reference, it worked fine. Since it is just one API routine, it is best to make that work. I suggest just using that method.

    Code
    Private Sub CommandButton1_Click()
        txtLotNo.SetFocus
        WindowToPDF Environ("temp") & "\test.pdf"
        'SaveScreenToPDF Environ("temp") & "\test.pdf", 2    '1=Portrait, 2=Landscape
        Unload Me
        Shell "cmd /c " & Environ("temp") & "\test.pdf", vbNormal
    End Sub


    The WindowToPDF routine can be coded to allow orientation option as I did for SaveScreenToPDF.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!