View Single Post
 
Old 02-12-2016, 07:54 AM
Dretherix Dretherix is offline Windows 8 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 4
Dretherix is on a distinguished road
Default

So, I tried to edit something and forgot to click save, so it looks like I deleted my post. Here's the rewrite.

This is my first attempt to create a macro in Word, and I'm stuck. I'm trying to automate a tedious process where I use multiple Heading 1 (title), Heading 2 (author) and Heading 3's (date) to create a table of contents. I've tried using the build in ToC, but it doesn't allow for the specific customization that I need. Basically, I need to copy and paste the first H2 at the top of page, italicize it, put a colon after it, then copy and paste the first H1 after the colon, hyperlink it so that it goes to back to where it is located in the doc, add a space, then put H3 inside parentheses so that it allows follows the (Month Day, Year) format, and then do it all again for the next set.

Here is what it's supposed to look like:

Author : Title (February 12, 2016)

Here is what that is pulling from in the body:

Title (H1)
Author (H2)
12 Feb 2016 (H3)

I've cobbled together bits and pieces from this and other forums, but I'm a bit out of my depth. I've pasted below the parts that I can get to work, but it is pasting both the H1 and H2 twice at the top of the page, and I'm not even close to figuring out how to put them on the same line, italicize and hyperlink. I wish I could have included a better attempt. Any help is greatly appreciated.

Here is what I have so far:

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
Reply With Quote