Hi all,
I am trying to make a macro that opens a site and prints an overview. It should be repeated multiple times so I need all pages to be closed in the end. I can't figure out how to close a Webpage dialog.
I've tried IE.quit but that does not work. Does anybody know how to get this done?
Thanks in advance!
I am trying to make a macro that opens a site and prints an overview. It should be repeated multiple times so I need all pages to be closed in the end. I can't figure out how to close a Webpage dialog.
I've tried IE.quit but that does not work. Does anybody know how to get this done?
Thanks in advance!
Code:
Private Sub FirstOrder()
'This will load a webpage in IE
Dim Browser As InternetExplorer
Dim i As Long
Dim Url As String
Dim IE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Dim Shipment As String
Shipment = ThisWorkbook.Sheets("Orderuitgifte").Range("E18")
'Create InternetExplorer Object
Set IE = New InternetExplorerMedium
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
Url = "[URL]https://spl.dhl.com/smcfs/console/BR2shipment.search[/URL]"
'Navigate to URL
IE.Navigate Url
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
Set objCollection = IE.Document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "xml:/Shipment/@ShipmentNo" Then
' Set text for search
objCollection(i).Value = Shipment
Else
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "btnSearch" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
Set objCollection = IE.Document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "xml:/Shipment/@ShipmentNo" Then
' Set text for search
objCollection(i).Value = Shipment
Else
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "btnSearch" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
objElement.Click ' click button to search
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
Set objCollection = IE.Document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "EntityKey" Then
'Check checkbox
objCollection(i).Checked = True
End If
i = i + 1
Wend
IE.Document.getElementById("scbutton1").Click
Application.Wait (Now() + TimeValue("00:00:03"))
'IE.Document.getElementById("scbutton1").Click
'Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
'Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
IE.Document.getElementById("scbutton8").Click
IE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
'Application.Dialogs(xlDialogPrint).Show
IE.Quit
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub