-
Jul 9th, 2024, 07:04 PM
#41
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.
-
Jul 10th, 2024, 02:46 AM
#42
Addicted Member
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
-
Jul 10th, 2024, 04:54 AM
#43
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.
-
Jul 10th, 2024, 11:18 AM
#44
Addicted Member
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)
-
Jul 11th, 2024, 05:23 PM
#45
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"
-
Jul 11th, 2024, 05:33 PM
#46
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.
-
Jul 11th, 2024, 05:39 PM
#47
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?
-
Jul 13th, 2024, 05:47 PM
#48
Re: Print to specific PDF file name
 Originally Posted by Jimboat
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
-
Jul 13th, 2024, 06:32 PM
#49
Re: Save PDF file with VBs Printer Object
How to modify this code to run on 64 bits. Above VBA, twinbasic
-
Dec 5th, 2024, 10:54 PM
#50
Hyperactive Member
Re: Save PDF file with VBs Printer Object
Printer object cannot write elements by coor.
-
Dec 6th, 2024, 01:01 AM
#51
Re: Save PDF file with VBs Printer Object
 Originally Posted by loquat
Printer object cannot write elements by coor.
What do you mean? I has CurrentX and CurrentY for setting the coordinates.
-
Jan 9th, 2025, 08:33 PM
#52
New Member
Re: Save PDF file with VBs Printer Object
 Originally Posted by xiaoyao
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"
This is of no use, and a restart of the print service may be required after the modification.
Attachment 193886
-
Mar 7th, 2025, 05:46 AM
#53
Addicted Member
Re: Save PDF file with VBs Printer Object
Hi!
Returning to this thread.
Like some user said before, if i compile the project with P-CODE, everything works fine, but for some reason i need now to compile to native code... and it crashes now.
The code is simple:
Code:
PrinterFilePath = "S:\Clientes\" & temp_Nombre_del_fichero
FPrincipal.InfoI.Caption = "Imprimiendo Datareport"
FPrincipal.InfoI.Refresh
Informe_ECI.PrintReport False, rptRangeAllPages
FPrincipal.InfoI.Caption = "Devolviendo API"
FPrincipal.InfoI.Refresh
PrinterFilePath = ""
Just when executing Printerfilepath = "", program closes. in IDE it works fine, but no compiled.
Any idea about what to do? (my knowledge with APis is so limited :-( )
-
Mar 7th, 2025, 05:53 AM
#54
Re: Save PDF file with VBs Printer Object
I just tested the sample project that is in the first post compiled to native code and it worked perfectly.
-
Mar 7th, 2025, 12:17 PM
#55
Re: Save PDF file with VBs Printer Object
 Originally Posted by Eduardo-
Hello, it is possible to use the Printer object to save PDF files with the Microsoft Print to PDF driver and without user interaction.
HTH
HOW TO chage for x64 twinbasic use it?
-
Mar 7th, 2025, 12:36 PM
#56
Addicted Member
Re: Save PDF file with VBs Printer Object
I think you are right, sorry!, i used a lot of "msgbox" to see where is really crashing, and it's just after:
Informe_ECI.PrintReport False, rptRangeAllPages
it's not arriving to printerfilepath = ""....
I hate the datareports..., let's see why they don't work if i change the compilation mode...
-
Mar 7th, 2025, 08:25 PM
#57
Re: Save PDF file with VBs Printer Object
when vba call word.print,how to change pdf save path?
on vb6 use “word.application",document.print ,how to change for pdf savepath?
-
Mar 7th, 2025, 08:27 PM
#58
Re: Save PDF file with VBs Printer Object
[QUOTE=wtusvi;5667319]This is of no use, and a restart of the print service may be required after the modification.
maybe can refresh Registry by windows api?
-
Mar 7th, 2025, 10:45 PM
#59
Re: Save PDF file with VBs Printer Object
 Originally Posted by xiaoyao
HOW TO chage for x64 twinbasic use it?
It's not necessary. twinBASIC supports setting the path natively, just set Printer.OutputFile.
Edit: But I did write basic 64bit API hooking code based on the functions in this thread... I changed to hijacking the MsgBox but the code has the same generic ReplaceFunction and RestoreFunction...
https://gist.github.com/fafalone/635...8484e516f5f205
Last edited by fafalone; Mar 8th, 2025 at 09:51 AM.
-
Mar 8th, 2025, 08:18 AM
#60
Addicted Member
Re: Save PDF file with VBs Printer Object
Solved my problem.
It was the datareport.
Changed all the datareports to PRINT (yes... a lot of work, but now everything works fine)
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|