![]() |
|
#1
|
|||
|
|||
|
I need to split a large file into two separate files with the main text in one file and all of the endnotes in the other, but without losing the superscript references exactly as they were originally. I've found a macro that will split endnotes off, with referents, but they start at 1 and go up into the hundreds. I currently have the file use small letters to mark endnotes, and they start over at "a" in every new chapter. How would I get a macro, either to read and reproduce the letter used originally, or detect that it has entered a new chapter/section and start over? (numbers would be okay instead of letters if they'd just start over when they're supposed to). |
|
#2
|
|||
|
|||
|
I probably should have included the code for what I've found
Code:
Dim aendnote As Endnote
For Each aendnote In ActiveDocument.Endnotes
ActiveDocument.Range.InsertAfter vbCr & aendnote.Index & vbTab & aendnote.Range
aendnote.Reference.InsertBefore "a" & aendnote.Index & "a"
Next aendnote
For Each aendnote In ActiveDocument.Endnotes
aendnote.Reference.Delete
Next aendnote
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = True
End With
With Selection.Find
.Text = "(a)([0-9]{1,})(a)"
.Replacement.Text = "\2"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
|
|
#3
|
||||
|
||||
|
Copying the endnotes to another file is quite simple; preserving their references isn't. Do you want them preserved in both files? I take it, too, you realize they'll no longer function as endnotes once you've done this?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
Yes, I realize. Basically, I need to convert thousands of endnotes into a self-contained study-guide to a separate textbook, with graphics and charts added to this guide, so they can't be left as endnotes. I'll add the graphics only after the book is finalized, so I won't need the endnotes to self-update after that (and this way, with section breaks, if anything does get changed, it will only affect one chapter's notes and numbering, so it won't be a complete disaster).
I think I’ve maybe figured out a general idea how it could be solved, but a few things are beyond my newbie VBA skills. I plan to dim an array search for each section break search for the first endnote after the break record this endnote’s index in my array then run the macro I presented, but instead of inserting aendnote.Index I’d subtract from it the highest value in my array less than it (+1) and insert that into the study guide, (and convert the index into a letter, that the textbook people prefer). If anybody’s got an easier way, I’d love to save myself the trouble. Otherwise maybe you can answer a couple things. If I search for an endnote with a simple Find “^e” how do I extract the index of the endnote I just landed on? Would it be something like Selection.Endnotes.Item.Index? That’s pure guesswork based on the options I got each time I hit a period. I have no idea if I’m on the right track. I assume all these properties are cataloged somewhere, but I'm flying blind. Are there any other landmines you see looming on my path. I’m an old basic programmer (who hasn't programmed in a couple decades), but this is my first significant encounter with VBA and I’m a little out of my depth. (The work is for a non-profit that can't afford a real programmer, so they're stuck with me). |
|
#5
|
||||
|
||||
|
Try:
Code:
Sub ExportEndNotes()
Application.ScreenUpdating = False
Dim Rng As Range, DocSrc As Document, DocTgt As Document, i As Long, StrRef As String
Set DocSrc = ActiveDocument
With DocSrc
'Unlink endnote/footnote cross-references
For i = .Fields.Count To 1 Step -1
If .Fields(i).Type = wdFieldNoteRef Then .Fields(i).Unlink
Next
'Process all endnotes
For i = 1 To .Endnotes.Count
'First, process the endnote ref in the document body
Set Rng = .Endnotes(i).Reference
With Rng
.Collapse wdCollapseStart
'To get the actual reference text, we need to cross-reference it!
.InsertCrossReference wdRefTypeEndnote, wdEndnoteNumberFormatted, i, False, False
.End = .End + 1
StrRef = .Text
.Fields(1).Unlink
End With
'Second, process the endnote ref in the endnote
Set Rng = .Endnotes(i).Range.Paragraphs.First.Range.Words.First
With Rng
If .Characters.Last Like "[ " & vbTab & "]" Then .End = .End - 1
'Overwrite the existing endnote reference
.Text = StrRef
End With
Next
'Give Word a chance to do its housekeeping
DoEvents
'Create the output document
Set DocTgt = Documents.Add
'Replicate the endnotes in the body of the output document
DocTgt.Range.FormattedText = .StoryRanges(wdEndnotesStory).FormattedText
'Delete the endnotes from the source document
For i = .Endnotes.Count To 1 Step -1
.Endnotes(i).Delete
Next
End With
Set Rng = Nothing: Set DocTgt = Nothing: Set DocSrc = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
Thanks, that works perfectly.
I think I was maybe getting close to a solution with my own method, but it was a mess, and frustration was rising faster than the problems were getting resolved, so this is much appreciated. I can't say I completely understand how it works, but it works. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Convert raw field codes to plain text and back again | ugcheleuce | Word VBA | 3 | 02-14-2017 05:03 AM |
Macro Needed To Convert Text in Word to Plain Text and Back to Word
|
rsrasc | Word VBA | 5 | 12-18-2015 07:13 AM |
Convert image-text hybrids into plain text
|
morlack | Excel | 4 | 12-03-2014 05:29 PM |
How to convert endnotes in a text doc to Word endnotes?
|
Dickison | Word VBA | 4 | 10-06-2012 09:11 PM |
| My plain text post got converted to rich text in a reply, how to convert it back? | david.karr | Outlook | 0 | 01-05-2012 09:46 AM |