VBA to get toner from a selected printer on userform

malveiro

New Member
Joined
May 13, 2015
Messages
32
Hello

I have a vba userform with all the printers of my company (mono or colour printers) , and now i'm triyng to get the toner level ( only to display the remaining toner in percentage ) without no success
I've tried several approaches , i created a button Get toner , when i click , the 4 textboxes (if printer is colour ) created don't get populate with the information

The code i have is this

Private Sub GetToner_Click()
' Get the IP address from TextBox2
Dim ipAddress As String
ipAddress = TextBox2.Value

' Get the HTML content from the printer's web page
Dim htmlContent As String
htmlContent = GetHTMLContent("http://" & ipAddress)

' Extract toner levels from the HTML content
Dim tonerLevels() As String
tonerLevels = ExtractTonerLevels(htmlContent)

' Update the TextBoxes with toner levels
TextBox5.Value = tonerLevels(0) ' Cyan Toner Level
TextBox6.Value = tonerLevels(1) ' Magenta Toner Level
TextBox7.Value = tonerLevels(2) ' Yellow Toner Level
TextBox8.Value = tonerLevels(3) ' Black Toner Level
End Sub


Private Function ExtractTonerLevels(ByVal html As String) As String()
Dim doc As Object
Set doc = CreateObject("HTMLFile")
doc.body.innerHTML = html

Dim tonerLevels(0 To 3) As String

' Cyan Toner Level
Dim cyanElements As Object
Set cyanElements = doc.getElementsByTagName("b")
Dim i As Integer
For i = 0 To cyanElements.Length - 1
If cyanElements(i).innerText = "Cartucho ciano" Then
Dim cyanLevel As String
cyanLevel = cyanElements(i).ParentNode.NextSibling.getElementsByTagName("td")(0).getElementsByTagName("td")(0).Title
tonerLevels(0) = Mid(cyanLevel, 1, Len(cyanLevel) - 1)
Exit For
End If
Next i

' Magenta Toner Level
Dim magentaElements As Object
Set magentaElements = doc.getElementsByTagName("b")
For i = 0 To magentaElements.Length - 1
If magentaElements(i).innerText = "Cartucho magenta" Then
Dim magentaLevel As String
magentaLevel = magentaElements(i).ParentNode.NextSibling.getElementsByTagName("td")(0).getElementsByTagName("td")(0).Title
tonerLevels(1) = Mid(magentaLevel, 1, Len(magentaLevel) - 1)
Exit For
End If
Next i

' Yellow Toner Level
Dim yellowElements As Object
Set yellowElements = doc.getElementsByTagName("b")
For i = 0 To yellowElements.Length - 1
If yellowElements(i).innerText = "Cartucho Amarelo" Then
Dim yellowLevel As String
yellowLevel = yellowElements(i).ParentNode.NextSibling.getElementsByTagName("td")(0).getElementsByTagName("td")(0).Title
tonerLevels(2) = Mid(yellowLevel, 1, Len(yellowLevel) - 1)
Exit For
End If
Next i

' Black Toner Level
Dim blackElements As Object
Set blackElements = doc.getElementsByTagName("b")
For i = 0 To blackElements.Length - 1
If blackElements(i).innerText = "Cartucho Preto" Then
Dim blackLevel As String
blackLevel = blackElements(i).ParentNode.NextSibling.getElementsByTagName("td")(0).getElementsByTagName("td")(0).Title
tonerLevels(3) = Mid(blackLevel, 1, Len(blackLevel) - 1)
Exit For
End If
Next i

ExtractTonerLevels = tonerLevels
End Function


Function FindElementByTagNameAndAttribute(doc As Object, tagName As String, attributeName As String, attributeValue As String) As Object
' Find the first element in the document that matches the specified tag name and attribute
Dim elements As Object
Set elements = doc.getElementsByTagName(tagName)

Dim element As Object
For Each element In elements
If element.getAttribute(attributeName) = attributeValue Then
Set FindElementByTagNameAndAttribute = element
Exit Function
End If
Next element

' Return Nothing if no matching element is found
Set FindElementByTagNameAndAttribute = Nothing
End Function


Private Function GetElementByInnerText(ByVal elements As Object, ByVal innerText As String) As Object
Dim i As Integer
For i = 0 To elements.Length - 1
If elements(i).innerText = innerText Then
Set GetElementByInnerText = elements(i)
Exit Function
End If
Next i
Set GetElementByInnerText = Nothing
End Function

Private Function GetTonerLevelFromElement(ByVal element As Object) As String
Dim tableElement As Object
Set tableElement = element.parentElement.parentElement.parentElement.getElementsByTagName("table")(1)
If Not tableElement Is Nothing Then
GetTonerLevelFromElement = Mid(tableElement.Rows(1).Cells(0).Title, 1, Len(tableElement.Rows(1).Cells(0).Title) - 1)
Else
GetTonerLevelFromElement = ""
End If
End Function





Private Function GetRegExpMatch(ByVal inputString As String, ByVal pattern As String) As Object
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")

regex.pattern = pattern
regex.Global = False

If regex.test(inputString) Then
Set GetRegExpMatch = regex.Execute(inputString)(0)
Else
Set GetRegExpMatch = Nothing
End If
End Function

Private Function GetHTMLContent(ByVal url As String) As String
Dim xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")

xhr.Open "GET", url, False
xhr.Send

If xhr.Status = 200 Then
GetHTMLContent = xhr.ResponseText
Else
GetHTMLContent = ""
End If

Set xhr = Nothing
End Function

If necessary i can provide the html code from one colour printer
Any ideas ?? Thanks in advance
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
If necessary i can provide the html code from one colour printer
Yes, do that. We need to see the entire HTML contained in the htmlContent string at this line:
VBA Code:
htmlContent = GetHTMLContent("http://" & ipAddress)
Use Free Online HTML Formatter - FreeFormatter.com to format the HTML nicely - so that it is readable - and post it between [CODE=html]the HTML here[/CODE] code tags.
 
