Last active
August 25, 2022 21:49
-
-
Save githubyouser/a89f4eeb07e3c08b421826a034894c2d to your computer and use it in GitHub Desktop.
Convert text (in parentheses) to a footnote [Word VBA]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'https://stackoverflow.com/a/29811973 | |
Sub ConvertParentheses2FootnoteV3() | |
'Add undo function! 'https://docs.microsoft.com/en-us/office/vba/word/Concepts/Working-with-Word/working-with-the-undorecord-object | |
Dim objUndo As UndoRecord | |
Set objUndo = Application.UndoRecord | |
'Begin the custom undo record and provide a name for the record | |
objUndo.StartCustomRecord ("Replace parenth. with footnote") | |
'You must SELECT the text you want converted to a footnote! | |
Set oRange = Selection.Range | |
Set Rng = Selection.FormattedText | |
'https://www.msofficeforums.com/word/39878-how-convert-text-enclosed-inside-parenthesis-word.html | |
With oRange | |
'Strip the (now unnecessary) parentheses from the text | |
Rng.Characters.First.Delete | |
Rng.Characters.Last.Delete | |
'https://stackoverflow.com/a/66020089 | |
Set NewFtNt = .Footnotes.Add(Range:=.Duplicate) | |
NewFtNt.Range.FormattedText = Rng.FormattedText | |
End With | |
oRange.Delete | |
'Select the new footnote reference | |
'https://docs.microsoft.com/en-us/office/vba/api/word.selection.expand | |
With Selection | |
.Expand Unit:=wdCharacter | |
'Remove styling from superscripted footnote number | |
.Font.Italic = False | |
.Font.Bold = False | |
If Selection.Footnotes.Count = 1 Then | |
Set oFN = Selection.Footnotes(1).Range | |
With oFN | |
oFN.Select | |
'https://answers.microsoft.com/en-us/msoffice/forum/all/solved-word-macro-to-clear-all-character-styles-in/41c6b364-30dd-43f9-b392-01c861eaaae0 | |
On Error Resume Next | |
Selection.ClearCharacterStyle | |
On Error GoTo 0 | |
.Style = "Footnote Text" | |
End With | |
End If | |
.Collapse wdCollapseEnd | |
End With | |
'End the custom undo record | |
objUndo.EndCustomRecord | |
End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.msofficeforums.com/word-vba/34270-transfer-text-footnotes.html | |
https://www.msofficeforums.com/word/45142-can-i-replace-reference-numbers-footnotes-which.html | |
https://www.msofficeforums.com/word-vba/44111-preserving-formatting-when-turning-footnotes-into-regular.html | |
https://www.msofficeforums.com/word/39878-how-convert-text-enclosed-inside-parenthesis-word.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment