PDA

View Full Version : Convert regular text notes to footnotes



Tim.BA
10-25-2011, 09:39 AM
I've spent some time trying to find out how to convert regular text which just looks like notes to footnotes in Office. In my long text, copied from web, 100+ notes are within text, marked with brackets like this "[1]", eg. "solution can be found on the web [2]" and after text there is NOTES section with notes like this "[1] Here is a note", eg "[2] solution found on vbaexpress web page". I'd like to have regular footnotes instead of this. Notes numbers in text and notes themselves after text should be deleted. Is VBA the only solution? Can someone write a macro for my problem? I found a solution which converts bracketed notes, written directly in text, to endnotes or footnotes, but that's not what I need.

macropod
10-26-2011, 06:03 AM
Hi Tim,

The following macro is based on one I wrote for someone in another forum (IIRC) for processing 'text' footnotes such as your's, turning them into 'proper' Word footnotes whilst also retaining their formatting (eg bold/italic/underline).

The macro assumes:
• The selected range contains the footnotes to be converted, with the first footnote number being, at most, enclosed in square brackets, thus [#], followed by a space. Comments in the code indicate where to make changes to suit other scenarios. If the footnotes are scattered throughout the document, rather than at the end, for example, the code will process the selected footnote text on each page;
• you’ll be using the “Footnote Text” style footnotes. If not, you can simply change it in the line ".Style = "Footnote Text"; and
• 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 shows its progress on the status bar.
Sub ReLinkFootNotes()
Dim i As Integer, j As Integer, k As Integer, l As Integer, FtRng As Range
Application.ScreenUpdating = False
With ActiveDocument
Set FtRng = Selection.Range
With FtRng
.Style = "Footnote Text"
With .Find
.ClearFormatting
.Replacement.ClearFormatting
' Change '[' and ']' on the next line to whatever is appropriate if the selected
' footnotes' numbers are enclosed in characters other than square brackets
.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
' Change '"[" & i & "]"' string on the next line to whatever is appropriate
' if the in-line references are not enclosed in square brackets
.Text = "[" & i & "]"
' Delete/comment out the next line if not applicable
.Font.Superscript = True
.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
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
How the code works:
• Take the selected footnote paragraphs, strip off any enclosing characters from the numbers;
• Apply the footnote style to the selected footnote paragraphs;
• Count the number of bookmarked paragraphs with sequential numbers, starting at the first paragraph’s footnote number;
• Find the corresponding footnote numbers in the document;
• Turn the matched numbers into empty footnotes;
• Cut & Paste the footnote paragraphs into the empty footnotes;
• Delete the first word (footnote number) and last character (duplicate para mark) from each footnote.

Note: With minor changes, the above code can be used with end notes instead.

Tim.BA
10-27-2011, 12:49 AM
Thank you. I've tried that but I get an error: "The Find What text contains a Pattern Match expression which is not valid" Debugger goes to ".Execute Replace:=wdReplaceAll" line.

macropod
10-30-2011, 07:48 PM
Hi Tim,

That suggests your system uses non-English separators. Try changing:
.Text = "\[([0-9]{1,})\]"
to:
.Text = "\[([0-9]{1;})\]"

Tim.BA
10-31-2011, 01:13 AM
Right, list separator is ";". Now that I've changed that line, I have another error, "Type Mismatch". Debugger goes to "k = .Paragraphs(1).Range.Words(1) - 1" line. Brackets are removed, but footnotes are not created.

macropod
10-31-2011, 03:25 AM
That suggests you have empty/non-compliant paragraphs in the selected range. See the last bullet point before the code I posted.

Tim.BA
10-31-2011, 03:52 AM
I was trying to run the code without selection, and that's why I got that error.

But, when I select Notes section of my document, so that the selected range contains the footnotes to be converted, I get another error, "The requested member of the collection does not exist". Debugger goes to "With ActiveDocument.Footnotes(i + l).Range".

To clarify further:
Notes are not followed by a space, as required. As show here, there is comma after the bracket sometimes.

I also have a few sections of Notes, each starting from [1]. But, I may run the macro each time separately, selecting a singe Notes section with the footnotes to be converted.


BEFORE:

Heading 1
In my long text, copied from web, 100+ notes are within text. Notes are marked with brackets like this [1], eg. solution can be found on the web [2] and after text there is "Notes" section I'd like to have regular footnotes instead of this. Notes numbers in text and notes themselves after text should be deleted [3].
Is VBA the only solution? Can someone write a macro for my problem? I found a solution which converts bracketed notes, written directly in text, to endnotes or footnotes, but that's not what I need.

Notes: (written as Default style in document, after text):
[1] Here is a note
[2] solution found on VBA Express page
[3] Notes should be deleted from here.

Heading 2
There is more text [some of it with in-text notes] with some more after-the-text notes [1].

Notes:
[1] Here is another note


REQUIERED (numbers symbolize footnotes):

Heading 1
In my long text, copied from web, 100+ notes are within text. Notes are marked with brackets like this1, eg. solution can be found on the web2 and after text there is NOTES section I'd like to have regular footnotes instead of this. Notes numbers in text and notes themselves after text should be deleted3.
Is VBA the only solution? Can someone write a macro for my problem? I found a solution which converts bracketed notes, written directly in text, to endnotes or footnotes, but that's not what I need.

Notes (just this line left):

Footnotes (written as Footnotes style, page bottom):
1 Here is a note
2 solution found on VBA Express page
3 Notes should be deleted from here.

Heading 2
There is more text [some of it with in-text notes] with some more after-the-text notes4.

Notes (just this line left):

Footnotes (written as Footnotes style, page bottom):
4 Here is another note

macropod
10-31-2011, 03:54 AM
Hi Tim,

Are your fottnote markers superscripted? In the code you'll see the line:
' Delete/comment out the next line if not applicable

Tim.BA
10-31-2011, 05:21 AM
Hi Tim,

Are your fottnote markers superscripted? In the code you'll see the line:
' Delete/comment out the next line if not applicable
Notes were not superscripted, if that was the question.
When I commented this out, everything worked as required. Thank you!

Just one more assumption, bracketed notes numbers at the end of the document must not have spaces before, so "....[1] Here is a note" is not acceptable.

macropod
10-31-2011, 05:30 AM
Notes were not superscripted, if that was the question.Correct - as coded the macro looks for superscripted footnote indicators in the document. That's so it doesn't get confused by other text that might be in square brackets, etc.

Just one more assumption, bracketed notes numbers at the end of the document must not have spaces before, so "....[1] Here is a note" is not acceptable.I Haven't tried it but, AFAIK, it shouldn't make any difference to the macro.

Tim.BA
11-01-2011, 04:24 AM
I would like to add that I'm using also LibreOffice, because I don't have licenses for Word on all computers I use. Now that I need to execute script like this, I can see that LibreOffice doesn't fully support VBA, but rather it has it's own Basic. Let me repeat that I'm very thankful for your help. But, I'd also be grateful if you could adapt your script for LibreOffice Writer. I can see that forum supports other apps, a part from MS Office, probably those that execute VBA.

macropod
11-01-2011, 05:18 AM
Hi Tim,

I am unable to help you with LibreOffice: I have never seen or used it and have no idea of what its Writer app's object model might be.

rbisht.india
03-21-2013, 02:08 AM
Hi Below Code I test regarding same Problem.
what Below Code Should Done...
If Any Text in Document Match With Pattern Line "[" + Number + "]" then Code / Macro will Replace this matched Text And will Be Inserted FOOTENOTE..

Starting Procedure Name Is " LinkFootenote_Test

'-------------Begin : Link FooteNote As Per Pattern Matching ------
Sub LinkFootenote_Test()

Dim pg As Paragraph
Dim strpg As String
Dim OpgNum As Integer
Dim NpgNum As Integer

Dim ftNote As Footnote

On Error GoTo ExceptionHanlde
For Each ftNote In ActiveDocument.Footnotes
ftNote.Delete
Next ftNote

OpgNum = 1
For Each pg In ActiveDocument.Paragraphs
pg.Range.Select
Selection.MoveLeft Unit:=wdCharacter
If OpgNum <> Selection.Information(wdFirstCharacterLineNumber) Then
FindFooteNotePattern
End If
OpgNum = Selection.Information(wdFirstCharacterLineNumber)

Next pg

Exit Sub
ExceptionHanlde:
MsgBox Err.Description, vbInformation, "R.BISHT"
End Sub

Sub FindFooteNotePattern()
'
' Macro1 Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "\[([0-9]{1,})\]"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then
'Selection.Find.Execute

Dim fnum As Integer
Dim strTemp As String
strTemp = Trim(Selection.Text)
fnum = CInt(Mid(strTemp, 2, Len(strTemp) - 2))
Selection.Text = ""
Call LinkFootenote(fnum)

End If

End Sub



Public Sub LinkFootenote(ByVal ftNum As Integer)

Dim temprg As Range

Dim ftStr As String

Set temprg = Selection.Range

On Error GoTo ExceptionHandle


'ftStr = ActiveDocument.Footnotes(ftNum).Range.Text
'ActiveDocument.Footnotes.Item(ftNum).Delete
ftStr = " Testing Testing Tsting... "
With Selection
With .FootnoteOptions
.Location = wdBottomOfPage
.NumberingRule = wdRestartContinuous
If ftNum = 1 Then
.StartingNumber = ftNum
End If
.NumberStyle = wdNoteNumberStyleArabic
End With
.Footnotes.Add Range:=Selection.Range, Reference:=""
End With
Selection.TypeText Text:=ftStr
'if temprg.Text =
Exit Sub
ExceptionHandle:
MsgBox Err.Description, vbInformation, "Santosh Mishra"

End Sub

'-------------END: Link FooteNote As Per Pattern Matching ------

Thanks: Rajesh Bisht : INDIA - NEW DELHI (BrightLeaf)

macropod
03-21-2013, 02:37 AM
Hi Below Code I test regarding same Problem.
what Below Code Should Done...
If Any Text in Document Match With Pattern Line "[" + Number + "]" then Code / Macro will Replace this matched Text And will Be Inserted FOOTENOTE..

Starting Procedure Name Is " LinkFootenote_Test
What 'same problem'?

PS: When posting code, please use the VBA tags. They're on the toolbar.

Also, the code I posted in this thread has been superseded. See:
http://gregmaxey.mvps.org/word_tip_pages/convert_reference_notes_to_dynamic_footnotes.html