Upvote 0
Yes, do that. We need to see the entire HTML contained in the htmlContent string at this line:
VBA Code:
htmlContent = GetHTMLContent("http://" & ipAddress)
Use Free Online HTML Formatter - FreeFormatter.com to format the HTML nicely - so that it is readable - and post it between [CODE=html]the HTML here[/CODE] code tags.
Thank you John , here goes the code , this is an example from a colour printer


<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><meta http-equiv="refresh" content="120; url=/cgi-bin/dynamic/printer/PrinterStatus.html"><title>Printer Status</title><base target="_self"><link rel="stylesheet" href="/configStyle.css" type="text/css"></head><body marginwidth="15" marginheight="10"><!-- Device Status Title--><h3>Status do dispositivo - <a href="/" target="_top"><sub><b>Atualizar</b></sub></a></h3><!-- More Details Link --><table><tbody><tr> <td> <font face="Arial" size="4" color="#0066CC"></font><font face="Arial" size="2"> <a href="/cgi-bin/dynamic/printer/SuppliesStatus.html"><font color="#0040CC" size="2" face="Arial"><b>Mais detalhes</b></font></a><font face="Arial" size="5" color="#FFFFFF">&nbsp;</font></font> </td></tr></tbody></table><!-- Job Parking --><hr class="status_hr"><!-- Panel and Toner Level --><table class="status_table"> <tbody><tr> <th colspan="4">&nbsp;</th> </tr> <!-- Toner Level --> <tr> <td colspan="4"> <b>Status do cartucho:</b> </td> </tr> </tbody></table><table width="650" borercolor="#000000" border="0"><tbody><tr><td><table width="80%" height="25" border="0"><tbody><tr><td width="30%" colspan="2" nowrap=""><b>Cartucho ciano</b></td></tr><tr><td width="25%"><table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1"><tbody><tr><td width="60%" bgcolor="#00ffff" title="60%">&nbsp;</td><td width="40%" bgcolor="#ffffff" title="60%">&nbsp;</td></tr></tbody></table></td></tr></tbody></table></td><td><table width="80%" height="25" border="0"><tbody><tr><td width="30%" colspan="2" nowrap=""><b>Cartucho magenta</b></td></tr><tr><td width="25%"><table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1"><tbody><tr><td width="30%" bgcolor="#ff00ff" title="30%">&nbsp;</td><td width="70%" bgcolor="#ffffff" title="30%">&nbsp;</td></tr></tbody></table></td></tr></tbody></table></td><td><table width="80%" height="25" border="0"><tbody><tr><td width="30%" colspan="2" nowrap=""><b>Cartucho Amarelo</b></td></tr><tr><td width="25%"><table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1"><tbody><tr><td width="50%" bgcolor="#ffff00" title="50%">&nbsp;</td><td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td></tr></tbody></table></td></tr></tbody></table></td><td><table width="80%" height="25" border="0"><tbody><tr><td width="30%" colspan="2" nowrap=""><b>Cartucho Preto</b></td></tr><tr><td width="25%"><table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1"><tbody><tr><td width="50%" bgcolor="#000000" title="50%">&nbsp;</td><td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table> <hr class="status_hr"><table class="status_table"> <tbody><tr> <th colspan="5">&nbsp;</th> </tr> <tr> <td width="23%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de entrada de papel:</b></p> </td> <td width="20%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p> </td> <td width="15%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p></td> <td width="15%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tamanho:</b></p></td> <td width="25%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tipo:</b></p></td> </tr> <tr> <td><p style="margin-left:5">Bandeja 1</p></td> <td width="30%"><p style="margin-left:5"><table width="50%" bgcolor="#009900" style="padding: .75pt" height="1"><tbody><tr><td><b>OK</b></td></tr></tbody></table></p></td><td><p style="margin-left:5">250</p></td> <td><p style="margin-left:5">A4</p></td> <td><p style="margin-left:5">Papel&nbsp;colorido</p></td></tr></tbody></table><hr class="status_hr"><table class="status_table"> <tbody><tr> <td width="23%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de saída de papel:</b></p></td> <td width="20%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p> </td> <td width="15%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p></td> <td width="15%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p></td> <td width="25%"><p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p></td> </tr> <tr> <td><p style="margin-left:5">Bandeja padrão</p></td> <td width="30%"><p style="margin-left:5"><table width="50%" bgcolor="#009900" style="padding: .75pt" height="1"><tbody><tr><td><b>OK</b></td></tr></tbody></table></p></td><td><p style="margin-left:5">125</p></td> <td><p style="margin-left:5"></p></td> <td><p style="margin-left:5"></p></td></tr></tbody></table><hr class="status_hr"><table class="status_table"> <tbody><tr> <th colspan="2">&nbsp;</th> </tr> <tr><td width="15%"><b>Tipo de dispositivo:</b></td><td width="60%">Laser colorida</td></tr><tr><td><b>Velocidade:</b></td><td>Até 32 Páginas/minuto</td></tr></tbody></table></body></html>
 
