Web Query Password Update Wanted

t01te6

Board Regular
Joined
Apr 3, 2008
Messages
93
Hey All,

Have just been doing a big search of the internet to see what I can do to put a web query into Excel that will first log into a password protected site and then retrieve the data I want.

I have gone through all the posts on Mr Excel which are excellent but as they were in 2005 Im wondering if there has been any updates, is there anything new which will help me do this.

I tried to use the following code on an open event when a workbook is open

Code:
Private Sub Workbook_Open()
'Open site if it requires a PassWord Model!
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Go to this Web Page!
IE.Navigate "Webpage"
'Check for good connection to web page loop!
Do
If IE.ReadyState = 4 Then
IE.Visible = True
Exit Do
Else
DoEvents
End If
Loop
'Wait for window to open!
Application.Wait (Now + TimeValue("0:00:05"))
'MsgBox "Done"
IE.Visible = True
'Send logg-on information!
'May need additional SendKeys as needed?
'Determine by the actual key-strokes needed!
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "Username", True
SendKeys "{TAB}", True
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "Password", True
SendKeys "(TAB)", True
Application.Wait (Now + TimeValue("0:00:05"))
SendKeys "{ENTER}", True
End Sub

and then hoped that if the page is now active the webqueries I had put in earlier would work, they did not.

So does anyone have any knowledge of this area that may help, i.e how can I get the web query to refresh every time it is opening on a password protected site.

Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
If you have tabbed browing, use the Browser control in userform.
Download Example.<a href="http://home.fuse.net/tstom/324416.0612080735.zip"><img src="http://home.fuse.net/tstom/zip.gif"width="16"height="16"border="0"></a> <a href="http://home.fuse.net/tstom/324416.0612080735.zip">324416.0612080735.zip</a>

Assumes UserForm1 containing WebBrowser1
Code:
Option Explicit

Private Sub Workbook_Open()
    Dim uf1 As New UserForm1, ie As InternetExplorer
    
    Set ie = uf1.WebBrowser1
    ie.RegisterAsBrowser = True
    ie.Navigate "https://sedsh04.sedsh.gov.uk/esd/xsql/sys_getLogin.xsql"
    Do Until Not ie.busy And ie.ReadyState = 4: DoEvents: Loop
    
    uf1.Show vbModeless

    With ie.document
        .all("username").Value = "username"
        .all("password").Value = "password"
        .all("submit_button").Click
    End With
    
    Do Until ie.ReadyState = 4 And Not ie.busy: DoEvents: Loop
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Thank you for your help, you guys are very clever at this,

the problem is that I can get the code to open and gain access to the website, but I want to run this with a web query.

Now I know Microsoft stopped allowing excel to save passwords and stuff so I am trying to find away around this,

Once I get passed the log in screen I have to navigate through a few more pages before I go into one particular customers area and want to retrieve a table of data.

I need however to do this for several customers, I can do it with with Sendkeys but your talking a lot of commands and that would have to be done for each of the 100's of customers.

So how do I get excel to link me allowing a website to become accessible and Excel continuing to update the web query in the workbook together without saying it cannot access the site.

Does that make sense

I feel there must be a way. I am determined to find it.
 
Upvote 0
I don't think that you can. As far as I know Excel maintains its own instance of IE when performing a web query. This instance is not accessible to the VBA programmer. I would like to be proved wrong...

I've not tried it yet, will shortly, but doubt that the IE that is displayed within the web query window is registered in the running object table or is available as a shelled window. You would have to obtain a reference to the instance of IE within the web query window and then the document contained within. I'll give it a shot and report back...

If it fails, you can use the method I prescribed and gather the tables data by way of code. So al is not lost in any case...

Nevermind. I'm not going to bother. The query window is modal and would prevent code from running and there is no way to save the login process within the query definition. You will need to skip Excel's query and do it with code. I dare somebody to prove me wrong... :)
 
Last edited by a moderator:
Upvote 0
Right_Click - I've tried combining InternetExplorer automation with a web query directly to an external web site, but without success. I tried creating a web query using the Macro Recorder and then modifying the code to change:
Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;https://www.asecuresite.com", Destination:=Range("A1"))
to:
Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;" & sURL, Destination:=Range("A1"))
where sURL is a string containing the value of ie.LocationURL (the same https://www.asecuresite.com). However, when the QueryTables Refresh method is called, this results in 'Unable to open https://www.asecuresite.com. Cannot download the information you requested'.

However, I found a workaround at http://www.tek-tips.com/faqs.cfm?fid=6400, which saves the HTML source to a local file and runs the QueryTables.Add on the local file, like this:
Code:
Private Sub WebQueryExcelQuestionsLocalFile(sOuterHTML As String)

    'Web query to get table containing list of threads on the MrExcel Excel Questions forum
    
    Dim sLocalHTMLFile As String
    
    sLocalHTMLFile = CreateLocalHTMLFile(sOuterHTML)
    
    'Original, as produced by Macro Recorder
    
    'With ActiveSheet.QueryTables.Add(Connection:= _
    '    "URL;http://www.mrexcel.com/forum/forumdisplay.php?f=10", Destination:=Range("A1"))
    
    'Modified version, using local file
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;" & sLocalHTMLFile, Destination:=Range("A1"))
        
        .Name = "forumdisplay.php?f=10"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingAll
        .WebTables = """threadslist"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    
End Sub

Private Function CreateLocalHTMLFile(sOuterHTML As String) As String

    Dim iFile As Integer
    
    CreateLocalHTMLFile = GetTempDir & "WebOutput.htm"
    iFile = FreeFile
    
    Open CreateLocalHTMLFile For Output As #iFile
    Print #iFile, sOuterHTML
    Close #iFile

End Function
You also need a module for GetTempDir:
Code:
'http://chrisrae.com/vba/routines/gettemppath.html

Option Explicit

Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
                (ByVal nBufferLength As Long, _
                ByVal lpBuffer As String) As Long

Const MAX_PATH = 256

Public Function GetTempDir() As String
    Dim sBuffer As String
    Dim lRetVal As Long

    sBuffer = String(MAX_PATH, vbNullChar)

    lRetVal = GetTempPath(Len(sBuffer), sBuffer)

    If lRetVal Then
        GetTempDir = Left$(sBuffer, lRetVal)
    Else
        GetTempDir = vbNullString
    End If
End Function
In summary, the technique 'web queries' the external site indirectly via a local temporary file. Of course, you still have to write code to navigate to the page you want to run the web query on.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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