PDA

View Full Version : EXCEL VBA StartDoc API Function



rs123
06-09-2017, 04:23 AM
Dear All,

I am working on macro where I print selected pdf files using excel vba macro. I have managed to set up default printer using setdefaultprinter api and then used shellexecute api to send files to printer however the issue is I need those files printed in order they have been sent but it is not happening and the order is getting messed up when the pages comes out of printer. I tried giving 3 seconds delay before sending each file to printer which worked however I need more robust solution to do this without adding any delay.

One solution is may be to use StartDoc API function rather than shellexecute can someone help me with how to declare startdoc function in VBA?

Regards,

Leith Ross
06-09-2017, 10:01 AM
Hello rs123,

I am assuming you have experience using the Windows API. Here is the API code for Windows Vista and later.


Type DOCINFO
cbSize As Long
lpszDocName As String
lpszOutput As String
lpszDatatype As String
fwType As Long
End Type


Declare PtrSafe Function StartDoc Lib "gdi32" Alias "StartDocA" (ByVal hdc As LongPtr, lpdi As DOCINFO) As Long

rs123
06-10-2017, 01:30 AM
Thank you for your response Leith. Please see my code below where I am using shellexecute api function. Here "myrange" has first column with paths and the second column with file names and I use following code to print documents at specific path in the order it has been listed in "myrange" but somehow the order gets messed up when printer prints the documents. do you have any suggestion in terms of doing this right way so that documents comes out in order?

i=1

for each rng in myrange
print rng.rows(i).columns(1) & rng.rows(i).columns(2)
i = i+1
next rng

function print(byval str as string)
shellexecute(activeprinter, "Print", str,vbnullstring, vbnullstring,0)
end function

mdmackillop
06-10-2017, 03:05 AM
Have you tried this (https://social.technet.microsoft.com/Forums/windowsserver/en-US/83c81106-f585-499d-a074-6e178c504879/jobs-in-pinter-queue-not-printing-in-job-order?forum=winserverprint)?
On that print queue look at the advanced tab on the properties. At the very bottom uncheck Print Spooled Documents first.

rs123
06-10-2017, 06:02 AM
the printer I am talking about is a network printer at my work place so I guess this needs to be done by the desktop support team who can change settings. The printer is also either HP or RICOH printer I am talking about. I will try to get the help and see if this technique works on monday.

rs123
06-11-2017, 10:28 AM
"On that print queue look at the advanced tab on the properties. At the very bottom uncheck Print Spooled Documents first."

Is there a way to do this through excel vba program?