Upvote 0
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<meta http-equiv="refresh" content="120; url=/cgi-bin/dynamic/printer/PrinterStatus.html">
<title>Printer Status</title>
<base target="_self">
<link rel="stylesheet" href="/configStyle.css" type="text/css">
</head>
<body marginwidth="15" marginheight="10">
<!-- Device Status Title-->
<h3>Status do dispositivo - <a href="/" target="_top"><sub><b>Atualizar</b></sub></a></h3>
<!-- More Details Link -->
<table>
<tbody>
<tr>
<td> <font face="Arial" size="4" color="#0066CC"></font><font face="Arial" size="2">
<a href="/cgi-bin/dynamic/printer/SuppliesStatus.html"><font color="#0040CC" size="2" face="Arial"><b>Mais detalhes</b></font></a><font face="Arial" size="5" color="#FFFFFF">&nbsp;</font></font>
</td>
</tr>
</tbody>
</table>
<!-- Job Parking -->
<hr class="status_hr">
<!-- Panel and Toner Level -->
<table class="status_table">
<tbody>
<tr>
<th colspan="4">&nbsp;</th>
</tr>
<!-- Toner Level -->
<tr>
<td colspan="4">
<b>Status do cartucho:</b>
</td>
</tr>
</tbody>
</table>
<table width="650" borercolor="#000000" border="0">
<tbody>
<tr>
<td>
<table width="80%" height="25" border="0">
<tbody>
<tr>
<td width="30%" colspan="2" nowrap=""><b>Cartucho ciano</b></td>
</tr>
<tr>
<td width="25%">
<table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
<tbody>
<tr>
<td width="60%" bgcolor="#00ffff" title="60%">&nbsp;</td>
<td width="40%" bgcolor="#ffffff" title="60%">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table width="80%" height="25" border="0">
<tbody>
<tr>
<td width="30%" colspan="2" nowrap=""><b>Cartucho magenta</b></td>
</tr>
<tr>
<td width="25%">
<table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
<tbody>
<tr>
<td width="30%" bgcolor="#ff00ff" title="30%">&nbsp;</td>
<td width="70%" bgcolor="#ffffff" title="30%">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table width="80%" height="25" border="0">
<tbody>
<tr>
<td width="30%" colspan="2" nowrap=""><b>Cartucho Amarelo</b></td>
</tr>
<tr>
<td width="25%">
<table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
<tbody>
<tr>
<td width="50%" bgcolor="#ffff00" title="50%">&nbsp;</td>
<td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table width="80%" height="25" border="0">
<tbody>
<tr>
<td width="30%" colspan="2" nowrap=""><b>Cartucho Preto</b></td>
</tr>
<tr>
<td width="25%">
<table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
<tbody>
<tr>
<td width="50%" bgcolor="#000000" title="50%">&nbsp;</td>
<td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<hr class="status_hr">
<table class="status_table">
<tbody>
<tr>
<th colspan="5">&nbsp;</th>
</tr>
<tr>
<td width="23%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de entrada de papel:</b></p>
</td>
<td width="20%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p>
</td>
<td width="15%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p>
</td>
<td width="15%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tamanho:</b></p>
</td>
<td width="25%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tipo:</b></p>
</td>
</tr>
<tr>
<td>
<p style="margin-left:5">Bandeja 1</p>
</td>
<td width="30%">
<p style="margin-left:5">
<table width="50%" bgcolor="#009900" style="padding: .75pt" height="1">
<tbody>
<tr>
<td><b>OK</b></td>
</tr>
</tbody>
</table>
</p>
</td>
<td>
<p style="margin-left:5">250</p>
</td>
<td>
<p style="margin-left:5">A4</p>
</td>
<td>
<p style="margin-left:5">Papel&nbsp;colorido</p>
</td>
</tr>
</tbody>
</table>
<hr class="status_hr">
<table class="status_table">
<tbody>
<tr>
<td width="23%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de saída de papel:</b></p>
</td>
<td width="20%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p>
</td>
<td width="15%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p>
</td>
<td width="15%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p>
</td>
<td width="25%">
<p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p>
</td>
</tr>
<tr>
<td>
<p style="margin-left:5">Bandeja padrão</p>
</td>
<td width="30%">
<p style="margin-left:5">
<table width="50%" bgcolor="#009900" style="padding: .75pt" height="1">
<tbody>
<tr>
<td><b>OK</b></td>
</tr>
</tbody>
</table>
</p>
</td>
<td>
<p style="margin-left:5">125</p>
</td>
<td>
<p style="margin-left:5"></p>
</td>
<td>
<p style="margin-left:5"></p>
</td>
</tr>
</tbody>
</table>
<hr class="status_hr">
<table class="status_table">
<tbody>
<tr>
<th colspan="2">&nbsp;</th>
</tr>
<tr>
<td width="15%"><b>Tipo de dispositivo:</b></td>
<td width="60%">Laser colorida</td>
</tr>
<tr>
<td><b>Velocidade:</b></td>
<td>Até 32 Páginas/minuto</td>
</tr>
</tbody>
</table>
</body>
</html>
 
Upvote 0
Now yes , the correct code

HTML:
<html>
   <head>
      <meta content="text/html; charset=UTF-8" http-equiv="content-type">
      <meta http-equiv="refresh" content="120; url=/cgi-bin/dynamic/printer/PrinterStatus.html">
      <title>Printer Status</title>
      <base target="_self">
      <link rel="stylesheet" href="/configStyle.css" type="text/css">
   </head>
   <body marginwidth="15" marginheight="10">
      <!-- Device Status Title-->
      <h3>Status do dispositivo - <a href="/" target="_top"><sub><b>Atualizar</b></sub></a></h3>
      <!-- More Details Link -->
      <table>
         <tbody>
            <tr>
               <td> <font face="Arial" size="4" color="#0066CC"></font><font face="Arial" size="2"> <a href="/cgi-bin/dynamic/printer/SuppliesStatus.html"><font color="#0040CC" size="2" face="Arial"><b>Mais detalhes</b></font></a><font face="Arial" size="5" color="#FFFFFF">&nbsp;</font></font> </td>
            </tr>
         </tbody>
      </table>
      <!-- Job Parking -->
      <hr class="status_hr">
      <!-- Panel and Toner Level -->
      <table class="status_table">
         <tbody>
            <tr>
               <th colspan="4">&nbsp;</th>
            </tr>
            <!-- Toner Level --> 
            <tr>
               <td colspan="4"> <b>Status do cartucho:</b> </td>
            </tr>
         </tbody>
      </table>
      <table width="650" borercolor="#000000" border="0">
         <tbody>
            <tr>
               <td>
                  <table width="80%" height="25" border="0">
                     <tbody>
                        <tr>
                           <td width="30%" colspan="2" nowrap=""><b>Cartucho ciano</b></td>
                        </tr>
                        <tr>
                           <td width="25%">
                              <table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
                                 <tbody>
                                    <tr>
                                       <td width="60%" bgcolor="#00ffff" title="60%">&nbsp;</td>
                                       <td width="40%" bgcolor="#ffffff" title="60%">&nbsp;</td>
                                    </tr>
                                 </tbody>
                              </table>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </td>
               <td>
                  <table width="80%" height="25" border="0">
                     <tbody>
                        <tr>
                           <td width="30%" colspan="2" nowrap=""><b>Cartucho magenta</b></td>
                        </tr>
                        <tr>
                           <td width="25%">
                              <table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
                                 <tbody>
                                    <tr>
                                       <td width="30%" bgcolor="#ff00ff" title="30%">&nbsp;</td>
                                       <td width="70%" bgcolor="#ffffff" title="30%">&nbsp;</td>
                                    </tr>
                                 </tbody>
                              </table>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </td>
               <td>
                  <table width="80%" height="25" border="0">
                     <tbody>
                        <tr>
                           <td width="30%" colspan="2" nowrap=""><b>Cartucho Amarelo</b></td>
                        </tr>
                        <tr>
                           <td width="25%">
                              <table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
                                 <tbody>
                                    <tr>
                                       <td width="50%" bgcolor="#ffff00" title="50%">&nbsp;</td>
                                       <td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td>
                                    </tr>
                                 </tbody>
                              </table>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </td>
               <td>
                  <table width="80%" height="25" border="0">
                     <tbody>
                        <tr>
                           <td width="30%" colspan="2" nowrap=""><b>Cartucho Preto</b></td>
                        </tr>
                        <tr>
                           <td width="25%">
                              <table bordercolor="#000000" cellspacing="0" width="160" height="23" border="1">
                                 <tbody>
                                    <tr>
                                       <td width="50%" bgcolor="#000000" title="50%">&nbsp;</td>
                                       <td width="50%" bgcolor="#ffffff" title="50%">&nbsp;</td>
                                    </tr>
                                 </tbody>
                              </table>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </td>
            </tr>
         </tbody>
      </table>
      <hr class="status_hr">
      <table class="status_table">
         <tbody>
            <tr>
               <th colspan="5">&nbsp;</th>
            </tr>
            <tr>
               <td width="23%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de entrada de papel:</b></p>
               </td>
               <td width="20%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p>
               </td>
               <td width="15%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p>
               </td>
               <td width="15%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tamanho:</b></p>
               </td>
               <td width="25%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Tipo:</b></p>
               </td>
            </tr>
            <tr>
               <td>
                  <p style="margin-left:5">Bandeja 1</p>
               </td>
               <td width="30%">
                  <p style="margin-left:5">
                  <table width="50%" bgcolor="#009900" style="padding: .75pt" height="1">
                     <tbody>
                        <tr>
                           <td><b>OK</b></td>
                        </tr>
                     </tbody>
                  </table>
                  </p>
               </td>
               <td>
                  <p style="margin-left:5">250</p>
               </td>
               <td>
                  <p style="margin-left:5">A4</p>
               </td>
               <td>
                  <p style="margin-left:5">Papel&nbsp;colorido</p>
               </td>
            </tr>
         </tbody>
      </table>
      <hr class="status_hr">
      <table class="status_table">
         <tbody>
            <tr>
               <td width="23%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Bandeja de saída de papel:</b></p>
               </td>
               <td width="20%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Status:</b></p>
               </td>
               <td width="15%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b>Capacidade:</b></p>
               </td>
               <td width="15%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p>
               </td>
               <td width="25%">
                  <p align="left" style="margin-top: 5; margin-bottom: 5"><b></b></p>
               </td>
            </tr>
            <tr>
               <td>
                  <p style="margin-left:5">Bandeja padrão</p>
               </td>
               <td width="30%">
                  <p style="margin-left:5">
                  <table width="50%" bgcolor="#009900" style="padding: .75pt" height="1">
                     <tbody>
                        <tr>
                           <td><b>OK</b></td>
                        </tr>
                     </tbody>
                  </table>
                  </p>
               </td>
               <td>
                  <p style="margin-left:5">125</p>
               </td>
               <td>
                  <p style="margin-left:5"></p>
               </td>
               <td>
                  <p style="margin-left:5"></p>
               </td>
            </tr>
         </tbody>
      </table>
      <hr class="status_hr">
      <table class="status_table">
         <tbody>
            <tr>
               <th colspan="2">&nbsp;</th>
            </tr>
            <tr>
               <td width="15%"><b>Tipo de dispositivo:</b></td>
               <td width="60%">Laser colorida</td>
            </tr>
            <tr>
               <td><b>Velocidade:</b></td>
               <td>Até 32 Páginas/minuto</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>
 
Upvote 0
I've used simple string search and parsing functions to extract a toner level, rather than using HTML library functions.

Add this function:
VBA Code:
Private Function Get_Toner_Level(titleNumber As Long, HTML As String) As String

    Dim p1 As Long, p2 As Long
    Dim i As Long
    
    'Find nth "title=" substring and return the nn% number (e.g. 60%)
    
    '<td width="60%" bgcolor="#00ffff" title="60%">&nbsp;</td>
    
    p1 = 0
    For i = 1 To titleNumber
        p1 = InStr(p1 + 1, HTML, "title=", vbTextCompare)
    Next
    p1 = p1 + Len("title=")
    p2 = InStr(p1, HTML, ">")
    
    Get_Toner_Level = Replace(Mid(HTML, p1, p2 - p1), Chr(34), "")

End Function

Call it like this to extract the 4 toner levels into the text boxes

VBA Code:
Private Sub GetToner_Click()

    ' Get the IP address from TextBox2
    Dim ipAddress As String
    ipAddress = TextBox2.Value
    
    ' Get the HTML content from the printer's web page
    Dim htmlContent As String
    htmlContent = GetHTMLContent("http://" & ipAddress)
        
    ' Update the TextBoxes with toner levels
    TextBox5.Value = Get_Toner_Level(1, htmlContent) ' Cyan Toner Level
    TextBox6.Value = Get_Toner_Level(3, htmlContent) ' Magenta Toner Level
    TextBox7.Value = Get_Toner_Level(5, htmlContent) ' Yellow Toner Level
    TextBox8.Value = Get_Toner_Level(7, htmlContent) ' Black Toner Level

