Page 2 of 2 FirstFirst 12
Results 41 to 49 of 49

Thread: Save PDF file with VBs Printer Object

  1. #41

    Thread Starter
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,224

    Re: Save PDF file with VBs Printer Object

    Who talked about PCODE? I don't think the issue has anything to do with PCODE or not PCODE.

    I am argentinian.

    The crash will happen if you close the program without resetting the API hook to the original state.
    Set PrinterFilePath = "" inmediately after the printing operation, and don't end the program before doing that.

  2. #42
    Lively Member
    Join Date
    May 2022
    Posts
    99

    Re: Save PDF file with VBs Printer Object

    HI, i have it solved. i'm doing:


    First Dim/Declare this:

    Code:
      Dim strCurrentPrn As String
      Dim p As Printer
      Private Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long

    Create this sub
    Code:
    Private Sub Pausa(Tiempo As Byte)
    
      Dim TiempoPausa, inicio, Final, TiempoTotal
    
        TiempoPausa = Tiempo 
        inicio = Timer 
        Do While Timer < inicio + TiempoPausa
            DoEvents 
        Loop
    End Sub

    Then, when you want to print:

    Code:
            strCurrentPrn = Printer.DeviceName  ' Save Actual Printer
    
            'Search if we have installed the PDF printer
            For Each p In Printers
                If p.DeviceName = "Microsoft Print to PDF" Then
                    SetDefaultPrinter p.DeviceName    
                    Set Printer = p
                    GoTo ItsOk
                End If
            Next p
    
    
            MsgBox "No PDF Printer found", vbCritical, "Error!!"
            Exit Sub
    
    ItsOk:
    
          PrinterFilePath = "C:\TEMP\" & Filename
    
          'Print Datareport
          '##########
    
           Datareport_Name.PrintReport False, rptRangeAllPages
    
           'Do a Pause (Important!!!)  (I found that if you create the PDF in a local hard drive, it works better than in a network folder)
           '##############################################################################
           Pausa 1
    
           PrinterFilePath = ""
    
    
         'returning Printer
         '##########  
    
            For Each p In Printers
                If p.DeviceName = strCurrentPrn Then
                    Set Printer = p
                    SetDefaultPrinter p.DeviceName 
                    Exit For
                End If
            Next
    
            Unload Informe  <----- this is the Datareport's name
    
          '  closing recordset and connection to mysql
    
            If Rst.State = 1 Then
                Rst.Close
            End If
            If Cnx.State = 1 Then
                Cnx.Close
            End If
    
            Set Rst = Nothing
            Set Cnx = Nothing

  3. #43
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,099

    Re: Save PDF file with VBs Printer Object

    It's not working for me; it creates a 0-byte pdf in the specified directory and then locks up (not oh exit, after clicking the button). (To be clear, yes I am talking about VB6, not tB; in tB, it locates and writes the memory at lPtr, but it doesn't work to replace the function; want to get it working in VB6 before pursuing it)
    Last edited by fafalone; Jul 10th, 2024 at 05:00 AM.

  4. #44
    Lively Member
    Join Date
    May 2022
    Posts
    99

    Re: Save PDF file with VBs Printer Object

    I'm talking about Vb6 too.

    Did you do everything? i mean, used the "pausa" sub.

    I had this problem too, it was solved with the pausa sub, try to put 2 or 3 seconds (just to try)

  5. #45
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,070

    Re: Save PDF file with VBs Printer Object

    you can change Registry for pdf printer port to D:\002.PDF OR E:\ABC.PDF

    If you modify the exported PDF path every time you print it, you can realize continuous numbering. Save to any file name and path.
    You don't need any hook API, and you don't need to write any code.
    Just change the port of the printer from USB,LPT to file name, as simple as that.

    Code:
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports]
    "D:\\output.pdf"=""
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\Microsoft Print to PDF]
    "ChangeID"=dword:0b79c556
    "StatusExt"=dword:00000000
    "Status"=dword:00000180
    "Name"="Microsoft Print to PDF"
    "Port"="D:\\output.pdf"

  6. #46
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,070

    Re: Save PDF file with VBs Printer Object

    use hook api,you can try how to hook word.exe open file and savefile ,so it can be load virtual file from memroy without disk.
    Virtual file, let windows media player ocX load MP3 from file, and change it into memory variable.
    Can you do something similar? In any case, it is really convenient to use various HOOK functions in VB6 ide.

  7. #47
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,070

    Re: Save PDF file with VBs Printer Object

    Printer.Circle (1000, 1000), 500, vbRed
    Can this draw polygons, ellipses, rounded rectangles, and support a variety of graphic line shapes?

  8. #48
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,070

    Re: Print to specific PDF file name

    Quote Originally Posted by Jimboat View Post
    Yes, that was all that the problem was. thanks, Edwardo. your program works great.

    Add'l question....
    is there a way to autofill the Windows dialog the "Microsoft Print to PDF" presents to the user, and allow the dialog to remain operational? One of the issues that users have is that the windows dialog initially shows with no filename, and users get error if they click either 'Save' or 'Cancel' without properly entering a valid filename. Yes, the users should pay closer attention to their entry and random clicking, but a preventative solution would be to autofill a default FileName, while still allowing the user to use the windows dialog to change directory selection or change the filename if desired.
    In this case, you can simulate pasting the path of that file. Or direct keyboard input.
    You can also hook api the pop-up dialog box and give him a pdf file path

  9. #49
    PowerPoster
    Join Date
    Jan 2020
    Posts
    4,070

    Re: Save PDF file with VBs Printer Object

    How to modify this code to run on 64 bits. Above VBA, twinbasic

Page 2 of 2 FirstFirst 12

Posting Permissions

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



Click Here to Expand Forum to Full Width