Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-26-2025, 06:37 PM
dpdalton dpdalton is offline Windows 11 Office 2021
Novice
How to create a "sort of" TOC for Word Document containing copied Macros
 
Join Date: Nov 2025
Posts: 2
dpdalton is on a distinguished road
Default How to create a "sort of" TOC for Word Document containing copied Macros

Before I retired a few years back, I pasted a copy of the text of all my accumulated macros (~120) into a single Word document (@10 pt type & ss, it's 218 pages long) . . . just in case I might ever need any of that in the future. Since then, I haven't used most of those old macros, but there are several of them that I still use pretty regularly, as well as a few others I use occasionally.

Just a few days ago, I opened Word to create a new document, pasted some copied text into it, then clicked on the Macro Dialog Box to run one of my macros on that text and discovered that none of my macros was listed. So I opened a few of my earlier documents - no macros listed in any of them. Next, I opened Normal.dotm separately - no macros listed. So I contacted MS Support & spent hours with them (including granting them remote access). They couldn't find my macros or explain why they were gone except to say that maybe Normal.dotm had been overwritten, perhaps by an Update, so he did a System Restore to go back to several days ago. Nope. MS says they're all irretrievably just gone.

So now I CAN make use of that macro document I created by copying the text for just the macros I want into the macro editor, save that document, & then the macros should be saved in my new Normal.dotm.

Of course, that document is just a lot of sequential lines of copied macro text. While I know I can manually find and copy each macro I want when I need it, it occurred to me that it will be easier to find them if I can add a sort of TOC at the beginning of this document that will list all of these macros by name with a hyperlink to the beginning of each macro. That way, I can click on it & be taken to the beginning of it, then copy just that macro text, then paste it into the Macro Editor.

However, I've never done TOCs at all (that may not be the right tool anyway) and not a lot with bookmarks, but it seemed like creating bookmarks at the beginning of each macro would be a way to start, but finding the starting spot for each 120 times seemed like a lot of work. Luckily, I found an example macro by Macropod that looked like it would do that for me (it's at https://www.msofficeforums.com/word-...bookmarks.html), so now I've created a bookmark at the initial text ("Sub") of each macro.* Those bookmarks are named "_Sub001" through "_Sub120". The next step would be the list (and I don't know how to do that either), but I now realize that a list of those bookmark names isn't going to be very helpful, anyway. So, in addition to TOC maybe not being the right tool, inserting bookmarks may also be the wrong starting point.

Anyway, now I realize I can't figure out how to make this all happen as I'd like, so I'm hoping someone here can tell me how I can automate this to end up with a page (or 2) at the beginning of this document that lists just the names of these macros, with each having a hyperlink that will take me to the beginning of that macro text.

Of course, the name of each macro follows the "Sub " text on that same line [e.g., "Sub ThreeSpaceFix()" ], so that's the name I'd like to have appear as the hyperlink text on the list. Maybe something that goes to each bookmark I've placed, then copies the rest of the line (preferably without the "()" at the end)? But doing that is beyond my (mostly Find/Replace) macro skills.

Any thoughts, suggestions, . . . help?

Thanks in advance!



* Before I could use Macropod's bookmark macro, I used Find/Replace to apply a unique character style to each occurrence of "Sub " [but only where it appears at the beginning of each macro, as there's also "End Sub" text in each]. Once I applied that unique style to each macro, it worked great.

Also, all this made me wonder why is there not a "macro backup" feature in Word to help prevent this sort of complete loss of macros? I feel lucky for having created that document back "when".

(FWIW: Win 11 & "Microsoft 365 Apps for Business" subscription)
Reply With Quote
  #2  
Old 11-26-2025, 09:34 PM
macropod's Avatar
macropod macropod is online now How to create a "sort of" TOC for Word Document containing copied Macros Windows 10 How to create a "sort of" TOC for Word Document containing copied Macros Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could simply create a descriptive title paragraph for each sub using Word's Heading 1 Style, then insert a standard Table of Contents.

As for backing up your macros, any decent backup software should be configurable to do that. Simply tell it which folders/files to include.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-27-2025, 04:38 AM
Italophile Italophile is offline How to create a "sort of" TOC for Word Document containing copied Macros Windows 11 How to create a "sort of" TOC for Word Document containing copied Macros Office 2021
Expert
 
Join Date: Mar 2022
Posts: 563
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by dpdalton View Post
Also, all this made me wonder why is there not a "macro backup" feature in Word to help prevent this sort of complete loss of macros?
There is an option to export code modules. Simply right click on a module and select Export File
Screenshot 2025-11-27 113115.png

