HTML Doc in Excel

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I've just found if I drag an html file into a worksheet cell A1, it displays perfectly as formatted text. All I need do is set the font colour to black.
I have several dozen of these files to work on, can I emulate the process in VBA? If I read in the file like so
VBA Code:
    ff = FreeFile
    Open Path & "\file1.html" For Input As #ff
        Dat = Input(LOF(ff), 1)
    Close #ff
What would be an instruction to copy Dat to a worksheet ?

Second question, with the text showing, I run this
VBA Code:
Sub test()
For i = 7 To 13
Debug.Print Cells(i, "A")
Next
End Sub
The worksheet has an indent or margin on some rows but this is not reflected in the above code
I need to know this, how might I do that ?
Thanks for any help.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
For your first question, assuming that you want the data written to the active worksheet, try the following...

VBA Code:
    Dim pathAndFilename As String
    pathAndFilename = "C:\Users\Domenic\Desktop\sample.html" 'change the path and filename accordingly
    
    Dim fileNum As Long
    fileNum = FreeFile()
    
    Open pathAndFilename For Input As #fileNum
        Dim dataText As String
        dataText = Input(LOF(fileNum), fileNum)
    Close #fileNum
        
    Dim dataArray() As String
    dataArray = Split(dataText, vbCrLf)
    
    Range("A1").Resize(UBound(dataArray) + 1).Value = Application.Transpose(dataArray)

Hope this helps!
 
Upvote 0
When dragging/dropping an html file on a worksheet, it strips out he html code and shows just the text. Which is perfect!
Your method unfortunately displays all the text, so won't work. But is worth knowing anyway. Thanks.
But not being able to detect each row (and it's indent) in code negates the whole exercise. But I have found msWord will. But finding details of the command and instructions for Word VBA is very difficult and no forum seems to exists. So it's very slow going, but may be the best solution eventually.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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