Tracking a print job

  • Thread starter Thread starter Michael Beck
  • Start date Start date
M

Michael Beck

I need to select one of about 15 printers, which I have been able to do.
Then I need to set that printer as the printer to use, run a Crystal Reports
reports, and track if/when the printing job completes.

Is there a book or article that can help me?

If it is fairly easy, can anyone sen me some code that will get me started.

TIA,

Mike
 
The below code I used when i did a similar thing once. It is in C# I know
but not difficult to translate to VB.Net OR put it in a class in your
solution and make it available to your VB.Net project. I can't post my
previous code because I don't have it anymore!


To get a list of printers:

public static StringCollection GetPrintersCollection()
{
StringCollection printerNameCollection = new StringCollection();
string searchQuery = "SELECT * FROM Win32_Printer";
ManagementObjectSearcher searchPrinters =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection printerCollection = searchPrinters.Get();
foreach(ManagementObject printer in printerCollection)
{
printerNameCollection.Add(printer.Properties["Name"].Value.ToString());
}
return printerNameCollection;
}


Using this printer name, we can fetch the print job collection by using the
following method:
public static StringCollection GetPrintJobsCollection(string printerName)
{
StringCollection printJobCollection = new StringCollection();
string searchQuery = "SELECT * FROM Win32_PrintJob";

/*searchQuery can also be mentioned with where Attribute,
but this is not working in Windows 2000 / ME / 98 machines
and throws Invalid query error*/
ManagementObjectSearcher searchPrintJobs =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
foreach(ManagementObject prntJob in prntJobCollection)
{
System.String jobName = prntJob.Properties["Name"].Value.ToString();

//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnterName = jobName.Split(splitArr)[0];
string documentName = prntJob.Properties["Document"].Value.ToString();
if(String.Compare(prnterName, printerName, true) == 0)
{
printJobCollection.Add(documentName);
}
}
return printJobCollection;
}


The query "SELECT * FROM Win32_PrintJob" can also be used as "SELECT * FROM
Win32_PrintJob WHERE Name like '"+ printerName.Replace("\", "\\") +"%'". But
the query with WHICH attribute caused problems in my system which was
running Windows 2000, but ran smooth in Windows XP machines. So currently, I
am making a loop through all the print jobs and identify print jobs for that
particular printer.

Now, let's see how to manage these print jobs. The print console provided by
Windows allows us to Pause, Resume and Cancel print jobs. It also allows to
set priority for a print job. Using WMI, we can perform Pause, Resume and
Cancel, but it doesn't provide any method for changing the priority level.



The following code depicts how to pause a print job:

public static bool PausePrintJob(string printerName, int printJobID)
{
bool isActionPerformed = false;
string searchQuery = "SELECT * FROM Win32_PrintJob";
ManagementObjectSearcher searchPrintJobs =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
foreach(ManagementObject prntJob in prntJobCollection)
{
System.String jobName = prntJob.Properties["Name"].Value.ToString();
//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnterName = jobName.Split(splitArr)[0];
int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
string documentName = prntJob.Properties["Document"].Value.ToString();
if(String.Compare(prnterName, printerName, true) == 0)
{
if(prntJobID == printJobID)
{
prntJob.InvokeMethod("Pause", null);
isActionPerformed = true;
break;
}
}
}
return isActionPerformed;
}By invoking the Pause method, we can pause the print job. Similar approach
is required for resuming the print job. Just we need to invoke the Resume
method.

prntJob.InvokeMethod("Resume", null);Win32_PrintJob WMI_CLASS provides
methods to pause and resume a print job. But it doesn't provide any method
for canceling the print job. To cancel the print job, you need to just
delete the management object.

public static bool CancelPrintJob(string printerName, int printJobID)
{
bool isActionPerformed = false;
string searchQuery = "SELECT * FROM Win32_PrintJob";
ManagementObjectSearcher searchPrintJobs =
new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
foreach(ManagementObject prntJob in prntJobCollection)
{
System.String jobName = prntJob.Properties["Name"].Value.ToString();
//Job name would be of the format [Printer name], [Job ID]
char[] splitArr = new char[1];
splitArr[0] = Convert.ToChar(",");
string prnterName = jobName.Split(splitArr)[0];
int prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]);
string documentName = prntJob.Properties["Document"].Value.ToString();
if(String.Compare(prnterName, printerName, true) == 0)
{
if(prntJobID == printJobID)
{
//performs a action similar to the cancel
//operation of windows print console
prntJob.Delete();
isActionPerformed = true;
break;
}
}
}
return isActionPerformed;
}
HTH
 
Back
Top