Searching for a variable? (Converting ascii footnotes)

P

Paul B

Hi,

I often convert plain text documents that have footnotes in the
ascii form "[#]".

I'm trying to write a macro that
- finds each in-text instance of a given number, [#]
- finds and cuts the corresponding footnote text with the same
number
- converts the first location to a footnote
- pastes the cut footnote body into the new footnote body
location.

My game plan is to search incrementally for the numbers, but to
do that I think I need to use each number as a variable so I can
search for it twice.

Can Word search for a variable? Or is there a better way - or an
existing way - to approach this?

Thanks,
p.
 
M

macropod

Hi Paul,

If I understand it correctly your document looks something like:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[1]. The quick brown fox jumps over the lazy
dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[2].

[1] The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
[2] The quick brown fox jumps over the lazy dog.

with the 'footnote' indicators in-line with the text and the footnotes are grouped together like endnotes. Is that correct?
 
P

Paul B

Hi Paul,

If I understand it correctly your document looks something like:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[1]. The quick brown fox jumps over the lazy
dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[2].

[1] The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
[2] The quick brown fox jumps over the lazy dog.

with the 'footnote' indicators in-line with the text and the footnotes are grouped together like endnotes. Is that correct?


Hi Macropod,

Yes, that's almost exactly the case. The one slight difference is
in the spacing:

The quick brown fox jumps over the lazy dog. The quick brown fox
jumps over the lazy dog. [1] The quick brown fox jumps over the
lazy...

Since these documents are pageless, the footnotes have been
deposited at each chapter's end, which can be many pages away. So
the way I see it, the macro would have to search ahead for the
corresponding footnote number (it's highly unlikely there would
be any false hits on the [#] string) in order to locate the
footnote body text.

bb,
p.
 
M

macropod

Hi Paul,

Try the following macro:

Sub ReLinkFootNotes()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
.Bookmarks.Add Range:=.Range, Name:="FootNotes"
k = .Paragraphs(1).Range.Words(1) - 1
j = k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
With ActiveDocument
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With .Bookmarks("FootNotes").Range
.Style = "Footnote Text"
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
.Bookmarks("FootNotes").Delete
End With
Application.ScreenUpdating = True
End Sub

All you should need to do is to select the block of footnotes to be converted. The macro assumes each footnote consists of a single
paragraph. If you have any multi-paragraph footnotes, you can get around that issue by changing their internal paragraph markers to
line feeds (i.e. Shift-Enter) before running the macro.

The macro is coded to show its progress on the status bar.

--
Cheers
macropod
[Microsoft MVP - Word]


Paul B said:
Hi Paul,

If I understand it correctly your document looks something like:

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[1]. The quick brown fox jumps over the
lazy
dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog[2].

[1] The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
[2] The quick brown fox jumps over the lazy dog.

with the 'footnote' indicators in-line with the text and the footnotes are grouped together like endnotes. Is that correct?


Hi Macropod,

Yes, that's almost exactly the case. The one slight difference is
in the spacing:

The quick brown fox jumps over the lazy dog. The quick brown fox
jumps over the lazy dog. [1] The quick brown fox jumps over the
lazy...

Since these documents are pageless, the footnotes have been
deposited at each chapter's end, which can be many pages away. So
the way I see it, the macro would have to search ahead for the
corresponding footnote number (it's highly unlikely there would
be any false hits on the [#] string) in order to locate the
footnote body text.

bb,
p.
 
P

Paul B

Wow, macropod, that is amazing. Here are my findings:

First, you answered my question about how to search for a
variable:
.Text = "[" & i & "]"

Second, I first stepped through the macro, testing it on a
one-footnote chapter. It worked perfectly. Awesome!

Then, in the second chapter of the two-chapter document, which
has maybe 15 footnotes, I selected the whole block and let her
rip. The first footnote, [2], was converted perfectly. But the
macro seemed to choke for a while (said it was converting
footnote 2) and none of the other footnotes were converted.
Moreover, the footnote numbers in the block of footnote text were
stripped of their brackets.

So I undid the changes and tried Chapter Two again. I found that
I needed to start with the first footnote, over in Chapter One,
or else the numbering would error out. I don't think that will be
a problem.

So I started again in Chapter One. Again the macro worked
perfectly, but now I noticed that it had gone ahead and stripped
the brackets off the footnote numbers of the footnote text in
Chapter Two, which I think is self-defeating.

So I think there are two issues I've unearthed. First, the search
for brackets to strip needs to be relegated to the selected
block. Second, somehow the larger block of about 15 footnotes is
choking the macro.

The file I used is 5 pages long, 56K on the hard drive. The first
chapter consists of one page, two words, and a one-sentence
footnote.

Thanks so much for expending effort on this. I think this is a
very classy"teaching" macro, that one can learn a lot from.

bw,
p.

Hi Paul,

Try the following macro:

Sub ReLinkFootNotes()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
.Bookmarks.Add Range:=.Range, Name:="FootNotes"
k = .Paragraphs(1).Range.Words(1) - 1
j = k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
With ActiveDocument
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With .Bookmarks("FootNotes").Range
.Style = "Footnote Text"
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
.Bookmarks("FootNotes").Delete
End With
Application.ScreenUpdating = True
End Sub

All you should need to do is to select the block of footnotes to be converted. The macro assumes each footnote consists of a single
paragraph. If you have any multi-paragraph footnotes, you can get around that issue by changing their internal paragraph markers to
line feeds (i.e. Shift-Enter) before running the macro.

The macro is coded to show its progress on the status bar.
 
P

Pesach Shelnitz

I used a slightly different approach. In this version, all the footnotes in
the file are processed, the numbering of the footnotes will be continuous,
and the actual numbers of the footnotes willl be ignored. It can be modified
to restart the numbering in each chapter, or to follow the actual numbering
of the footnotes in the original text file.

Sub ConvertToWordFootnotes()
Dim i As Integer
Dim j As Integer
Dim foundRange As Range
Dim wildcardsEnabled As Boolean

Application.ScreenUpdating = False
With Selection
' Save status of the Use Wildcards check box.
With .Find
wildcardsEnabled = .MatchWildcards
.ClearFormatting
End With
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
End With
j = 1
Do While .Find.Execute(Findtext:="\[([0-9]{1,})\]", _
MatchWildcards:=True, _
Forward:=True, Wrap:=wdFindStop, _
ReplaceWith:="\1", Replace:=wdReplaceOne) = True
i = Int(Selection.Text)
Set foundRange = ActiveDocument.Range(Start:=Selection.Start, _
End:=Selection.Start)
Selection.Delete
With .Find
.Text = "[" & CStr(i) & "] "
.MatchWholeWord = True
.MatchWildcards = False
.Execute
Selection.Delete
End With
.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
.Cut
foundRange.Select
.Footnotes.Add Range:=Selection.Range, Text:=""
With ActiveDocument.Footnotes(j).Range
.Paste
.Characters.Last.Delete
End With
j = j + 1
Loop
' Clean up the Search and Replace dialog.
.Find.MatchWildcards = wildcardsEnabled
.Find.Text = ""
.Find.Replacement.Text = ""
End With
Application.ScreenUpdating = True
End Sub

--
Hope this helps,
Pesach Shelnitz


Paul B said:
Wow, macropod, that is amazing. Here are my findings:

First, you answered my question about how to search for a
variable:
.Text = "[" & i & "]"

Second, I first stepped through the macro, testing it on a
one-footnote chapter. It worked perfectly. Awesome!

Then, in the second chapter of the two-chapter document, which
has maybe 15 footnotes, I selected the whole block and let her
rip. The first footnote, [2], was converted perfectly. But the
macro seemed to choke for a while (said it was converting
footnote 2) and none of the other footnotes were converted.
Moreover, the footnote numbers in the block of footnote text were
stripped of their brackets.

So I undid the changes and tried Chapter Two again. I found that
I needed to start with the first footnote, over in Chapter One,
or else the numbering would error out. I don't think that will be
a problem.

So I started again in Chapter One. Again the macro worked
perfectly, but now I noticed that it had gone ahead and stripped
the brackets off the footnote numbers of the footnote text in
Chapter Two, which I think is self-defeating.

So I think there are two issues I've unearthed. First, the search
for brackets to strip needs to be relegated to the selected
block. Second, somehow the larger block of about 15 footnotes is
choking the macro.

The file I used is 5 pages long, 56K on the hard drive. The first
chapter consists of one page, two words, and a one-sentence
footnote.

Thanks so much for expending effort on this. I think this is a
very classy"teaching" macro, that one can learn a lot from.

bw,
p.

Hi Paul,

Try the following macro:

Sub ReLinkFootNotes()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
.Bookmarks.Add Range:=.Range, Name:="FootNotes"
k = .Paragraphs(1).Range.Words(1) - 1
j = k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
With ActiveDocument
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With .Bookmarks("FootNotes").Range
.Style = "Footnote Text"
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
.Bookmarks("FootNotes").Delete
End With
Application.ScreenUpdating = True
End Sub

All you should need to do is to select the block of footnotes to be converted. The macro assumes each footnote consists of a single
paragraph. If you have any multi-paragraph footnotes, you can get around that issue by changing their internal paragraph markers to
line feeds (i.e. Shift-Enter) before running the macro.

The macro is coded to show its progress on the status bar.
 
M

macropod

Hi Pesach,

I hope the OP is using at least Word 2003, because FootnoteOptions isn't in Word 2000 or earlier.

--
Cheers
macropod
[Microsoft MVP - Word]


Pesach Shelnitz said:
I used a slightly different approach. In this version, all the footnotes in
the file are processed, the numbering of the footnotes will be continuous,
and the actual numbers of the footnotes willl be ignored. It can be modified
to restart the numbering in each chapter, or to follow the actual numbering
of the footnotes in the original text file.

Sub ConvertToWordFootnotes()
Dim i As Integer
Dim j As Integer
Dim foundRange As Range
Dim wildcardsEnabled As Boolean

Application.ScreenUpdating = False
With Selection
' Save status of the Use Wildcards check box.
With .Find
wildcardsEnabled = .MatchWildcards
.ClearFormatting
End With
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
End With
j = 1
Do While .Find.Execute(Findtext:="\[([0-9]{1,})\]", _
MatchWildcards:=True, _
Forward:=True, Wrap:=wdFindStop, _
ReplaceWith:="\1", Replace:=wdReplaceOne) = True
i = Int(Selection.Text)
Set foundRange = ActiveDocument.Range(Start:=Selection.Start, _
End:=Selection.Start)
Selection.Delete
With .Find
.Text = "[" & CStr(i) & "] "
.MatchWholeWord = True
.MatchWildcards = False
.Execute
Selection.Delete
End With
.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
.Cut
foundRange.Select
.Footnotes.Add Range:=Selection.Range, Text:=""
With ActiveDocument.Footnotes(j).Range
.Paste
.Characters.Last.Delete
End With
j = j + 1
Loop
' Clean up the Search and Replace dialog.
.Find.MatchWildcards = wildcardsEnabled
.Find.Text = ""
.Find.Replacement.Text = ""
End With
Application.ScreenUpdating = True
End Sub

--
Hope this helps,
Pesach Shelnitz


Paul B said:
Wow, macropod, that is amazing. Here are my findings:

First, you answered my question about how to search for a
variable:
.Text = "[" & i & "]"

Second, I first stepped through the macro, testing it on a
one-footnote chapter. It worked perfectly. Awesome!

Then, in the second chapter of the two-chapter document, which
has maybe 15 footnotes, I selected the whole block and let her
rip. The first footnote, [2], was converted perfectly. But the
macro seemed to choke for a while (said it was converting
footnote 2) and none of the other footnotes were converted.
Moreover, the footnote numbers in the block of footnote text were
stripped of their brackets.

So I undid the changes and tried Chapter Two again. I found that
I needed to start with the first footnote, over in Chapter One,
or else the numbering would error out. I don't think that will be
a problem.

So I started again in Chapter One. Again the macro worked
perfectly, but now I noticed that it had gone ahead and stripped
the brackets off the footnote numbers of the footnote text in
Chapter Two, which I think is self-defeating.

So I think there are two issues I've unearthed. First, the search
for brackets to strip needs to be relegated to the selected
block. Second, somehow the larger block of about 15 footnotes is
choking the macro.

The file I used is 5 pages long, 56K on the hard drive. The first
chapter consists of one page, two words, and a one-sentence
footnote.

Thanks so much for expending effort on this. I think this is a
very classy"teaching" macro, that one can learn a lot from.

bw,
p.

Hi Paul,

Try the following macro:

Sub ReLinkFootNotes()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
.Bookmarks.Add Range:=.Range, Name:="FootNotes"
k = .Paragraphs(1).Range.Words(1) - 1
j = k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
With ActiveDocument
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With .Bookmarks("FootNotes").Range
.Style = "Footnote Text"
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
.Bookmarks("FootNotes").Delete
End With
Application.ScreenUpdating = True
End Sub

All you should need to do is to select the block of footnotes to be converted. The macro assumes each footnote consists of a
single
paragraph. If you have any multi-paragraph footnotes, you can get around that issue by changing their internal paragraph
markers to
line feeds (i.e. Shift-Enter) before running the macro.

The macro is coded to show its progress on the status bar.
 
M

macropod

Hi Paul,

Try the following:

Sub ReLinkFootNotes()
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim FtRng As Range
Application.ScreenUpdating = False
With ActiveDocument
Set FtRng = Selection.Range
With FtRng
.Style = "Footnote Text"
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
k = .Paragraphs(1).Range.Words(1) - 1
j = k
l = ActiveDocument.Footnotes.Count - k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i + l
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With FtRng
MsgBox .Text
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i + l
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i + l).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
Set FtRng = Nothing
End With
Application.ScreenUpdating = True
End Sub

If your footnote numbering restarts, and you want to preserve that, you'll need to format the relevant footnotes afterwards. I could
have made the code do that automatically, but I thought that might introduce other problems if you did the relinking out of
sequence.

--
Cheers
macropod
[Microsoft MVP - Word]


Paul B said:
Wow, macropod, that is amazing. Here are my findings:

First, you answered my question about how to search for a
variable:
.Text = "[" & i & "]"

Second, I first stepped through the macro, testing it on a
one-footnote chapter. It worked perfectly. Awesome!

Then, in the second chapter of the two-chapter document, which
has maybe 15 footnotes, I selected the whole block and let her
rip. The first footnote, [2], was converted perfectly. But the
macro seemed to choke for a while (said it was converting
footnote 2) and none of the other footnotes were converted.
Moreover, the footnote numbers in the block of footnote text were
stripped of their brackets.

So I undid the changes and tried Chapter Two again. I found that
I needed to start with the first footnote, over in Chapter One,
or else the numbering would error out. I don't think that will be
a problem.

So I started again in Chapter One. Again the macro worked
perfectly, but now I noticed that it had gone ahead and stripped
the brackets off the footnote numbers of the footnote text in
Chapter Two, which I think is self-defeating.

So I think there are two issues I've unearthed. First, the search
for brackets to strip needs to be relegated to the selected
block. Second, somehow the larger block of about 15 footnotes is
choking the macro.

The file I used is 5 pages long, 56K on the hard drive. The first
chapter consists of one page, two words, and a one-sentence
footnote.

Thanks so much for expending effort on this. I think this is a
very classy"teaching" macro, that one can learn a lot from.

bw,
p.

Hi Paul,

Try the following macro:

Sub ReLinkFootNotes()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
.Bookmarks.Add Range:=.Range, Name:="FootNotes"
k = .Paragraphs(1).Range.Words(1) - 1
j = k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
With ActiveDocument
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With .Bookmarks("FootNotes").Range
.Style = "Footnote Text"
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
.Bookmarks("FootNotes").Delete
End With
Application.ScreenUpdating = True
End Sub

All you should need to do is to select the block of footnotes to be converted. The macro assumes each footnote consists of a
single
paragraph. If you have any multi-paragraph footnotes, you can get around that issue by changing their internal paragraph markers
to
line feeds (i.e. Shift-Enter) before running the macro.

The macro is coded to show its progress on the status bar.
 
P

Paul B

Macropod, that's awesome. I went through the same procedure:
stepping through the first chapter, then letting it fly for the
second. This time everything worked perfectly.

I had no incident of the footnote numbering restarting. The files
I'm working on now are through-numbered, but I can see how
chapter by chapter numbering could be a good thing for footnotes
(as opposed to endnotes).

Thanks so much for your efforts here. Cleaning up these files is
going to be a whole lot easier now!

BW,
p.

Hi Paul,

Try the following:

Sub ReLinkFootNotes()
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim FtRng As Range
Application.ScreenUpdating = False
With ActiveDocument
Set FtRng = Selection.Range
With FtRng
.Style = "Footnote Text"
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
k = .Paragraphs(1).Range.Words(1) - 1
j = k
l = ActiveDocument.Footnotes.Count - k
For i = 1 To .Paragraphs.Count
If .Paragraphs(i).Range.Words(1) = j + 1 Then
j = j + 1
End If
Next i
End With
For i = k + 1 To j
StatusBar = "Finding Footnote Location: " & i + l
With .Content.Find
.Text = "[" & i & "]"
.MatchWholeWord = True
.MatchWildcards = False
.Execute
If .Found = True Then
.Parent.Select
With Selection
.Delete
.Footnotes.Add Range:=Selection.Range, Text:=""
End With
End If
End With
Next i
With FtRng
MsgBox .Text
For i = k + 1 To j
StatusBar = "Transferring Footnote: " & i + l
With .Paragraphs(1).Range
.Cut
With ActiveDocument.Footnotes(i + l).Range
.Paste
.Words(1).Delete
.Characters.Last.Delete
End With
End With
Next i
On Error Resume Next
End With
Set FtRng = Nothing
End With
Application.ScreenUpdating = True
End Sub

If your footnote numbering restarts, and you want to preserve that, you'll need to format the relevant footnotes afterwards. I could
have made the code do that automatically, but I thought that might introduce other problems if you did the relinking out of
sequence.
 
P

Paul B

Pesach, thanks, that also is awesome. It worked flawlessly here
right from the top.

One small thing I've noticed in both versions is that the
embedded superscripted footnote number is followed by a dash,
non-superscripted. Nothing to cause that leaps out at me.

Also, would the alteration to number by chapter be difficult to
work up? I suppose it would have to be keyed to a certain heading
level.

Thanks again to both you and macropod for solving a pesky problem
here!

bw,
p.



I used a slightly different approach. In this version, all the footnotes in
the file are processed, the numbering of the footnotes will be continuous,
and the actual numbers of the footnotes willl be ignored. It can be modified
to restart the numbering in each chapter, or to follow the actual numbering
of the footnotes in the original text file.

Sub ConvertToWordFootnotes()
Dim i As Integer
Dim j As Integer
Dim foundRange As Range
Dim wildcardsEnabled As Boolean

Application.ScreenUpdating = False
With Selection
' Save status of the Use Wildcards check box.
With .Find
wildcardsEnabled = .MatchWildcards
.ClearFormatting
End With
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
.StartingNumber = 1
.NumberStyle = wdNoteNumberStyleArabic
End With
j = 1
Do While .Find.Execute(Findtext:="\[([0-9]{1,})\]", _
MatchWildcards:=True, _
Forward:=True, Wrap:=wdFindStop, _
ReplaceWith:="\1", Replace:=wdReplaceOne) = True
i = Int(Selection.Text)
Set foundRange = ActiveDocument.Range(Start:=Selection.Start, _
End:=Selection.Start)
Selection.Delete
With .Find
.Text = "[" & CStr(i) & "] "
.MatchWholeWord = True
.MatchWildcards = False
.Execute
Selection.Delete
End With
.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
.Cut
foundRange.Select
.Footnotes.Add Range:=Selection.Range, Text:=""
With ActiveDocument.Footnotes(j).Range
.Paste
.Characters.Last.Delete
End With
j = j + 1
Loop
' Clean up the Search and Replace dialog.
.Find.MatchWildcards = wildcardsEnabled
.Find.Text = ""
.Find.Replacement.Text = ""
End With
Application.ScreenUpdating = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top