
Originally Posted by
gmaxey
Paul,
I've got to go to regular work, but tried to finish this before I left. I think, by using a temporary document, the table requirement can be met:
[vba]Sub ConvertHardEndNotesToDynamicFootnotes()
Dim oDoc As Word.Document
Dim oDocTemp As Word.Document
Dim lngIndex As Long
Dim oRngFNRef As Range, oRngFNs As Range
Dim arrFNs() As Long
Dim arrID() As String
Dim strFNRef As String, strFN_ID As String
'Get variable values from user.
strFNRef = InputBox("Enter the text string that identifies\enumerates each end\footnote in the end\footnote text. " & vbCr _
& "Be sure to include the trailing space and use ""#"" to represent the number. (e.g., ""Footnote # - "").", "Enumerator Text", "Footnote #: ")
arrID() = Split(strFNRef, " ")
strFN_ID = InputBox("Enter the text string that identifies\enumerates each end\footnote in the document text. " & vbCr _
& "Be sure to include any leading space and use ""#"" to represent the number. (e.g., "" FN#"").", "Enumerator Text", " FN#")
Application.ScreenUpdating = False
Set oDoc = ActiveDocument
'Reset all find and replace parameters to the default value.
ResetFRParameters
'Identify the the text of the endnote section of the document.
'This is all text from the first enumerated endnote, to the end of the document text.
With oDoc.Content
With .Find
'Find the first qualifying hard endnote.
.Text = Replace(strFNRef, "#", "([0-9]{1,})")
.MatchWildcards = True
.Execute
End With
If .Find.Found Then
Set oRngFNs = .Duplicate
Else
Beep
MsgBox "No applicable content found.", vbOKOnly
GoTo lbl_Exit
End If
oRngFNs.End = oDoc.Paragraphs.Last.Range.End
DoEvents
'Cut and paste this text to a temporary document.
oRngFNs.Cut
Set oDocTemp = Documents.Add
oDocTemp.Range.Paste
'Identify and store the range .start value of each hard endnote.
Set oRngFNs = oDocTemp.Range
With oRngFNs.Find
.MatchWildcards = True
.Text = Replace(strFNRef, "#", "([0-9]{1,})")
While .Execute
If .Found Then
ReDim Preserve arrFNs(lngIndex)
arrFNs(lngIndex) = oRngFNs.Start
lngIndex = lngIndex + 1
End If
Wend
ReDim Preserve arrFNs(lngIndex)
arrFNs(lngIndex) = oDocTemp.Paragraphs.Last.Range.End
End With
DoEvents
'Create the footnotes in the document.
For lngIndex = 0 To UBound(arrFNs) - 1
StatusBar = "Locating Footnote Reference: " & lngIndex
'Find the reference.
With .Find
.Text = Replace(strFN_ID, "#", "") & lngIndex + 1
'Delete/comment out the next line if not applicable
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.Execute
End With
If .Find.Found = True Then
Set oRngFNRef = .Duplicate
'Wipe the existing reference and create empty dynamic footnote.
oRngFNRef.Text = vbNullString
.Footnotes.Add Range:=oRngFNRef, Text:=""
'Get the footnote text from the temporary document.
Set oRgnFNs = oDocTemp.Range
With oRgnFNs
.Start = arrFNs(lngIndex)
.End = arrFNs(lngIndex + 1) - 1
.Start = .Words(UBound(arrID) + 1).Start
.Copy
End With
'Put it it in the dynamic footnote.
oDoc.Activate
oDoc.Footnotes(lngIndex + 1).Range.Characters.Last.Previous.Select
oDoc.Footnotes(lngIndex + 1).Range.Characters.Last.Previous.Paste
'oDoc.Footnotes(lngIndex + 1).Range.Style = "Footnote Text"
End If
DoEvents
Next lngIndex
End With
lbl_Exit:
oDocTemp.Close wdDoNotSaveChanges
Set oRngFNRef = Nothing: Set oRngFNs = Nothing
StatusBar = "Done!!"
Application.ScreenUpdating = True
End Sub
Sub ResetFRParameters()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop 'Continue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
[/vba]
What say ye?