-
Dec 15th, 2006, 04:02 PM
#1
[RESOLVED] Printer Document Name
When I use API, I can do this:
VB Code:
Private Type DOCINFO
cbSize As Long
lpszDocName As String
lpszOutput As String
lpszDatatype As String
fwType As Long
End Type
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As Long, lpdi As DOCINFO) As Long
Private Declare Function EndDoc Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function StartPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPage Lib "gdi32" (ByVal hdc As Long) As Long
Private Function PrintTest(ByVal hPrintDc As Long)
Dim Result As Long
Dim di As DOCINFO 'Structure for Print Document info
di.cbSize = 20 ' Size of DOCINFO structure
di.lpszDocName = "Document Name: 1234567..."
Result = StartDoc(hPrintDc, di) ' Start a new print document
'...... the rest of the code...
End Function
It will print on the printer with a document name, but when I don't use API, how can I set the document name ?
For example:
VB Code:
Private Function PrintTest()
[COLOR=Red][B]Printer.DocumentName = "Document Name: 1234567..."[/B][/COLOR]
Printer.Print "testing..."
Printer.EndDoc
End Function
The printer does not have a DocumentName property, so I cannot do that, but how else ?
Can I mix the 2, and set the document name using api, but print on the printer object ?
Last edited by CVMichael; Dec 15th, 2006 at 04:38 PM.
-
Dec 15th, 2006, 06:16 PM
#2
Re: Printer Document Name
Why not just use the API?
-
Dec 15th, 2006, 06:21 PM
#3
Hyperactive Member
Re: Printer Document Name
Originally Posted by MartinLiss
Why not just use the API?
Smaller, less bloated code? Beats me...
-
Dec 15th, 2006, 07:33 PM
#4
Re: Printer Document Name
Originally Posted by MartinLiss
Why not just use the API?
Because all the code (like 500 lines at least) is written for the Printer object, all the drawing and the text, etc... If I have to change everything to API, it will be way too much to change...
All I need to do, is to be able to give it a name...
-
Dec 15th, 2006, 07:46 PM
#5
Re: Printer Document Name
Well this works.
VB Code:
Private Function PrintTest(ByVal hPrintDc As Long)
Dim Result As Long
Dim di As DOCINFO 'Structure for Print Document info
di.cbSize = 20 ' Size of DOCINFO structure
di.lpszDocName = "Document Name: 1234567..."
[HL="#FFFF80"]Printer.Print di.lpszDocName[/HL]
Result = StartDoc(hPrintDc, di) ' Start a new print document
Result = EndDoc(hPrintDc)
'...... the rest of the code...
End Function
-
Dec 15th, 2006, 08:30 PM
#6
Re: Printer Document Name
Originally Posted by MartinLiss
Well this works.
VB Code:
Private Function PrintTest(ByVal hPrintDc As Long)
Dim Result As Long
Dim di As DOCINFO 'Structure for Print Document info
di.cbSize = 20 ' Size of DOCINFO structure
di.lpszDocName = "Document Name: 1234567..."
[HL="#FFFF80"]Printer.Print di.lpszDocName[/HL]
Result = StartDoc(hPrintDc, di) ' Start a new print document
Result = EndDoc(hPrintDc)
'...... the rest of the code...
End Function
I did not test it, but i'm sure that that will just print the document name on the paper....
I need to set the document name, the document that goes to the printer.
Not what it's on paper, but the paper's name....
-
Dec 15th, 2006, 08:46 PM
#7
Re: Printer Document Name
Originally Posted by CVMichael
I did not test it, but i'm sure that that will just print the document name on the paper....
I need to set the document name, the document that goes to the printer.
Not what it's on paper, but the paper's name....
Yes, that's all it does.
-
Dec 15th, 2006, 09:40 PM
#8
Re: Printer Document Name
Maybe I should explain this a little more for what I need it like this:
2 reasons:
First: You know those really big printers that are photocopier, scanner, etc, like this one:
We have one of those at work, and the printer has a log. When I print from my program without using API, the document name in the log it just shows the application name, but I need it to show whatever i'm actually printing...
Second reason: I'm printing to a PDF Creator (a program that takes the print-out, and converts it to PDF), the name of the PDF file name is taken from the Document Name, so if I print many things, it will have the same name (with a time stamp in the file name also), but that's not good enough because if I print in a very short period of time (a few seconds), then I won't be able to tell what file is for what document.
I know that a way to solve this is to print then wait until the file is created, rename it, then print again, wait... and so on, but that takes a lot of time when printing many documents.
If I have to convert to API, well, lets just say that it took me like 3 days to get it to work with the printer object (the printout is very complex), converting to API will take me too long...
I just need a simple thing... to set the document name
-
Dec 16th, 2006, 12:40 AM
#9
Re: Printer Document Name
i have found some sample code from microsoft that maybe can do what you want
VB Code:
Private Sub Command1_Click()
' Combine API Calls with the Printer object
Dim OutString As String
Dim lf As LOGFONT
Dim result As Long
Dim hOldfont As Long
Dim hPrintDc As Long
Dim hFont As Long
Printer.Print "Printer Object"
hPrintDc = Printer.hdc
OutString = "Hello World"
lf.lfEscapement = 1800
lf.lfHeight = (DESIREDFONTSIZE * -20) / Printer.TwipsPerPixelY
hFont = CreateFontIndirect(lf)
hOldfont = SelectObject(hPrintDc, hFont)
result = TextOut(hPrintDc, 1000, 1000, OutString, Len(OutString))
result = SelectObject(hPrintDc, hOldfont)
result = DeleteObject(hFont)
Printer.Print "xyz"
Printer.EndDoc
End Sub
This will print a single page to the current default printer with "Printer Object" and "xyz" printed normally and "Hello World" printed rotated 180 degrees.
if you want the rest of the article http://support.microsoft.com/kb/175535 but the above is all that is relevant
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 16th, 2006, 12:44 AM
#10
Re: Printer Document Name
He wants to grab and print the print document name.
-
Dec 16th, 2006, 01:15 AM
#11
Re: Printer Document Name
i realised that this is not what he actually wants to do, but the above is a method of combining the printer object and API calls, which is what he wants to do
He wants to grab and print the print document name
not print the name, set the name going to the printer manager
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 18th, 2006, 12:27 PM
#12
Re: Printer Document Name
Bump...
Still did not find a solution for this
-
Dec 18th, 2006, 12:35 PM
#13
Re: Printer Document Name
Please don't bump your threads.
-
Dec 18th, 2006, 01:07 PM
#14
Re: Printer Document Name
Originally Posted by MartinLiss
Please don't bump your threads.
It's been 2 days since last post on this thread, it's not like I bumped after a few hours, and this is the first time I bumped on this thread...
I think it's fair to let everyone know that i'm still looking for an anser to this
-
Dec 18th, 2006, 01:12 PM
#15
Re: Printer Document Name
What is "fair" is to give everyone the same chance at getting their question answered. If we were all to compete for the top spot or even for the first page it would be a real mess.
-
Dec 18th, 2006, 01:13 PM
#16
Re: Printer Document Name
Send a PM to Merri - he apparently has a large library of printer tools he has created.
-
Dec 19th, 2006, 09:26 AM
#17
Re: Printer Document Name
Finally !!!
After 4 days of searching, this is the thread that helped me:
http://www.vbforums.com/showthread.php?t=258243
-
Dec 22nd, 2006, 08:12 AM
#18
Fanatic Member
Re: [RESOLVED] Printer Document Name
CVMichael,
Could you possibly post a small project that shows how to do this. Or point me in the direction. This questions been around for a while. Glad it seems to have just been solved.
I don't know what an API is, don't know if how to use a class module, don't know how to use the dll (and the link doesn't work), and I can't even find where the code :
Dim prJob as New ApiPrintJob
prJob.DocumentName = "A catchy name"
is located in Merrions web site. Which seems to be the guts of it.
-
Dec 22nd, 2006, 09:19 AM
#19
Re: [RESOLVED] Printer Document Name
First go into the PDFCreator Options, and make sure in the Auto-Save you have PDF, set a file name format, then set the directory to save into.
When you print in VB, just set the default printer to PDFCreator, and print to it...
Something like this:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim P As Printer
For Each P In Printers
If P.DeviceName = "PDFCreator" Then
Set Printer = P
Exit For
End If
Next P
Printer.FontSize = 50
Printer.Print "Hello world !!"
Printer.EndDoc
End Sub
That will create a file in the directory you specified in the options, named something like "Microsoft Visual Basic, 20061222090914.pdf"
Now if you want to name it diferently, other than "Microsoft Visual Basic", things get a little more complicated.
First search for EventVB_I.dll in the UtilityBank (here), download it (compile the project if neccesary), and put the DLL in C:\Windows\System32
In your project go to reference, and add the DLL in the list, it should look like this:
After that, you just have to use the code that Merrion posted in post #7
So, the end result is:
VB Code:
Option Explicit
Dim WithEvents vbLink As APIFunctions
Private Sub Form_Load()
Dim P As Printer
Set vbLink = New APIFunctions
For Each P In Printers
If P.DeviceName = "PDFCreator" Then
Set Printer = P
Exit For
End If
Next P
Printer.FontSize = 50
Printer.Print "Hello world !!"
RenameJob "Microsoft Visual Basic", "Testing save to PDF file"
DoEvents
Printer.EndDoc
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set vbLink = Nothing
End Sub
Public Sub RenameJob(ByVal OldName As String, ByVal NewName As String)
Dim vbPrinter As ApiPrinter
Dim vbJob As ApiPrintJob
Set vbPrinter = New ApiPrinter
vbPrinter.DeviceName = Printer.DeviceName
For Each vbJob In vbPrinter.PrintJobs
If vbJob.DocumentName = OldName Then
vbJob.DocumentName = NewName
Exit For
End If
Next vbJob
End Sub
Last edited by CVMichael; Dec 22nd, 2006 at 12:15 PM.
-
Jan 5th, 2007, 06:05 AM
#20
Junior Member
Re: [RESOLVED] Printer Document Name
I have a problem with the PDFCreator. The function Merrion made works fine, i have a breakpoint just before the Printer.EndDoc and i can see that the document name has changed, but when the PDFCreator window appears, it still have the "Microsoft Visual Basic" name. What could be the problem? Its a problem with VB code or with the PDFCreator?
Last edited by Zerodyme; Jan 5th, 2007 at 06:22 AM.
-
Jan 5th, 2007, 06:13 AM
#21
Re: [RESOLVED] Printer Document Name
i think pdf creator is called as soon as the first page is completed, so the document name would be taken from that point
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jan 5th, 2007, 08:04 AM
#22
Re: [RESOLVED] Printer Document Name
Don't change the name just before the Printer.EndDoc, change it as soon as you start to write on the printer, you can even do something like this:
Pseudocode:
VB Code:
' Select the printer
Printer.Print "" ' first call initializes the printer
DoEvents ' I don't think DoEvents is really neccessary, but just in case...
RenameJob "Microsoft Visual Basic", "Testing save to PDF file" ' Rename document
Printer.CurrentY = 0
Printer.CurrentX = 0
Printer.Print "Hello world !" ' print whaterver you wanted to print...
Printer.EndDoc
-
Jan 7th, 2007, 08:30 PM
#23
Fanatic Member
Re: [RESOLVED] Printer Document Name
Zerodyme, Did you ever solve your problem?
I want the save as file dialog box to show up to allow an alternative path and filename.
I've found that there's a bit of a Catch 22. Hope someone can give me a clue on how to get around it.
This dialogbox is initiated at the first thing that is sent to the printer eg Printer.Print "Hello world !". This first line also sets up the first vbPrinter.PrintJobs. Without a vbPrinter.PrintJobs value defined the RenameJob routine doesn't work as it skips the rename.
VB Code:
For Each vbJob In vbPrinter.PrintJobs
If vbJob.DocumentName = OldName Then
vbJob.DocumentName = NewName
Exit For
End If
Next vbJob
So, I can't rename the job before the saveas dialog appears as RenameJob must be placed after the printer is initialised.
Anyone have any ideas? Desperately need help.
Last edited by sgrya1; Jan 7th, 2007 at 08:46 PM.
-
Jan 7th, 2007, 10:43 PM
#24
Fanatic Member
Re: [RESOLVED] Printer Document Name
I don't surpose anyone has an idea on where the string variable containing "Microsoft Visual Basic" (or the program executable title) is stored? If this were able to be changed then...
-
Jan 8th, 2007, 02:45 AM
#25
Junior Member
Re: [RESOLVED] Printer Document Name
ok, i have resolved my problem. i did what CVMichael told me and almost worked, but i had another problem. in my changename function i also wanted to change the username of the file, and that caused me problems, so i removed it and now it works fine. thanks a lot!
VB Code:
Public Sub ChangeName(ByVal new_name As String)
Dim vbPrinter As ApiPrinter
Dim vbJob As ApiPrintJob
Set vbPrinter = New ApiPrinter
vbPrinter.DeviceName = Printer.DeviceName
For Each vbJob In vbPrinter.PrintJobs
'"Microsoft Visual Basic" when debugging the program and "Mosaicos" for the compiled .exe
If vbJob.DocumentName = "Microsoft Visual Basic" Or vbJob.DocumentName = "Mosaicos" Then
vbJob.DocumentName = new_name
'vbJob.UserName = client_name
Exit For
End If
Next vbJob
End Sub
Last edited by Zerodyme; Jan 8th, 2007 at 03:01 AM.
-
Jan 8th, 2007, 11:32 AM
#26
Re: [RESOLVED] Printer Document Name
VB Code:
'vbJob.UserName = client_name
This will throw an error if the user is not known to the server or if that user does not have sufficient access rights. If you just want a different user to be notified of the job progress you need to set the .Notifyuser property instead.
-
Jan 10th, 2007, 05:50 PM
#27
Fanatic Member
Re: [RESOLVED] Printer Document Name
Is there a way to disable then reenablee the "Save As" dialog box for all printers in general? I specifically want to disable it for PDF writers but I guess that it needs general regardless of printer types as I won't know the name given to specifiv PDF writers.
I just don't want to have to ask users to download PDFCreator and run them through the steps required to allow saving without the dialog box.
-
Jan 10th, 2007, 07:09 PM
#28
Re: [RESOLVED] Printer Document Name
The source code (in VB) for PDFCreator is available, so you can use (and customise) parts of it in your project if you want.
-
Jul 3rd, 2015, 05:15 PM
#29
Registered User
Re: [RESOLVED] Printer Document Name
Originally Posted by sgrya1
I don't surpose anyone has an idea on where the string variable containing "Microsoft Visual Basic" (or the program executable title) is stored? If this were able to be changed then...
Bit of a Late reply here
But you can just set app.title = "Job Name you Want" before you start the print routine.
Much easier
-
Jul 4th, 2015, 06:45 AM
#30
Re: [RESOLVED] Printer Document Name
Welcome to the forum!
Yes - really late reply!
Your answer does not talk to the point of the thread at all. It answers a silly question that had nothing to do with the thread.
-
Jul 4th, 2015, 09:39 AM
#31
Re: [RESOLVED] Printer Document Name
Originally Posted by szlamany
Welcome to the forum!
Yes - really late reply!
Your answer does not talk to the point of the thread at all. It answers a silly question that had nothing to do with the thread.
I think it does...
Why don't you check it out yourself - I just did so,
placing the following in a new StdExe-Project:
Code:
Option Explicit
Private Sub Form_Click()
Dim OldTitle As String
OldTitle = App.Title
App.Title = "Job Name you Want"
Printer.Print "Hollow void"
Printer.EndDoc
App.Title = OldTitle
End Sub
Then I've compiled this little Executable - started the Binary -
clicked the Form - and what was popping up (a PDF-Printer
being my default here) was:
As you see, the Printer-API which works underneath of VBs Printing-Functionality,
will take over the current App.Title as the Document-Name for the PrintJob in question.
That was all haydos was saying...
(it's a workaround for the problem, known for years - but wasn't mentioned in this thread here)
Olaf
-
Jul 4th, 2015, 09:41 AM
#32
Re: [RESOLVED] Printer Document Name
Changing the title of the app to get a print job name might work - certainly not something I would do.
The thread, imo, is about getting a PDF driver to pickup a job name.
-
Jul 4th, 2015, 09:47 AM
#33
Re: [RESOLVED] Printer Document Name
Originally Posted by szlamany
The thread, imo, is about getting a PDF driver to pickup a job name.
And the PDF-Driver in question does this just fine, when you look at the ScreenShot I've posted.
Olaf
-
Jul 4th, 2015, 10:10 AM
#34
Re: [RESOLVED] Printer Document Name
Here's another variant, which doesn't wait with Resetting the App-Title
until the whole Document was printed (encapsulated in a small Routine,
which only performs a very fast "Printer-No-Op" and then returns):
Code:
Option Explicit
Private Sub Form_Click()
Printer_StartDoc "Another Job-Name"
Printer.Print "Hollow void"
Printer.EndDoc
End Sub
Sub Printer_StartDoc(DocName As String)
Dim OldTitle As String
OldTitle = App.Title
App.Title = DocName
Printer.Print " "; 'do a No-Op on the Printer, to init the Document
Printer.CurrentX = 0 'reset the Cursor (due to the Space we printed)
App.Title = OldTitle 'reset the App.Title to its former value
End Sub
Olaf
-
Jul 10th, 2015, 09:55 AM
#35
Re: [RESOLVED] Printer Document Name
Nice !
9 years late though
-
May 21st, 2017, 07:59 AM
#36
Addicted Member
Re: [RESOLVED] Printer Document Name
[QUOTE=Schmidt;4901139]Here's another variant, which doesn't wait with Resetting the App
This works perfectly! Seems to work great for all OS's except for Win10? Any reason why the method doesn't work for Win10? Any easy fix?
-
May 21st, 2017, 03:05 PM
#37
Re: [RESOLVED] Printer Document Name
Originally Posted by Jimboat
...Seems to work great for all OS's except for Win10? Any reason why the method doesn't work for Win10? Any easy fix?
Maybe it is just a "timing"-issue ... meaning the App.Title might have to "stand there" a bit longer...
A short test, where you comment out the last line which resets to the old, previous title should help
to determine whether this is a timing-thing or a general problem on Win10.
Olaf
-
May 30th, 2017, 03:03 PM
#38
Addicted Member
Re: [RESOLVED] Printer Document Name
Originally Posted by Schmidt
Maybe it is just a "timing"-issue ... meaning the App.Title might have to "stand there" a bit longer...
A short test, where you comment out the last line which resets to the old, previous title should help
to determine whether this is a timing-thing or a general problem on Win10. /Olaf
Olaf - thanks for the suggestion. I tried your test, but Win10 still doesn't pick up the app name.title at all (Win10 just takes to printer with name = "".
must be a Win10 problem. it'd be nice if we had a work-around for it?
-
Oct 26th, 2017, 09:03 AM
#39
Addicted Member
Re: [RESOLVED] Printer Document Name
Still can't get this to work. Does anyone know of a work-around?
-
Oct 27th, 2017, 11:56 AM
#40
Re: [RESOLVED] Printer Document Name
Originally Posted by Jimboat
Still can't get this to work. Does anyone know of a work-around?
Seems that the little trick over the App.Title is not working anymore on Win10...
So you will have to print per Win32-API then (StartDoc/EndDoc, StartPage/EndPage etc.), using only the Printer.hDC for rendering.
There's a few wrappers for that out there (either in form of a "PrinterObject"-encapsulation or in form of a "Printer-Dialogue" encapsulation).
For a fast check on Win10, you could use the EMF-based Printing-Encapsulation from vbRichClient5 (cReportDocument, cReportPage).
This allows, to build up your page(s) InMemory first - preview them on a given Form or PictureBox -
and then finally print them all in one go - to a Printer of your choice (from the VB6-Printers-Collection, including a Document-Title).
I've posted a small Demo-Zip (based on code from Wolfgang Wolff) in the following thread (in post #9):
http://www.vbforums.com/showthread.p...=1#post4906853
Let me know, how that works out with regards to document-titles on Win10...
Olaf
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
|