Shane Russell
asked on
Printer Management
Found this site here on code project
http://www.codeproject.com/KB/printing/prntjobcontrollerusingwmi.aspx
Which shows you how to do the relevant things I am after in C# however would it be possible to get the equivelant functions in vb dot net for 2008 ?
As I would like to have a play around with doing what they have done but in vb dot net.
I have never touched C# - at least not to the extent of WMI or anything like that so would be great if someone can port the code for me.
Thanks!!!
http://www.codeproject.com/KB/printing/prntjobcontrollerusingwmi.aspx
Which shows you how to do the relevant things I am after in C# however would it be possible to get the equivelant functions in vb dot net for 2008 ?
As I would like to have a play around with doing what they have done but in vb dot net.
I have never touched C# - at least not to the extent of WMI or anything like that so would be great if someone can port the code for me.
Thanks!!!
There are also code translaters out there paste the c# code in here and it does a pretty good job of making the vb equivalient.
http://www.carlosag.net/Tools/CodeTranslator/
http://www.carlosag.net/Tools/CodeTranslator/
ASKER
Tried that translator which I could not get to work - but found another one which gave me the following code - which is not working.
The data I am passing to the function is the printer name which I am getting from the printers form like so
Jobs.strName = lstPrinters.SelectedItems( 0).SubItem s(0).Text
The print job ID I am assuming is the Integer Index Value of the Item in the listview I want to cancel and I am getting that value like the 2nd chunk of code below which I have made a public variable so I can call as per the 3rd chunk of code and I put a label on the Jobs form and assigned the lblIndex.Text the same information just to make sure that the information is being passed correctly.
Is the code wrong or am I passing the wrong info into the function or what exactly ?
The cancell all jobs works fine but for some reason I cant seem to cancel individual jobs
The data I am passing to the function is the printer name which I am getting from the printers form like so
Jobs.strName = lstPrinters.SelectedItems(
The print job ID I am assuming is the Integer Index Value of the Item in the listview I want to cancel and I am getting that value like the 2nd chunk of code below which I have made a public variable so I can call as per the 3rd chunk of code and I put a label on the Jobs form and assigned the lblIndex.Text the same information just to make sure that the information is being passed correctly.
Is the code wrong or am I passing the wrong info into the function or what exactly ?
The cancell all jobs works fine but for some reason I cant seem to cancel individual jobs
Public Function CancelPrintJob(ByVal printerName As String, ByVal printJobID As Integer) As Boolean
Dim isActionPerformed As Boolean = False
Dim searchQuery As String = "SELECT * FROM Win32_PrintJob"
Dim searchPrintJobs As New ManagementObjectSearcher(searchQuery)
Dim prntJobCollection As ManagementObjectCollection = searchPrintJobs.[Get]()
For Each prntJob As ManagementObject In prntJobCollection
Dim jobName As System.String = prntJob.Properties("Name").Value.ToString()
'Job name would be of the format [Printer name], [Job ID]
Dim splitArr As Char() = New Char(0) {}
splitArr(0) = Convert.ToChar(",")
Dim prnterName As String = jobName.Split(splitArr)(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Split(splitArr)(1))
Dim documentName As String = prntJob.Properties("Document").Value.ToString()
If [String].Compare(prnterName, printerName, True) = 0 Then
If prntJobID = printJobID Then
'performs a action similar to the cancel
'operation of windows print console
prntJob.Delete()
isActionPerformed = True
Exit For
End If
End If
Next
Return (isActionPerformed)
End Function
' Code to
Public intIndex As Integer
Private Sub lstJobs_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstJobs.MouseClick
Dim hti As ListViewHitTestInfo = lstJobs.HitTest(e.Location)
intIndex = hti.Item.Index
End Sub
'Called like so
Private Sub btnCancelS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelS.Click
CancelPrintJob(strName, intIndex)
lblIndex.Text = strName & " " & intIndex
End Sub
ASKER
No I am trying to cancel individual print jobs that has nothing to do with that - thanks for the links though
If you read the second link with a bit more attention, it has the same loop as your code above
Dim mo As ManagementObject
For Each mo In queryCollection1
Console.WriteLine(("Printe r Driver : " + mo("DriverName").ToString( )))
Console.WriteLine(("Docume nt Name : " + mo("Document").ToString()) )Console.W riteLine(( "Document Owner : " + mo("Owner").ToString()))Co nsole.Writ eLine("=== ========== ========== ========== ========== ")
Next mo
And following code could be rewritten
Dim splitArr As Char() = New Char(0) {}
splitArr(0) = Convert.ToChar(",")
Dim prnterName As String = jobName.Split(splitArr)(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp lit(splitA rr)(1))
Dim documentName As String = prntJob.Properties("Docume nt").Value .ToString( )
If [String].Compare(prnterNam e, printerName, True) = 0
as
Dim prnterName As String = jobName.Split(",")(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp lit(",")(1 ))
Dim documentName As String = prntJob.Properties("Docume nt").Value .ToString( )
If prnterName.tolower = printerName.tolower then
Dim mo As ManagementObject
For Each mo In queryCollection1
Console.WriteLine(("Printe
Console.WriteLine(("Docume
Next mo
And following code could be rewritten
Dim splitArr As Char() = New Char(0) {}
splitArr(0) = Convert.ToChar(",")
Dim prnterName As String = jobName.Split(splitArr)(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp
Dim documentName As String = prntJob.Properties("Docume
If [String].Compare(prnterNam
as
Dim prnterName As String = jobName.Split(",")(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp
Dim documentName As String = prntJob.Properties("Docume
If prnterName.tolower = printerName.tolower then
ASKER
I must be missing the point but that is writing to the console I dont see any code in there that cancels the print jobt that is selected from a listview ?
That is the code written in VB.NET which takes you to the edge of the solution. You have to add your own bits to that code like the one i posted above to get it do what you want.
ASKER
I dont know what those bits of code are hence the question as I tried converting the code and that did not work -
Any suggestions on the code ?
Any suggestions on the code ?
Did you try the modified code i have posted?
Dim prnterName As String = jobName.Split(",")(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp lit(",")(1 ))
Dim documentName As String = prntJob.Properties("Docume nt").Value .ToString( )
If prnterName.tolower = printerName.tolower then
If prntJobID = printJobID Then
prntJob.Delete()
isActionPerformed = True
Exit For
End If
End If
Dim prnterName As String = jobName.Split(",")(0)
Dim prntJobID As Integer = Convert.ToInt32(jobName.Sp
Dim documentName As String = prntJob.Properties("Docume
If prnterName.tolower = printerName.tolower then
If prntJobID = printJobID Then
prntJob.Delete()
isActionPerformed = True
Exit For
End If
End If
ASKER
Not yet - will try the snippet of code you posted soon.
Thanks for all the help and fast replies.
Thanks for all the help and fast replies.
ASKER
Tried that and it did not work - Please find all the source code etc attached in the zip file.
Most likely very messy and there are some chunks that are most likely not needed - is in visual basic 2008 express.
Any tidying of code or anything you can do to get it working would be great.
Thanks!!!
Most likely very messy and there are some chunks that are most likely not needed - is in visual basic 2008 express.
Any tidying of code or anything you can do to get it working would be great.
Thanks!!!
ASKER
the EE file attachment would not allow me to attach it because of the SLN file extension so please find the zip download here
http://www.2shared.com/file/5965705/9364a6d0/Printers.html
You will have to scroll to the bottom of that page and there should be a link to save the file to your pc.
http://www.2shared.com/file/5965705/9364a6d0/Printers.html
You will have to scroll to the bottom of that page and there should be a link to save the file to your pc.
ASKER
any updates??
Could you not test the code yourself and see what happens? Post any errors here.
ASKER
I am getting a lot of code suggestions and I decided to post what I have currently ( from a week ago ) and I have tried all the code suggestions - I think I wanted the delete button ( as in select one print job ) and it cancel only the selected print job - not all of them.
I could not get this to work hence the question and hence posting the attached code.
I tried what was suggested so far but still no luck.
If someone could just get the delete selected job working I would be happy with that.
Thanks
I could not get this to work hence the question and hence posting the attached code.
I tried what was suggested so far but still no luck.
If someone could just get the delete selected job working I would be happy with that.
Thanks
In my code above, you see following block
If prntJobID = printJobID Then
prntJob.Delete()
isActionPerformed = True
Exit For
End If
All you need to do is populate the printJobID variable with the selected job ID and it should do the trick.
If prntJobID = printJobID Then
prntJob.Delete()
isActionPerformed = True
Exit For
End If
All you need to do is populate the printJobID variable with the selected job ID and it should do the trick.
ASKER
so instead of having
If prntJobID = printJobID Then
I would have
If prntJobID =Selected_ID_From_List_Vie w Then
code here to delete job ?
If prntJobID = printJobID Then
I would have
If prntJobID =Selected_ID_From_List_Vie
code here to delete job ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help although it took a long time to get there ( most likely my fault ) as not brilliant with coding.
http://www.devcity.net/PrintArticle.aspx?ArticleID=144
Gary