Download webpage as mhtml?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a list of urls in column K. I want to be able to download them as mhtml files.

Here's what I have (which creates a file on my hard drive but I can't open it, it just opens multiple tabs and I have to CTRL-ALT-DEL to kill the process):
Code:
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
Sub GetPageTitle()
    Dim i As Long, url As String
    For i = 2 To 2
        DoEvents
        url = Sheet1.Range("K" & i).Value
        URLDownloadToFile 0, url, "D:\Zoo Escape\" & Sheet1.Range("j" & i).Value & ".mhtml", 0, 0
    Next
End Sub

Anyone managed to achieve something similar?

Many thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi

The following code should hopefully help. I've left your subroutine - GetPageTitle - alone as much as possible. Instead of using the API to down the HTML file, this should convert the URL to an MHTML file. Let me know if you have any problems implementing it.

VBA Code:
Sub GetPageTitle()
    Dim i As Long, URL As String, Destination As String
    For i = 2 To 2
        DoEvents
        URL = Sheet1.Range("K" & i).Value
        Destination = "D:\Zoo Escape\" & Sheet1.Range("j" & i).Value & ".mhtml"
        URLtoMHTML URL, Destination
    Next
End Sub

Sub URLtoMHTML(URL As String, MHTMLFile As String)
    With CreateObject("CDO.Message")
        .MimeFormatted = True
        .CreateMHTMLBody URL, 0, "", ""
        .GetStream.SaveToFile MHTMLFile, 2
    End With
End Sub
 
Upvote 0
Thanks for that, it's not quite doing what I want.

Files

So that link has two mhtml files - the one created by the VBA code and one created by Google Chrome when visiting the page and selecting CTRL-S (save as single web page .mhtml)

If you open them both (preferably in Chrome, don't use Firefox as it spawns hundreds of tabs and you'll need Task Manager to kill the process) you'll see the difference - the one via Chrome is twice the size and has complete functionality (as if you were actually on the webpage).

Hope I've explained myself and thank you for your time.
 
Upvote 0
Ahh,, I see. That makes sense - thank you for sending through the sample file. So I suspect that the reason for the difference is that the solution above leverages the internal Windows browser/rendering engine (Trident) to produce the MHTML file - that's the same engine as what powers Internet Explorer. And because Microsoft aren't supporting internet explorer for much longer, it doesn't benefit for any updates to produce prettier/modern websites for uses. There isn't much I can do about that, I'm afraid.

If it has to be an MHTML file, it might be possible to use Selenium to make a MHTML - which is used to interface between Excel and Chrome. I've not heard of anyone doing it, but then I don't ever hear about people wanting MHTML files either! :) I can check, though.

If I may ask, why do you need it to be an MHTML file and not a PDF file or an image capture of the webpage? To be honest, you're the only other person I've ever spoken to about MHTML files before - and I stopped using them a while ago. If a PDF or image would suffice, that would be much easier to solve.
 
Upvote 0
Ahh,, I see. That makes sense - thank you for sending through the sample file. So I suspect that the reason for the difference is that the solution above leverages the internal Windows browser/rendering engine (Trident) to produce the MHTML file - that's the same engine as what powers Internet Explorer. And because Microsoft aren't supporting internet explorer for much longer, it doesn't benefit for any updates to produce prettier/modern websites for uses. There isn't much I can do about that, I'm afraid.

If it has to be an MHTML file, it might be possible to use Selenium to make a MHTML - which is used to interface between Excel and Chrome. I've not heard of anyone doing it, but then I don't ever hear about people wanting MHTML files either! :) I can check, though.

If I may ask, why do you need it to be an MHTML file and not a PDF file or an image capture of the webpage? To be honest, you're the only other person I've ever spoken to about MHTML files before - and I stopped using them a while ago. If a PDF or image would suffice, that would be much easier to solve.
Thanks for taking the trouble to reply.

My end goal is to capture the dice rolls and the moves made by the two players (in the Google Chrome generated file, you can scroll through the list of dice rolls on the left hand side). If there's another way to capture the dice rolls and moves I'm all ears :)
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

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