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 StringCollectio n GetPrintersColl ection()
{
StringCollectio n printerNameColl ection = new StringCollectio n();
string searchQuery = "SELECT * FROM Win32_Printer";
ManagementObjec tSearcher searchPrinters =
new ManagementObjec tSearcher(searc hQuery);
ManagementObjec tCollection printerCollecti on = searchPrinters. Get();
foreach(Managem entObject printer in printerCollecti on)
{
printerNameColl ection.Add(prin ter.Properties["Name"].Value.ToString ());
}
return printerNameColl ection;
}
Using this printer name, we can fetch the print job collection by using the
following method:
public static StringCollectio n GetPrintJobsCol lection(string printerName)
{
StringCollectio n printJobCollect ion = new StringCollectio n();
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*/
ManagementObjec tSearcher searchPrintJobs =
new ManagementObjec tSearcher(searc hQuery);
ManagementObjec tCollection prntJobCollecti on = searchPrintJobs .Get();
foreach(Managem entObject prntJob in prntJobCollecti on)
{
System.String jobName = prntJob.Propert ies["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(s plitArr)[0];
string documentName = prntJob.Propert ies["Document"].Value.ToString ();
if(String.Compa re(prnterName, printerName, true) == 0)
{
printJobCollect ion.Add(documen tName);
}
}
return printJobCollect ion;
}
The query "SELECT * FROM Win32_PrintJob" can also be used as "SELECT * FROM
Win32_PrintJob WHERE Name like '"+ printerName.Rep lace("\", "\\") +"%'". 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(s tring printerName, int printJobID)
{
bool isActionPerform ed = false;
string searchQuery = "SELECT * FROM Win32_PrintJob" ;
ManagementObjec tSearcher searchPrintJobs =
new ManagementObjec tSearcher(searc hQuery);
ManagementObjec tCollection prntJobCollecti on = searchPrintJobs .Get();
foreach(Managem entObject prntJob in prntJobCollecti on)
{
System.String jobName = prntJob.Propert ies["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(s plitArr)[0];
int prntJobID = Convert.ToInt32 (jobName.Split( splitArr)[1]);
string documentName = prntJob.Propert ies["Document"].Value.ToString ();
if(String.Compa re(prnterName, printerName, true) == 0)
{
if(prntJobID == printJobID)
{
prntJob.InvokeM ethod("Pause", null);
isActionPerform ed = true;
break;
}
}
}
return isActionPerform ed;
}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.InvokeM ethod("Resume", null);Win32_Pri ntJob 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 isActionPerform ed = false;
string searchQuery = "SELECT * FROM Win32_PrintJob" ;
ManagementObjec tSearcher searchPrintJobs =
new ManagementObjec tSearcher(searc hQuery);
ManagementObjec tCollection prntJobCollecti on = searchPrintJobs .Get();
foreach(Managem entObject prntJob in prntJobCollecti on)
{
System.String jobName = prntJob.Propert ies["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(s plitArr)[0];
int prntJobID = Convert.ToInt32 (jobName.Split( splitArr)[1]);
string documentName = prntJob.Propert ies["Document"].Value.ToString ();
if(String.Compa re(prnterName, printerName, true) == 0)
{
if(prntJobID == printJobID)
{
//performs a action similar to the cancel
//operation of windows print console
prntJob.Delete( );
isActionPerform ed = true;
break;
}
}
}
return isActionPerform ed;
}
HTH
--
Bob
--------------------------------------
I'll have a B please Bob.
"Michael Beck" <Mi***********@ pacbell.net> wrote in message
news:ea******** ******@TK2MSFTN GP15.phx.gbl...
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