[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Edit html using excel macro

Posted on 2012-03-16
8
Medium Priority
?
258 Views
Last Modified: 2012-03-18
Hi experts,

I have to edit urgent html text files with macro. I tried to import html file into excel but the text file is broken into parts according cell size. The view is the same but I have to add the cells content to have the original line.
How to solve this job?  Any ideas appreciated.

wbr kacor
0
Comment
Question by:kacor
[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
  • 5
  • 3
8 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 37731784
I would read the text file using old fashioned methods.

Here's an example that will read your input into Excel, more "properly".

Option Explicit
Sub readHTMLTxtFile()
Dim wkb As Workbook
Dim wks As Worksheet
Dim wksOut As Worksheet
Dim dialogfile As FileDialog
Dim fName As String
Dim wholeLine As String
Dim i As Long

    Set wkb = ThisWorkbook
    Set wks = wkb.Worksheets("Control Panel")
    
    On Error Resume Next
    Set wksOut = wkb.Worksheets("Output")
    If Err.Number <> 0 Then
        Set wksOut = wkb.Worksheets.Add(after:=wkb.Worksheets("Control Panel"))
        wksOut.Name = "Output"
    End If
    On Error GoTo 0
    
    wksOut.Cells.Clear 'clear old output, if exists
    
    'prompt for txt file to read
    
    Set dialogfile = Application.FileDialog(msoFileDialogFilePicker)
    With dialogfile
        .Filters.Clear
        .Filters.Add "Text Files (*.txt)", "*.txt", 1 'change extension to .html if that's your extension
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = ThisWorkbook.Path & "\"
        .Title = "Select TEXT File for HTML import"
        .Show
    End With
    If dialogfile.SelectedItems.Count > 0 Then
        fName = dialogfile.SelectedItems(1)
    Else
        fName = ""
    End If
    
    If fName <> "" Then 'read the file in
        Open fName For Input As #1
        Do While Not EOF(1) 'check for end of file
            Line Input #1, wholeLine
            'do something with it
            wksOut.Range("A1").Offset(i, 0).Value = wholeLine
            i = i + 1
        Loop
        wksOut.Columns("A").AutoFit
        Close #1
    End If

End Sub

Open in new window


See attached demonstration workbook.

Cheers,

Dave
readHTMLTxt-r1.xls
0
 
LVL 42

Accepted Solution

by:
dlmille earned 2000 total points
ID: 37731801
Modified to allow you to read HTML files or TXT files (note the filter in the dialog allows you to change from HTML to TXT.  I thought you'd get use out of it with just TXT files as well, and you can add more filters for this.  The dataset goes into A column of the output sheet, as its read in.
Option Explicit
Sub readHTMLTxtFile()
Dim wkb As Workbook
Dim wks As Worksheet
Dim wksOut As Worksheet
Dim dialogfile As FileDialog
Dim fName As String
Dim wholeLine As String
Dim i As Long

    Set wkb = ThisWorkbook
    Set wks = wkb.Worksheets("Control Panel")
    
    On Error Resume Next
    Set wksOut = wkb.Worksheets("Output")
    If Err.Number <> 0 Then
        Set wksOut = wkb.Worksheets.Add(after:=wkb.Worksheets("Control Panel"))
        wksOut.Name = "Output"
    End If
    On Error GoTo 0
    
    wksOut.Cells.Clear 'clear old output, if exists
    
    'prompt for txt file to read
    
    Set dialogfile = Application.FileDialog(msoFileDialogFilePicker)
    With dialogfile
        .Filters.Clear
        .Filters.Add "HTML Files (*.html)", "*.html", 1
        .Filters.Add "Text Files (*.txt)", "*.txt", 2 'change to .html if your text file has that extension
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        .InitialFileName = ThisWorkbook.Path & "\"
        .Title = "Select TEXT File for HTML import"
        .Show
    End With
    If dialogfile.SelectedItems.Count > 0 Then
        fName = dialogfile.SelectedItems(1)
    Else
        fName = ""
    End If
    
    If fName <> "" Then 'read the file in
        Open fName For Input As #1
        Do While Not EOF(1) 'check for end of file
            Line Input #1, wholeLine
            'do something with it
            wksOut.Range("A1").Offset(i, 0).Value = wholeLine
            i = i + 1
        Loop
        wksOut.Columns("A").AutoFit
        Close #1
    End If

End Sub

Open in new window


See attached demonstration workbook.

Dave
readHTMLTxt-r2.xls
0
 
LVL 10

Author Comment

by:kacor
ID: 37732028
Hi Dave,
thanks for the quick answer. Now I'm on the way to return home, and I'll test your suggestion asap.
I've not tested but I see a problem: they can occur some to long lines even up to 1 k or more. Would be this problem?

wbr kacor
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 42

Expert Comment

by:dlmille
ID: 37732045
In Excel 2003, a cell will hold 32767 characters, display 1024, but all is editable in the formula bar.

You asked to have the ability to read it all in without breaking lines.  Its now up to you to determine how you want to use the data in Excel.

Dave
0
 
LVL 10

Author Comment

by:kacor
ID: 37732048
thanks Dave!

kacor
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37732052
You might try using Notepad++ also as an alternative.  I believe you can google and download for free.

Cheers,

Dave
0
 
LVL 10

Author Comment

by:kacor
ID: 37734598
Hi Dave,
your macro is excellent. The only needed change was - as you wrote - in line 30 the .txt extension changed to .htm

thanks again for this support!!

wbr Janos
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37735211
You're welcome.  The second post has both the HTML and TXT options.

Cheers,

Dave
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

649 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