What is the correct method for targeting and printing a child (Internet Explorer) web browser window using VBA from within Excel 2016?
I've tried a couple of different methods and so far none of them have worked.
Try 1:
This prints a page to the printer but it targets the parent window to print, it did not print the child popup window.
Try 2:
This method didn't error but it also didn't print anything.
Try 3:
This method printed but it again targeted the parent window to print, it did not print the child popup window:
Is there something wrong in my code or is there a different approach I should be taking to print a child popup window in a web browser?
Here is the code that establishes the connection with the parent window:
Thank much!
I've tried a couple of different methods and so far none of them have worked.
Try 1:
This prints a page to the printer but it targets the parent window to print, it did not print the child popup window.
Code:
PrintTicketPreview:
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Look At Only Element Types Of Form
Set Tags = IE.Document.getElementsByTagName("form")
'Click The Escalate Button Twice To Escalate This Ticket To IT Electronic Repair Services Level 3
For Each Tagx In Tags
'Identify DIV Container By ID Attribute
If Tagx.getAttribute("name") = "ticketForm" Then
'Get Path From upDateURL Attribute In DIV Container
Path = Tagx.getAttribute("action")
'Use Srring Manipulation To Get Only The RequestID Number From Path
RequestID = Right(Path, Len(Path) - Application.WorksheetFunction.Find("wo/", Path) - 2)
'Add RequestID To Below JavaScript Call To Click On Asset ID Hyperlink To Attach It To The Ticket
Call CurrentWindow.execScript("javascript:************('/helpdesk/WebObjects/Helpdesk.woa/wo/" & RequestID & ".1.1.6.1','printView','toolbar=no,location=yes,status=no,menubar=no,resizable=yes,scrollbars=yes,top=50,left=50,width=700,height=700')")
GoTo PrintTicket
End If
Next
Exit Sub
PrintTicket:
On Error Resume Next
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Loop Through All Open Windows...
For Each wd In CreateObject("Shell.Application").Windows
'And Find The Internet Explorer Windows...
If wd = "Internet Explorer" Then
'With The Title Containins Print View
If InStr(wd.Document.Title, "Print View") <> 0 Then
'Bring This WindoXws To The Front
wd.Focus
wd.AppActive
'Send That IE Ticket Window To The Default Printer
IE.ExecWB 6, 2
End If
End If
Next wd
Try 2:
This method didn't error but it also didn't print anything.
Code:
PrintTicketPreview:
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Look At Only Element Types Of Form
Set Tags = IE.Document.getElementsByTagName("form")
'Click The Escalate Button Twice To Escalate This Ticket To IT Electronic Repair Services Level 3
For Each Tagx In Tags
'Identify DIV Container By ID Attribute
If Tagx.getAttribute("name") = "ticketForm" Then
'Get Path From upDateURL Attribute In DIV Container
Path = Tagx.getAttribute("action")
'Use Srring Manipulation To Get Only The RequestID Number From Path
RequestID = Right(Path, Len(Path) - Application.WorksheetFunction.Find("wo/", Path) - 2)
'Add RequestID To Below JavaScript Call To Click On Asset ID Hyperlink To Attach It To The Ticket
Call CurrentWindow.execScript("javascript:************('/helpdesk/WebObjects/Helpdesk.woa/wo/" & RequestID & ".1.1.6.1','printView','toolbar=no,location=yes,status=no,menubar=no,resizable=yes,scrollbars=yes,top=50,left=50,width=700,height=700')")
GoTo PrintTicket
End If
Next
Exit Sub
PrintTicket:
On Error Resume Next
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Const PRINT_WAITFORCOMPLETION = 2
Dim oIExplorer : Set oIExplorer =
CreateObject("InternetExplorer.Application")
oIExplorer.Navigate "http://www.scriptbox.at.tt/"
oIExplorer.Visible = 1
Do while oIExplorer.ReadyState <> 4
wscript.sleep 1000
Loop
oIExplorer.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
Try 3:
This method printed but it again targeted the parent window to print, it did not print the child popup window:
Code:
PrintTicketPreview:
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Look At Only Element Types Of Form
Set Tags = IE.Document.getElementsByTagName("form")
'Click The Escalate Button Twice To Escalate This Ticket To IT Electronic Repair Services Level 3
For Each Tagx In Tags
'Identify DIV Container By ID Attribute
If Tagx.getAttribute("name") = "ticketForm" Then
'Get Path From upDateURL Attribute In DIV Container
Path = Tagx.getAttribute("action")
'Use Srring Manipulation To Get Only The RequestID Number From Path
RequestID = Right(Path, Len(Path) - Application.WorksheetFunction.Find("wo/", Path) - 2)
'Add RequestID To Below JavaScript Call To Click On Asset ID Hyperlink To Attach It To The Ticket
Call CurrentWindow.execScript("javascript:************('/helpdesk/WebObjects/Helpdesk.woa/wo/" & RequestID & ".1.1.6.1','printView','toolbar=no,location=yes,status=no,menubar=no,resizable=yes,scrollbars=yes,top=50,left=50,width=700,height=700')")
GoTo PrintTicket
End If
Next
Exit Sub
PrintTicket:
On Error Resume Next
Dim oIExplorer:
Set IE = New SHDocVw.InternetExplorerMedium
'Look At Only Element Types Of DIV
Set Tags = IE.Document.getElementsByTagName("a")
For Each Tagx In Tags
'Identify DIV Container By ID Attribute
If Tagx.getAttribute("title") = "Printable view" Then
'Get Path From upDateURL Attribute In DIV Container
Path = Tagx.getAttribute("href")
'Use Srring Manipulation To Get Only The RequestID Number From Path
RequestID = Right(Path, Len(Path) - Application.WorksheetFunction.Find("wo/", Path) - 2)
'Set IE.Navigate Equal To The URL Of The Print View Ticket Print Preview Windows That Appears
IE.Navigate "https://help.psdschools.org/helpdesk/WebObjects/Helpdesk.woa/wo/" & RequestID & ""
'Set
IE.Visible = True
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
'Send Print Job Directly To The Default Printer Without Any User Interaction
IE.ExecWB 6, 2
End If
Next
Is there something wrong in my code or is there a different approach I should be taking to print a child popup window in a web browser?
Here is the code that establishes the connection with the parent window:
Code:
GoToWebsite:
'URL For The Ticketing System
URL = "https://help.psdschools.org/helpdesk/WebObjects/Helpdesk.woa"
'Create InternetExplorer Object
'Set IE = New SHDocVw.InternetExplorerMedium
Set IE = New SHDocVw.InternetExplorerMedium
'Make Internet Explorer Visible
IE.Visible = True
'Go to UR Specified Above
IE.Navigate URL
'Get Screen Resolution Width in Points
ScreenWidth = GetSystemMetrics32(0)
'Get Screen Resolution Height in Points
ScreenHeight = GetSystemMetrics32(1)
'Set Internet Explorer Window To Match Monitor Screen Resolution With Position Being In The Top-Left Corner
IE.Left = 0
IE.Top = 0
IE.Width = ScreenWidth
IE.Height = ScreenHeight
'Wait Until Internet Explorer Is Not Busy
While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Thank much!