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

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
John_w posted some code here, http://www.mrexcel.com/forum/showthread.php?t=507871, for dealing with the download dialog in IE.

Not sure if it's particularly relevant and it is just for the download dialog but perhaps it's worth a look anyway.

By the way do you know the full address and name of the PDF?
 
Upvote 0
John_w posted some code here, http://www.mrexcel.com/forum/showthread.php?t=507871, for dealing with the download dialog in IE.

Not sure if it's particularly relevant and it is just for the download dialog but perhaps it's worth a look anyway.

By the way do you know the full address and name of the PDF?


Thanks Norie, I'll give it a go. It looks like I was on the right track trying to find the Download window but I got hung up with activating the save-as button. I will try the code John_W posted. Cheers.
 
Upvote 0
EDIT: This works, i have edited the post to show the correct code


arkus,
This is what I have so far, to test, first have an open pdf file/webpage

This code creates a folder on your desktop named invoices and date

Sub test()
Dim DTAddress As String
DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Dim fso
Dim fol As String
fol = DTAddress & "Invoices " & Format(Date, "mm.dd.yy hh.mmAMPM") ' change to match the folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
Else
MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
End
End If

This bit of code cycles through each window you have open.
When it reaches a pdf window, it attemps to save it, however I cant get it to save without using send keys
Make sure you have you Adobe, Internet controls, and html references on


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.Visible = True
AcroPDF.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, fol & "5"

AcroPDF.Quit
End If
Next
End Sub
 
Last edited:
Upvote 0
I ran out of edits, so here is the working code

Sub test()
Dim DTAddress As String
DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Dim fso
Dim fol As String
fol = DTAddress & "Invoices " & Format(Date, "mm.dd.yy") ' change to match the folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
Else
MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
End
End If

Dim Shell As Object
Dim AcroPDF As Object
Set Shell = CreateObject("Shell.Application")
Application.Wait Now + TimeValue("00:00:02")
For Each AcroPDF In Shell.Windows
If TypeName(AcroPDF.Document) = "AcroPDF" Then
AcroPDF.Visible = True
AcroPDF.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, fol & ActiveCell.Value
'AcroPDF.SaveAs fol & "\" & ActiveCell.Value
Application.SendKeys fol & "\" & ActiveCell.Value

Application.Wait Now + TimeValue("00:00:01")
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "~", True
'Application.Wait Now + TimeValue("00:00:02")
'AcroPDF.Quit
End If
Next
End Sub
 
Upvote 0
I ran out of edits, so here is the working code

Sub test()
Dim DTAddress As String
DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Dim fso
Dim fol As String
fol = DTAddress & "Invoices " & Format(Date, "mm.dd.yy") ' change to match the folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
Else
MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
End
End If

Dim Shell As Object
Dim AcroPDF As Object
Set Shell = CreateObject("Shell.Application")
Application.Wait Now + TimeValue("00:00:02")
For Each AcroPDF In Shell.Windows
If TypeName(AcroPDF.Document) = "AcroPDF" Then
AcroPDF.Visible = True
AcroPDF.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, fol & ActiveCell.Value
'AcroPDF.SaveAs fol & "\" & ActiveCell.Value
Application.SendKeys fol & "\" & ActiveCell.Value

Application.Wait Now + TimeValue("00:00:01")
Application.SendKeys "{TAB}", True
Application.SendKeys "{TAB}", True
Application.SendKeys "~", True
'Application.Wait Now + TimeValue("00:00:02")
'AcroPDF.Quit
End If
Next
End Sub

Hi. was anyone able to solve this? I mean save the PDF to the folder path? This code currenlty saves the PDF to Desktop/My Documents. I would like to save it in a defined path. Please help me
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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