End Sub
 
Upvote 0
I've used simple string search and parsing functions to extract a toner level, rather than using HTML library functions.

Add this function:
VBA Code:
Private Function Get_Toner_Level(titleNumber As Long, HTML As String) As String

    Dim p1 As Long, p2 As Long
    Dim i As Long
   
    'Find nth "title=" substring and return the nn% number (e.g. 60%)
   
    '<td width="60%" bgcolor="#00ffff" title="60%">&nbsp;</td>
   
    p1 = 0
    For i = 1 To titleNumber
        p1 = InStr(p1 + 1, HTML, "title=", vbTextCompare)
    Next
    p1 = p1 + Len("title=")
    p2 = InStr(p1, HTML, ">")
   
    Get_Toner_Level = Replace(Mid(HTML, p1, p2 - p1), Chr(34), "")

End Function

Call it like this to extract the 4 toner levels into the text boxes

VBA Code:
Private Sub GetToner_Click()

    ' Get the IP address from TextBox2
    Dim ipAddress As String
    ipAddress = TextBox2.Value
   
    ' Get the HTML content from the printer's web page
    Dim htmlContent As String
    htmlContent = GetHTMLContent("http://" & ipAddress)
       
    ' Update the TextBoxes with toner levels
    TextBox5.Value = Get_Toner_Level(1, htmlContent) ' Cyan Toner Level
    TextBox6.Value = Get_Toner_Level(3, htmlContent) ' Magenta Toner Level
    TextBox7.Value = Get_Toner_Level(5, htmlContent) ' Yellow Toner Level
    TextBox8.Value = Get_Toner_Level(7, htmlContent) ' Black Toner Level

End Sub
Thanks John , I've updated my code with this , no errors , but still didn't populate textboxes 5 to 8 ... But , when i change this the htmlContent = GetHTMLContent("http://" & ipAddress & "/cgi-bin/dynamic/printer/PrinterStatus.html") instead of only the ip address , it worked . But to the mono printers didn't work . returns an error . Since all printers are LExmark but with several diffrent models , is there a way to the function Get Toner Level extract from different models ?
 
Upvote 0
John , one thing i forgot , is there a way to , in the mono printers , to populate only the textbox5 ?
 
Upvote 0
New update , the code worked but not for all the colour printers , in the recent models , it gives an error .
Here is the code from one other colour printer :
HTML:
<div class="main" id="maincontent" lang="en" role="main">
   <div class="spinner-block" style="display: none;">
      <div class="block-spinner"></div>
   </div>
   <ul class="breadcrumb" role="navigation">
      <li data-path="/Status" data-preserve-query="0" id="Status-Breadcrumb" class="breadcrumb-item">
         Status
      </li>
   </ul>
   <div class="bodyHeader" role="gridcell">
      <span class="mainHeader" role="heading" tabindex="1000">
      <span class="translated" data-textid="67964" tabindex="-1">
      Status
      </span>
      </span>
   </div>
   <div id="Status" class="page" data-node="Status" data-init="initPage(this);">
      <div class="setting-saved-message" style="display: none;">
         <div class="setting-saved-spinner"></div>
         <div class="setting-saved-message-text"></div>
      </div>
      <div class="page-contents setting-contents">
         <ul>
            <li id="IrPanel" data-node="IrPanel" class="child-row">
               <div class="panel" panel-data-node="IrPanel" data-init="initPanel(this);" data-toggleable="1">
                  <div class="panel-header panel-header-toggled" role="tab" tabindex="1001">
                     <div class="panel-header-indicator"></div>
                     <span class="translated" data-textid="72888" tabindex="-1">
                     Messages
                     </span>
                  </div>
                  <div class="setting-saved-message" style="display: none;">
                     <div class="setting-saved-spinner"></div>
                     <div class="setting-saved-message-text"></div>
                  </div>
                  <div class="panel-contents setting-contents " style="">
                     <ul>
                        <li id="Irs" data-node="Irs" class="child-row">
                           <div class="ir-list" data-init="initIrList(this);">
                              <div class="ir-alerts-container">
                                 <div class="ir-header">
                                    <span class="translated" data-textid="73282" tabindex="-1">
                                    Alerts
                                    </span>
                                 </div>
                                 <div class="ir-header-button" data-node="SendLogs">
                                    <button tabindex="1002" class="action greyBtn" name="ir-reset-printer">
                                    <span class="translated" data-textid="70437" tabindex="-1">
                                    Reset Printer
                                    </span>
                                    </button>
                                    <span data-action="SEND_LOGS" data-callback="ews.external.AboutThisPrinter.SendLogs" data-cancel="1" data-confirm-textid="TXT_SEND_DIAGNOSTIC_INFO" data-confirm-yesid="TXT_SEND" data-confirm-noid="TXT_CANCEL" data-confirm-text="" data-confirm-yestext="" data-confirm-notext=""><button class="action greyBtn" tabindex="1002"><span tabindex="-1"><span class="translated" data-textid="75433">Send Logs</span></span></button></span>
                                 </div>
                                 <table class="tablesorter ir-alerts">
                                    <thead>
                                       <tr>
                                          <th class="ir-icon"></th>
                                          <th class="ir-message">
                                             <span class="translated" data-textid="68454" tabindex="-1">
                                             Message
                                             </span>
                                          </th>
                                          <th class="ir-source">
                                             <span class="translated" data-textid="76771" tabindex="-1">
                                             Source
                                             </span>
                                          </th>
                                          <th class="ir-actions"></th>
                                       </tr>
                                    </thead>
                                    <tbody>
                                       <tr>
                                          <td></td>
                                          <td><span class="translated" data-textid="72890">No alerts exist on the device.</span></td>
                                          <td></td>
                                          <td></td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </div>
                              <div class="ir-warnings-container">
                                 <div class="ir-header">
                                    <span class="translated" data-textid="67494" tabindex="-1">
                                    Warnings
                                    </span>
                                 </div>
                                 <table class="tablesorter ir-warnings">
                                    <thead>
                                       <tr>
                                          <th class="ir-icon"></th>
                                          <th class="ir-message">
                                             <span class="translated" data-textid="68454" tabindex="-1">
                                             Message
                                             </span>
                                          </th>
                                       </tr>
                                    </thead>
                                    <tbody>
                                       <tr>
                                          <td></td>
                                          <td><span class="translated" data-textid="72891">No warnings exist on the device.</span></td>
                                       </tr>
                                    </tbody>
                                 </table>
                              </div>
                           </div>
                        </li>
                     </ul>
                     <ul class="links"></ul>
                  </div>
               </div>
            </li>
            <li id="Supplies" data-node="Supplies" class="child-row">
               <div class="content " data-init="initControlNode(this);" data-child="">
                  <div class="groupHeader">
                     <span class="translated" data-textid="67586" tabindex="-1">
                     Supplies
                     </span>
                  </div>
                  <div class="panel-contents">
                     <ul>
                        <li id="NLRP" data-node="NLRP" class="child-row">
                           <div class="NLRPNode" data-init="initNLRP(this);"></div>
                        </li>
                        <li id="TonerSupplies" data-node="TonerSupplies" class="child-row">
                           <div class="supplyStatusContainer" data-init="initSupplyStatusContainer(this)">
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="67527" tabindex="-1">
                                    Black Cartridge
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-1">
                                       <div class="progress-inner BlackGauge" role="img" title="34%" aria-labelledby="34%">
                                          <div class="progress-slider" style="width: 79px; overflow: hidden;">
                                             <span class="dataText">34</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="67526" tabindex="-1">
                                    Cyan Cartridge
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-2">
                                       <div class="progress-inner CyanGauge" role="img" title="49%" aria-labelledby="49%">
                                          <div class="progress-slider" style="width: 113px; overflow: hidden;">
                                             <span class="dataText">49</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="67525" tabindex="-1">
                                    Magenta Cartridge
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-3">
                                       <div class="progress-inner MagentaGauge" role="img" title="79%" aria-labelledby="79%">
                                          <div class="progress-slider" style="width: 182px; overflow: hidden;">
                                             <span class="dataText">79</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="67524" tabindex="-1">
                                    Yellow Cartridge
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-4">
                                       <div class="progress-inner YellowGauge" role="img" title="41%" aria-labelledby="41%">
                                          <div class="progress-slider" style="width: 95px; overflow: hidden;">
                                             <span class="dataText">41</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                           </div>
                        </li>
                        <li id="PCDrumStatus" data-node="PCDrumStatus" class="child-row">
                           <div class="supplyStatusContainer" data-init="initSupplyStatusContainer(this)"></div>
                        </li>
                        <li id="FuserSuppliesStatus" data-node="FuserSuppliesStatus" class="child-row">
                           <div class="supplyStatusContainer" data-init="initSupplyStatusContainer(this)">
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="65634" tabindex="-1">
                                    Fuser
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-32">
                                       <div class="progress-inner BlackGauge" role="img" title="76%" aria-labelledby="76%">
                                          <div class="progress-slider" style="width: 175px; overflow: hidden;">
                                             <span class="dataText">76</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                           </div>
                        </li>
                        <li id="OtherSuppliesStatusGauge" data-node="OtherSuppliesStatusGauge" class="child-row">
                           <div class="supplyStatusContainer" data-init="initSupplyStatusContainer(this)">
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="68236" tabindex="-1">
                                    Black Imaging Unit
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-57">
                                       <div class="progress-inner BlackGauge" role="img" title="78%" aria-labelledby="78%">
                                          <div class="progress-slider" style="width: 180px; overflow: hidden;">
                                             <span class="dataText">78</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="73756" tabindex="-1">
                                    Color Imaging Kit
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-58">
                                       <div class="progress-inner BlackGauge" role="img" title="78%" aria-labelledby="78%">
                                          <div class="progress-slider" style="width: 180px; overflow: hidden;">
                                             <span class="dataText">78</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="67349" tabindex="-1">
                                    Transfer Module
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="progress" role="presentation" tabindex="" data-deviceid="8-35">
                                       <div class="progress-inner BlackGauge" role="img" title="73%" aria-labelledby="73%">
                                          <div class="progress-slider" style="width: 168px; overflow: hidden;">
                                             <span class="dataText">73</span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                           </div>
                        </li>
                        <li id="OtherSuppliesStatusLevel" data-node="OtherSuppliesStatusLevel" class="child-row">
                           <div class="supplyStatusContainer" data-init="initSupplyStatusContainer(this)">
                              <div class="contentRow" role="gridcell">
                                 <div class="contentHeader" role="heading">
                                    <span class="translated" data-textid="68245" tabindex="-1">
                                    Waste Toner Bottle
                                    </span>
                                    <br>
                                 </div>
                                 <div class="contentBody" role="presentation">
                                    <div class="supplyStatusIndicator" role="img" aria-labelledby="supplystatus-8-112" tabindex="" data-deviceid="8-112">
                                       <label id="supplystatus-8-112" aria-hidden="true">
                                       <span class="translated" data-textid="67896" tabindex="-1">
                                       OK
                                       </span>
                                       </label>
                                       <div class="statusIndicator error">
                                          <div class="text" aria-label="">
                                             <span class="translated" data-textid="66109" tabindex="-1">
                                             Full
                                             </span>
                                          </div>
                                          <div class="angle right">&nbsp;</div>
                                       </div>
                                       <div class="statusIndicator warning">
                                          <div class="angle left">&nbsp;</div>
                                          <div class="text" aria-label="">
                                             <span class="translated" data-textid="73035" tabindex="-1">
                                             Nearly Full
                                             </span>
                                          </div>
                                          <div class="angle right">&nbsp;</div>
                                       </div>
                                       <div class="statusIndicator ok selected">
                                          <div class="angle left ">&nbsp;</div>
                                          <div class="text" aria-label="">
                                             <span class="translated" data-textid="75701" tabindex="-1">
                                             OK
                                             </span>
                                          </div>
                                       </div>
                                       <div class="statusIndicator" id="missinglevel">
                                          <div class="text" aria-label="">
                                             <span class="translated" data-textid="67471" tabindex="-1">
                                             Missing
                                             </span>
                                          </div>
                                       </div>
                                    </div>
                                 </div>
                              </div>
                           </div>
                        </li>
                     </ul>
                  </div>
               </div>
            </li>
            <li id="Printer" data-node="Printer" class="child-row">
               <div class="content " data-init="initControlNode(this);" data-child="">
                  <div class="groupHeader">
                     <span class="translated" data-textid="67399" tabindex="-1">
                     Printer
                     </span>
                  </div>
                  <div class="panel-contents">
                     <ul>
                        <li id="DeviceType" data-node="DeviceType" class="child-row">
                           <div class="contentHeader" role="heading">
                              <span class="translated" data-textid="66242" tabindex="-1">
                              Device Type
                              </span>
                           </div>
                           <div class="contentBody" role="presentation">
                              <div class="node-display node-textdisplay">
                                 <span class="untranslated">Color</span>
                              </div>
                           </div>
                        </li>
                        <li id="DeviceSpeed" data-node="DeviceSpeed" class="child-row">
                           <div class="contentHeader" role="heading">
                              <span class="translated" data-textid="72907" tabindex="-1">
                              Device Speed
                              </span>
                           </div>
                           <div class="contentBody" role="presentation">
                              <div class="node-display node-textdisplay">
                                 <span class="untranslated">Up to 50 pages/minute</span>
                              </div>
                           </div>
                        </li>
                        <li id="DeviceFirmwareLevel" data-node="DeviceFirmwareLevel" class="child-row">
                           <div class="contentHeader" role="heading">
                              <span class="translated" data-textid="72908" tabindex="-1">
                              Firmware Level
                              </span>
                           </div>
                           <div class="contentBody" role="presentation">
                              <div class="node-display node-textdisplay">
                                 <span class="untranslated">CXTAT.081.225</span>
                              </div>
                           </div>
                        </li>
                        <li id="DeviceSerialNumberLxk" data-node="DeviceSerialNumberLxk" class="child-row">
                           <div class="contentHeader" role="heading">
                              <span class="translated" data-textid="71123" tabindex="-1">
                              Serial Number
                              </span>
                           </div>
                           <div class="contentBody" role="presentation">
                              <div class="node-display node-textdisplay">
                                 <span class="untranslated">75281170H64NB</span>
                              </div>
                           </div>
                        </li>
                        <li id="InputTrayStatus" data-node="InputTrayStatus" class="child-row">
                           <div class="content " data-init="initControlNode(this);" data-child="">
                              <div class="panel-contents">
                                 <ul>
                                    <li id="InputsStatus" data-node="InputsStatus" class="child-row">
                                       <div class="trayStatusContainer" data-init="initTrayStatusContainer(this)">
                                          <div class="contentRow" data-deviceid="1-131" role="gridcell">
                                             <div class="trayInfoTitle contentHeader" role="heading">
                                                <span class="translated" data-textid="65635" tabindex="-1">
                                                Multipurpose Feeder
                                                </span>
                                             </div>
                                             <div class="contentBody trayStatus tray-error" role="presentation">
                                                <div class="trayInfoContainer">
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="67421" tabindex="-1">
                                                         Capacity
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         100
                                                      </div>
                                                   </div>
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="67976" tabindex="-1">
                                                         Size
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         A4
                                                      </div>
                                                   </div>
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="66227" tabindex="-1">
                                                         Type
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         Custom Type 6
                                                      </div>
                                                   </div>
                                                </div>
                                             </div>
                                          </div>
                                          <div class="contentRow" data-deviceid="1-1" role="gridcell">
                                             <div class="trayInfoTitle contentHeader" role="heading">
                                                <span class="translated" data-textid="65633" tabindex="-1">
                                                Tray 1
                                                </span>
                                             </div>
                                             <div class="contentBody trayStatus tray-ok" role="presentation">
                                                <div class="trayInfoContainer">
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="67421" tabindex="-1">
                                                         Capacity
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         550
                                                      </div>
                                                   </div>
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="67976" tabindex="-1">
                                                         Size
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         A4
                                                      </div>
                                                   </div>
                                                   <div class="contentRow trayInfo" role="gridcell">
                                                      <div class="contentHeader" role="heading">
                                                         <span class="translated" data-textid="66227" tabindex="-1">
                                                         Type
                                                         </span>
                                                      </div>
                                                      <div class="contentBody" role="presentation">
                                                         Plain Paper
                                                      </div>
                                                   </div>
                                                </div>
                                             </div>
                                          </div>
                                       </div>
                                    </li>
                                 </ul>
                              </div>
                           </div>
                        </li>
                        <li id="OutputBins" data-node="OutputBins" class="child-row">
                           <div class="content " data-init="initControlNode(this);" data-child="">
                              <div class="panel-contents">
                                 <ul>
                                    <li id="OutputBinsStatus" data-node="OutputBinsStatus" class="child-row">
                                       <div class="outputBinStatusContainer" data-init="initOutputBinStatusContainer(this)">
                                          <div class="contentRow" data-deviceid="2-1" role="gridcell">
                                             <div class="contentHeader" role="heading">
                                                Standard Bin
                                             </div>
                                             <div class="contentBody" role="presentation">
                                                <div class="binStatusIndicator" role="img" aria-labelledby="supplystatus-2-1">
                                                   <label id="supplystatus-2-1" aria-hidden="true">
                                                   <span class="translated" data-textid="75701" tabindex="-1">OK</span>
                                                   </label>
                                                   <div class="statusIndicator error">
                                                      <div class="text" aria-label="">
                                                         <span class="translated" data-textid="66109" tabindex="-1">
                                                         Full
                                                         </span>
                                                      </div>
                                                      <div class="angle right">&nbsp;</div>
                                                   </div>
                                                   <div class="statusIndicator warning">
                                                      <div class="angle left">&nbsp;</div>
                                                      <div class="text" aria-label="">
                                                         <span class="translated" data-textid="73035" tabindex="-1">
                                                         Nearly Full
                                                         </span>
                                                      </div>
                                                      <div class="angle right">&nbsp;</div>
                                                   </div>
                                                   <div class="statusIndicator ok selected">
                                                      <div class="angle left ">&nbsp;</div>
                                                      <div class="text" aria-label="">
                                                         <span class="translated" data-textid="75701" tabindex="-1">
                                                         OK
                                                         </span>
                                                      </div>
                                                   </div>
                                                   <div class="statusIndicator missing" id="missinglevel">
                                                      <div class="text" aria-label=""></div>
                                                   </div>
                                                </div>
                                             </div>
                                          </div>
                                       </div>
                                    </li>
                                 </ul>
                              </div>
                           </div>
                        </li>
                     </ul>
                  </div>
               </div>
            </li>
         </ul>
      </div>
   </div>
</div>
 
Upvote 0
But , when i change this the htmlContent = GetHTMLContent("http://" & ipAddress & "/cgi-bin/dynamic/printer/PrinterStatus.html") instead of only the ip address , it worked .

I just used the same GetHTMLContent call in your OP, assuming you were using the full URL.

Here is a better method using the HTML library. Also, it doesn't rely on looking for specific numbered "title" substrings (1,3,5,7), which I suspect would fail if any of the toners is 100%, because that would need only one <td title="60%" width="60%" bgcolor="#00ffff">&nbsp;</td> element, instead of 2 elements, and therefore the number sequence would be different. Note that this code is specific to the HTML in post #5 and you would have to write specific code for the HTML for the other printers.

This code requires a reference to Microsoft HTML Object Library, set via Tools -> References in the VBA editor.

VBA Code:
Private Sub GetToner_Click()
  
    ' Get the IP address from TextBox2
    Dim ipAddress As String
    'ipAddress = TextBox2.Value
  
    ' Get the HTML content from the printer's web page
    Dim htmlContent As String
    htmlContent = GetHTMLContent("http://" & ipAddress & "/cgi-bin/dynamic/printer/PrinterStatus.html")
  
    Dim HTMLdoc As HTMLDocument
    Dim colourLevel As String
  
    Set HTMLdoc = New HTMLDocument
    HTMLdoc.body.innerHTML = htmlContent
  
    colourLevel = Get_Toner_Level(HTMLdoc, "Cartucho ciano")
    If colourLevel <> "" Then
        TextBox5.Value = colourLevel
    Else
        MsgBox "'Cartucho ciano' not found", vbExclamation
    End If
  
    colourLevel = Get_Toner_Level(HTMLdoc, "Cartucho magenta")
    If colourLevel <> "" Then
        TextBox6.Value = colourLevel
    Else
        MsgBox "'Cartucho magenta' not found", vbExclamation
    End If
  
    colourLevel = Get_Toner_Level(HTMLdoc, "Cartucho Amarelo")
    If colourLevel <> "" Then
        TextBox7.Value = colourLevel
    Else
        MsgBox "'Cartucho Amarelo' not found", vbExclamation
    End If
  
    colourLevel = Get_Toner_Level(HTMLdoc, "Cartucho Preto")
    If colourLevel <> "" Then
        TextBox8.Value = colourLevel
    Else
        MsgBox "'Cartucho Preto' not found", vbExclamation
    End If
      
