This is my first time trying to create a macro and I'm stuck. I'm trying to automate a tedious process where I have to pull a series of Heading 1 (title), Heading 2 (author), and Heading 3's (date) and create a table of contents. I've tried using the ToC in Word, but I can't customize it to look exactly like I need. Basically, I need to copy and paste H2 at the top of the page, make it italicized, put a colon after it, copy and paste H1, hyperlink it so it takes you to H1 in the doc, then copy and paste H3 in paratheses so that it always reads in the (Month Day, Year) format. Then I need it to go to the next set and do the same thing, but paste it underneath the first one.
Here is an example of what it would look like:
Author:
Title (February 11, 2016)
And the headings in the body look like:
Title (H1)
Author (H2)
11 Feb 2016 (H3)
I've tried to cobble together bits and pieces from other forums and ended up with the below. However, it is pasting H1 and H2 twice at the top of the page, and I'm not even getting close to italicizing and hyperlinking. So, I realize that I'm very far away and I would have liked to have included a better attempt, but I'm a bit out of my depth. Any help would be greatly appreciated.
Code:
Sub HeadingTest4()
'
' HeadingTest4 Macro
'
'
While Selection.Characters.Last.Next.Style = "Heading 1"
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Wend
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Collapse Direction:=wdCollapseEnd
With Selection.Find
.ClearFormatting
.Text = ""
.MatchWildcards = False
.Forward = True
.Style = ActiveDocument.Styles("Heading 1")
.Execute
.ClearFormatting
End With
Selection.Find.Execute
Selection.Copy
Selection.MoveUp Unit:=wdWindow, Count:=1
Selection.PasteAndFormat (wdListDontMerge)
Selection.PasteAndFormat (wdFormatPlainText)
While Selection.Characters.Last.Next.Style = "Heading 2"
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Wend
Selection.MoveStart Unit:=wdCharacter, Count:=1
Selection.Collapse Direction:=wdCollapseEnd
With Selection.Find
.ClearFormatting
.Text = ""
.MatchWildcards = False
.Forward = True
.Style = ActiveDocument.Styles("Heading 2")
.Execute
.ClearFormatting
End With
Selection.Find.Execute
Selection.Copy
Selection.MoveUp Unit:=wdWindow, Count:=1
Selection.PasteAndFormat (wdListDontMerge)
Selection.PasteAndFormat (wdFormatPlainText)
End Sub