VBA to recognize new IE Window vs PDF

Darkspartan

New Member
Joined
Jul 7, 2011
Messages
48
Hi,
This is my first time posting but I have been in vba for about 6 months now.
I am having trouble getting my code to recognize a PDF file that opens in a new IE window. The window is an invoice image that opens when you click a "submit" button.

I currently have code that works with identifying new IE windows, shown below...

Option Explicit
Public WithEvents IE1 As InternetExplorer
Public WithEvents IE2 As InternetExplorer

and

Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Set IE2 = New InternetExplorer
Set ppDisp = IE2.Application
Debug.Print "NewWindow2"
End Sub

This code works on every popup I have come across except for this one I am having trouble with (pdf image).
I have also tried a work around method with the below code, with which I am able to get the pdf image to "quit", or close, but cant get it to print or save. The code cycles through windows until it hits the PDF window.

Public Sub CloseIE()
Dim Shell As Object
Dim AcroPDF As Object
Set Shell = CreateObject("Shell.Application")

For Each AcroPDF In Shell.Windows
If TypeName(AcroPDF.document) = "AcroPDF" Then
AcroPDF.Quit
MsgBox AcroPDF.Name
AcroPDF.Quit
End If
Next
End Sub

The PDF name however is "Microsoft Internet Explorer"
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
As an update,
I am able to get the Window to quit and print now. I cannot however, get it to print without the print promt. Would anyone be able to assist?

Public Sub CloseIE()
Dim Shell As Object
Dim AcroPDF As Object
Set Shell = CreateObject("Shell.Application")

For Each AcroPDF In Shell.Windows
If TypeName(AcroPDF.document) = "AcroPDF" Then

With AcroPDF
.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End With

End If
Next
End Sub
 
Upvote 0
I was able to figure it out.

For some reason, I am still prompted to print even though the code says not to.
I have resorted to using send keys.

If anyone has a suggestion to where I do not have to do this work around, please post :-)
 
Upvote 0
Sorry no suggestion. But a Question; Are you able to get a PDF to save? I have been trying to automate a csv and PDF download but I get stuck at the Save-as dialoge box. I have not successfully been able to use send keys to make it save. Would you be willing to share your code or at least parts of it?
Thanks

Mark
 
Upvote 0
Hello,

Try this to cancel the print:

Code:
Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
cancel = true
Set IE2 = New InternetExplorer
Set ppDisp = IE2.Application
Debug.Print "NewWindow2"
End Sub
 
Upvote 0
Sorry no suggestion. But a Question; Are you able to get a PDF to save? I have been trying to automate a csv and PDF download but I get stuck at the Save-as dialoge box. I have not successfully been able to use send keys to make it save. Would you be willing to share your code or at least parts of it?
Thanks

Mark
Hi ArkusM,
I actually have to do that myself so I will let you know as soon as I figure it out :)
 
Upvote 0
Hello,

Try this to cancel the print:

Code:
Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
cancel = true
Set IE2 = New InternetExplorer
Set ppDisp = IE2.Application
Debug.Print "NewWindow2"
End Sub
Hi repairman,
I actually use this bit of code on another webpage we have and it works great.
Thing is, I want it to print the image (invoice) which happens to be a pdf in a ie window. I use the below code, but it still prompts the user with a print box

With AcroPDF
.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End With
 
Upvote 0
AcroPDF.Exec WBOLECMDID_SAVEAS, OLECMDEXECOPT_DONTPROMPTUSER
Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "~", True
AcroPDF.Quit

This code works for a "save as" for excel, so I am going to try and modify it for that to automate the file name
ActiveWorkbook.SaveAs Filename:=ThisWeeksFileLocation & "CancatinateReport.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
 
Upvote 0
Cool. Keep us posted. I have had no luck getting the save-as to work but I will try to adapt your code and see what if I can make it work... it would be great.

Cheers,

Mark
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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