#1
|
|||
|
|||
Macro to find text only footnote numbers
I convert ebooks into text so I can convert them with text to speech software so I can listen to them. I want to create a macro so I can search for footnote numbers in order. First I want to search for 1 then 2 then 3 so I can delete them. I would need to find a number and if it is the correct number I want to delete it, if not I need to search for the next occurrence of the same number and delete it if it is the correct one. If I delete the number then it should increment the number and search again.
These footnotes are just text numbers, they are not actual Microsoft Word footnotes. They also are not superscript or subscript. |
#2
|
||||
|
||||
If all you want is a means to Find numbers, you can use a wildcard Find for:
[0-9]{1,} You don't need a macro for this; the only thing a macro might add to the process is the ability to not stop at a number that is lower than the last one found. However, given that a document can contain more than one reference to the same footnote, and the numbering therefore won't be sequential, that's not much use.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
A macro would make this process significantly faster.
I am an e-book addict. I have been taking e-books and turning them into clean text and then into audio for about 5-1/2 years now. I've listened to about 2500 nonfiction e-books. I use firebug to copy the innerHTML in the body copy and then strip out all of the HTML tags and I am then left with clean text except for the footnotes and some other problems that I need to clean up.
I have functions that I use to remove things that are in parentheses, square brackets, curly brackets and then I perform a whole series of text manipulations on the text to prepare it for the text to speech software to work properly. Getting rid of the footnote numbers has been a major pain. Currently I go through the book from top to bottom and manually find each footnote number and delete it. Sometimes the numbering starts at one in the first chapter and can go up to 250 or more by the end of the book. Other times, the numbering restarts at 1 for each chapter. I wrote a macro that started at the highest number footnote and searched backwards for each number as a negatively incremented counter. But the problem is when there are dates or other sorts of numbers within the text the deletion process deletes the wrong numbers. I don't mind working my way through the document, looking at each individual footnote number but visually searching for the numbers is somewhat straining. I thought perhaps there might be some way to create a modal window with yes or no buttons and a (reset counter to 1) button so I could hit the yes or no button to confirm the deletion of each number. If I had no it would search forward and find the next occurrence of the counter variable. If I hit yes it would delete the number from the page then increment the variable and search again. If I hit reset it would set the variable to 1 and start searching again. This would really be extremely handy and I truly appreciate any help you might be able to give. Tim Anderson |
#4
|
||||
|
||||
If the e-book material starts off with formatting such as superscripting for footnotes, it would be far easier to use Find/Replace (even in a macro) to delete them at that point - or even convert them to real footnotes (since, presumably, you have the footnote content that goes with them) - than to try to do it after the formatting has been removed. The same applies to processing any other formatted content.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
The footnotes in the original text is in normal text format
The footnotes are regular text without any formatting whatsoever even in the original text.
In my previous note I was assuming that the macro would use find and replace to do the text deletion. I guess all I would really need to see is a window that finds text then deletes the text if I i hit one button or searches again if I don't want to delete it. Sort of like find and replace buttons on the search and replace window. I can write the code for incrementing the counter. |
#6
|
||||
|
||||
You could use a macro like:
Code:
Sub Demo() Dim Rslt, Rng As Range Set Rng = Selection.Range With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[0-9]{1,}" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found If (.Characters.First.Previous <> "/") And (.Characters.Last.Next <> "/") Then .Select Rslt = MsgBox("Replace this instance of:" & vbCr & .Text, vbYesNoCancel) If Rslt = vbCancel Then Exit Sub If Rslt = vbYes Then .Text = vbNullString End If .Collapse wdCollapseEnd .Find.Execute Loop End With Rng.Select End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Is there a quicker way to search?
I created a form with the following code:
It works okay, but the process is too slow because I get too many false matches. Code:
Public FootnoteCounter As Integer Private Sub CurrentFootnote_Change() FootnoteCounter = Me.CurrentFootnote.Value End Sub Public Sub FindFootnote_Click() Selection.Find.ClearFormatting With Selection.Find .Text = FootnoteCounter .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute End Sub Public Sub DeleteFootnote_Click() Selection.Delete Selection.Find.ClearFormatting With Selection.Find .Text = FootnoteCounter .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute End Sub Public Sub IncrementCounter_Click() FootnoteCounter = FootnoteCounter - 1 Me.CurrentFootnote.Value = FootnoteCounter Selection.HomeKey Unit:=wdStory End Sub I'm starting with the highest footnote number and working through backwards because it is quicker. For instance searching for "1" returns 1, 11, 141, 1990, etc. I get less errors if I search in reverse. Can you think of a way to do a search for "1" that would not find 11, 141, 1990, etc? Is there a way I could use a regular expression to do the search that would exclude numerals to the left or right of my target? Thanks for all your help Last edited by macropod; 04-10-2014 at 06:51 PM. Reason: Added code tags & formatting |
#8
|
||||
|
||||
If you only want to find numbers that increment try:
Code:
Sub Demo() Dim Rslt, Rng As Range, i As Long Set Rng = Selection.Range With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[0-9]{1,}" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found If .Text = i + 1 Then .Select Rslt = MsgBox("Replace this instance of:" & vbCr & .Text, vbYesNoCancel) If Rslt = vbCancel Then Exit Sub If Rslt = vbYes Then .Text = vbNullString i = i + 1 End If ElseIf .Text = i Then .Select Rslt = MsgBox("Replace this instance of:" & vbCr & .Text, vbYesNoCancel) If Rslt = vbCancel Then Exit Sub If Rslt = vbYes Then .Text = vbNullString End If .Collapse wdCollapseEnd .Find.Execute Loop End With Rng.Select End Sub I'm not sure what you mean by "Is there a way I could use a regular expression to do the search that would exclude numerals to the left or right of my target?" The code in both this post and my previous one (both of which use wildcards, which are Word's equivalent of regular expressions) only picks up whole numbers anyway - it won't find just the 1 or 11 within 110 or 211 or 123114, for example. PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text and replace with form field containing that text | iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM |
Macro to find text in between two characters and then format selected text? | qcom | Word | 5 | 02-19-2015 11:23 PM |
VBA code for Microsoft Word macro — select text and insert footnote | ndnd | Word VBA | 10 | 01-06-2015 01:47 PM |
Need help on Macro 03- Find text - if text is blank then remove line | simpleonline1234 | Word VBA | 1 | 02-25-2011 02:28 AM |
Find and replace page numbers in body of text | tollanarama | Word | 3 | 02-13-2011 06:00 AM |