Macro for Excel: create HTML table of contents

Note – I am not responsible for any problems that may arise from using a macro!

Let’s say you want to create an HTML table of contents, based on h2 tags. See the macro below.

Start with:

Run the macro, get this data:

(the last row is for easy copy&pasting; the #0 links need to manually be updated to #01, #02 etc. The a name needs to be made into 01, 02 etc. (no #, for sure).

How to install a Macro for Word? Details #1 ». Details #2.

You can DOWNLOAD THE MACRO HERE ».

This is the macro:

Sub TableOfContents_FromH2Tags()

Selection.HomeKey Unit:=wdStory
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.TypeText Text:=”<b>Contents:</b>”
Selection.TypeParagraph
Selection.TypeText Text:=”<ul>”
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.TypeText Text:=”</ul>”
Selection.TypeParagraph
Selection.TypeText Text:=”<a name=””0″”></a>”
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = „<h2>”
.Replacement.Text = „<li><a href=””#0″”>”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = „</h2>”
.Replacement.Text = „</a></li>”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
End Sub

 

Share on WhatsApp

Lasă un comentariu

Rules for commenters »

Puteți folosi Gravatar pentru a adăuga avatar (imagine comentarii).