Instead of storing macros in your Normal template create a template for them and save it to Word's startup folder, typically %APPDATA%\Microsoft\Word\STARTUP
Reply With Quote
  #4  
Old 11-27-2025, 05:59 PM
Charles Kenyon Charles Kenyon is online now How to create a "sort of" TOC for Word Document containing copied Macros Windows 11 How to create a "sort of" TOC for Word Document containing copied Macros Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,584
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

You could save the document that contains your macros as macros rather than text as a .dotm. Then place it in your Word Startup Folder so it will be a Global Template. Your macros would then be available in every document.
Organizing your macros by Beth Melton, Word MVP
Reply With Quote
  #5  
Old 11-27-2025, 07:16 PM
dpdalton dpdalton is offline Windows 11 Office 2021
Novice
How to create a "sort of" TOC for Word Document containing copied Macros
 
Join Date: Nov 2025
Posts: 2
dpdalton is on a distinguished road
Default

Thanks all.

I think I wasn't clear about what I wanted to accomplish. So I'm going to try to better describe my goal. I do not want a TOC that uses the normal TOC Heading styles, because those all change the appearance of the text in the document. And I don't want the resultant "list" itself to have indented text lines.

I believe I've figured out how to make each of these items appear in a plain list (including the desired hyperlinks) by inserting Field Codes (FC). However, because this document has 120 of these macro items spread out over 215 pages, I really need a macro to help me locate and mark them all.

Here's what almost worked: I knew how to find each item (all begin with "Sub" followed by a space), so -- started at the top of the document -- I recorded a macro to find that string, then move (down) to the location of that macro's name, copy that name text, move 10 spaces to the right, then create the field code (pasting that copied name into it) and insert that field code at that location. After recording that macro, I confirmed the inserted field code for the first item was as I'd hoped, so I ran the macro. It found the second item and inserted a field code at the desired location, but not using the correct name text for that second item (it inserted the text from the first one).

Nevertheless, I tested the general concept by returning again to the top of the document, then creating there a Custom TOC (being sure to checkmark "Table entry fields" under Options). The attached screenshot of the top of the 1st page of the document shows the resultant list.

Other than the second item shows the wrong name, this is the sort of list I was looking for. But I don't know how to fix this recorded macro to do the copy/paste when/how I'd hoped. Here's that recorded macro:

Sub tmp()
'
' tmp Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Sub "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"TC ""SectionSign Macro"" ", PreserveFormatting:=False
End Sub

I can see at the end that the name of the first item appears "hard coded". Obviously, I'd wanted it to insert into the FC code field the text I thought it had copied just above. How to do that?

My other problem is that I don't know how to automate this macro so it will keep repeating itself in doing this "find and repeat action" process to similarly mark all of the remaining 118 macro items in this document.

Can someone please tell me how to fix these issues?

Thanks
Attached Images
File Type: jpg Screenshot 2025-11-27 192659.jpg (125.9 KB, 7 views)
Reply With Quote
  #6  
Old 11-27-2025, 10:15 PM
macropod's Avatar
macropod macropod is online now How to create a "sort of" TOC for Word Document containing copied Macros Windows 10 How to create a "sort of" TOC for Word Document containing copied Macros Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You can achieve the required Table of Contents result, as I said, via the Use of heading Styles. All you need to do is prefix each macro with an appropriate descriptor in the Heading 1 Style. For example, to use the macro name as the descriptor:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Sub [!\(^l^13]@\(\)"
    .Replacement.Text = ""
    .Forward = True
    .Format = False
    .Wrap = wdFindStop
    .MatchWildcards = True
  End With
  Do While .Find.Execute
    .Text = Split(Split(.Text, " ")(1), "()")(0) & " Macro" & vbCr & .Text
    .Paragraphs.First.Style = wdStyleHeading1
    .Collapse wdCollapseEnd
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
list, macros, toc

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preventing "Keep the last item you copied" popup on exit gogreen Word 0 09-15-2019 03:48 PM
How to create a "Font color" button and a "Highlight text" button in the Quick Launch toolbar ? gloub Word 12 02-19-2019 03:19 PM
Combine "sort within groups" and "identify duplicate/unique values" in two different variables. civilcervant Excel 3 06-12-2017 07:27 AM
How to create a "sort of" TOC for Word Document containing copied Macros How do I stop option for "new blog post" appearing when I create a new Word document? Kimu Word 3 10-27-2014 02:58 AM
Word crashing on "sort" routine Kayale Word 0 04-12-2010 04:54 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft