?
Solved

need to save the complete html of a webpage using ie using vba access

Posted on 2016-10-07
10
Medium Priority
?
256 Views
Last Modified: 2016-11-17
i am trying to save the complete webpage so it looks basically like a print screen, with all the formatting using vba.  I am using the internet explorer object.  I want it to save a copy like internet explorer does when you do a saveas from the internet explorer file menu and saving it as a webpage complete html.



S = IE.Document.documentElement.outerHTML ' get html file

Fname = "Test.html"
Open Fname For Output As #1
Print #1, S ' save the header file
Close #1
0
Comment
Question by:hal60
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
10 Comments
 
LVL 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 1000 total points
ID: 41834404
You can use this code and function:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long


Public Function DownloadFile( _
    ByVal strURL As String, _
    ByVal strLocalFilename As String) _
    As Long
  
' Download file or page with public access from the web.
' 2004-12-17. Cactus Data ApS, CPH.

' Usage, download a file:
' lngRet = DownloadFile("http://www.databaseadvisors.com/Graphics/conf2002/2002ConferencePicsbySmolin/images/dba02smolin27.jpg", "c:\happybassett.jpg")
'
' Usage, download a page:
' lngRet = DownloadFile("http://www.databaseadvisors.com/conf2002/conf200202.asp", "c:\dbaconference.htm")

' Returns 0 if success, error code if not.
' Error codes:
' -2146697210 "file not found".
' -2146697211 "domain not found".

' Limitation.
' Does not check if local file was created successfully.

    Dim lngRetVal As Long
      
    lngRetVal = URLDownloadToFile(0, strURL & vbNullChar, strLocalFilename & vbNullChar, 0, 0)
    
    DownloadFile = lngRetVal
  
End Function

Open in new window

/gustav
0
 

Author Comment

by:hal60
ID: 41839835
That did not work for my case. all it did was force me to login to the site. it is a secure website and it somehow doesn't like this method.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41839840
Well, you didn't mention anything about a login. Then you need the browser object.

I don't have code for that, sorry.

/gustav
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41840268
0
 
LVL 20

Accepted Solution

by:
darbid73 earned 1000 total points
ID: 41851743
Hi Hal60,

I will try and help as well. I would suggest you open IE and login yourself and then you use vba to get what you want.  If that works well then you can extend it to also automatically logging in.

Have a look at post #6.

If TypeOf IE Is SHDocVw.InternetExplorer And IE.LocationURL <> "" And InStr(IE.LocationURL, "file://") <> 1 Then
                    Set Get_IE_Window2 = IE
                End If

Open in new window

This code about you need to customize in case you have more than one IE tab open by checking the location URL.

Once you have good code to get the currently open IE on the tab where you have already logged in then use code like is in this question.  Note that the code you need to customize here is which part of the document you want.  So body, inner or outer html etc.

If you put this together and it does not work or you still have questions post the code you have put together and point out which line is still not doing what you want.
0
 

Author Comment

by:hal60
ID: 41852353
i tried using Ie.Document.execCommand "SaveAs", True, X   it kind of works but you can't specify a destination file(goes to a default folder) .  the dialog save as comes up but prevents the code from continuing so i have to manually enter ok (which kind of defeats the purpose of automation), i would have liked to sned a key from the code.   I tried changing true or false which didn't matter.and  The only way a dialog box would come up is if i put a filename that didn't have a subfolder. for example  c:\test.htm would bring up dialog  but c:\TEST2\test.htm did not  weird
0
 
LVL 20

Expert Comment

by:darbid73
ID: 41852597
That is why that person also asked the question.

The next person quotes from the information on that method...

"The Save HTML Document dialog cannot be suppressed when calling this method from script."
0
 
LVL 20

Expert Comment

by:darbid73
ID: 41891807
Gustav's post #a41834404 is a solution to the question on downloading a file, however the author then added a further comment which meant that this solution could not be used.

Darbid's post at #a41851743 is a solution to the authors question and further comments. The link forum question offer 2 solutions, it appears the author has tried the first solution as he comments above he does not like it.  The second solution is exactly what he needs and I assume he is using this and it suits him which is why he has not replied.

I suggest a split of points between Gustav and Darbid.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question