End Sub


Public Function Get_Toner_Level(HTMLdoc As HTMLDocument, colourTitle As String) As String

    'Return colour level value (e.g. 60%) for the specified colour title
  
    '<table width="80%" height="25" border="0">
    ' <tbody>
    '   <tr>
    '      <td colspan="2" width="30%" nowrap=""><b>Cartucho ciano</b></td>
    '   </tr>
    '   <tr>
    '      <td width="25%">
    '         <table width="160" height="23" cellspacing="0" bordercolor="#000000" border="1">
    '            <tbody>
    '               <tr>
    '                  <td title="60%" width="60%" bgcolor="#00ffff">&nbsp;</td>
    '                  <td title="60%" width="40%" bgcolor="#ffffff">&nbsp;</td>
    '               </tr>
    '            </tbody>
    '         </table>
    '      </td>
    '   </tr>
    ' </tbody>
    '</table>

    Dim colourTable As HTMLTable
    Dim tCell As HTMLTableCell
    Dim elem As HTMLGenericElement
  
    Get_Toner_Level = ""
  
    'Find td element containing the specified colour title and get its table
  
    Set colourTable = Nothing
    For Each tCell In HTMLdoc.getElementsByTagName("td")
        If tCell.getElementsByTagName("td").Length = 0 Then 'innermost table cell
            If InStr(1, tCell.innerText, colourTitle, vbTextCompare) Then
                'Found it - backtrack to parent table
                Set elem = tCell
                Do
                    Set elem = elem.parentElement
                Loop Until elem.tagName = "TABLE"
                Set colourTable = elem
            End If
        End If
    Next
      
    If Not colourTable Is Nothing Then
  
        'Found the table for this colour title. Extract the 'title' attribute value (e.g. 60%) from the first td element which has the 'title' attribute
        '<td title="60%" width="60%" bgcolor="#00ffff">&nbsp;</td>
        '<td title="60%" width="40%" bgcolor="#ffffff">&nbsp;</td>
              
        Get_Toner_Level = colourTable.querySelector("td[title]").getAttribute("title")
      
    End If
      
End Function

But to the mono printers didn't work . returns an error . Since all printers are LExmark but with several diffrent models , is there a way to the function Get Toner Level extract from different models ?

You would have to write VBA code which is specific to the HTML for each printer. I'm not going to do that for you, but hopefully my code above and other HTML parsing code on this forum should be enough to get you started.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top