#1
|
|||
|
|||
Word macro for selecting text and putting it in footnotes
Hello everybody,
I am new to this forum and I was wondering if someone here could help me (Google brought me here). I have to convert a huge document (400+ pages based on a Latex-project) into a .docx file. Converting via pdf and Adobe Acrobat works pretty well, except for the footnotes. What I was trying to do was change the footnote into inline text market by two square brackets (looks like this “[[former-footnote text]]”). What I am looking for is a macro that 1. finds the text between the double square brackets 2. crops this text 3. puts it in a footnote 4. removes the now empty brackets 5. repeats this behavior I am not really familiar with Visual Basic. Maybe someone could point me to some snippets that help me. Thank you very much! |
#2
|
||||
|
||||
Maybe something like
Code:
Sub Macro1() Dim oRng As Range Dim strText As String Dim i As Long Set oRng = ActiveDocument.Range i = 1 With oRng.Find Do While .Execute(FindText:="\[\[(*)\]\]", _ MatchWildcards:=True, _ ReplaceWith:="\1") ActiveDocument.Footnotes.Add oRng, CStr(i), oRng.Text i = i + 1 oRng.Collapse 0 Loop End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
This works, thank you so much! (I'm not the one to judge, but: I looks really elegant too.) Just one thing: The text between the brackets isn't deleted. It is transfered to the footnote, but it stays in the main art as well. How can I adapt the code get rid of the text in the main part? Thanks! |
#4
|
|||
|
|||
Based on the clock, the mysterious one may have signed off for the day. It is elegant. I particularly like the lbl_Exit: Exit Sub part.
Code:
Sub Macro1() Dim oRng As Range Dim strText As String Dim lngIndex As Long Set oRng = ActiveDocument.Range lngIndex = 1 With oRng.Find While .Execute(FindText:="\[\[(*)\]\]", _ MatchWildcards:=True, _ ReplaceWith:="\1") ActiveDocument.Footnotes.Add oRng, CStr(lngIndex), oRng.Text lngIndex = lngIndex + 1 oRng.Text = vbNullString oRng.Collapse 0 Wend End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub 'http://gregmaxey.com/word_tip_pages/convert_reference_notes_to_dynamic_footnotes.html A collaborative effort with Macropod. |
#5
|
|||
|
|||
Cool. This is exactly what I was looking for.
Thank you very much! |
#6
|
|||
|
|||
Sorry about resurrecting an old, superficially solved thread.
But: The solution works great, as long as there are no footnotes added or deleted. Deleting a footnote will not change the numbering of the following footnotes. Adding a footnote anywhere in the document will open a new level of footnotes (starting with 1 or whatever one stets the counter to). I will have to do both, deleting and adding footnotes. I would highly appreciate if anyone with a deeper understanding of the macro and the way how footnotes work in Word could help me getting to grips with the macro-generated footnotes. I am using Office 2010 Professional Plus on Windows 10. |
#7
|
|||
|
|||
So I guess gmaxey tried to point me to the problem:
Quote:
When I try to run the macro in the link above it generates 6 (empty) dynamic footnotes (there are nearly 500 footnotes in the document) and exits with run time error 4605. I assume that my lack of knowledge concerning VBA must be annoying to some of you. But I have been spending an awful lot of time working on the document for the past months only to notice now that there are no dynamic footnotes -- which is a requirement of the publisher that I have to submit the manuscript to in 12 days. My bad, of course. Any help to get those footnotes behaving like regularly generated Word footnotes would be most appreciated. |
#8
|
||||
|
||||
My old friend Greg jumped in and took over the thread, but to address your concerns with my original macro try
Code:
Sub Macro1() Dim oRng As Range Dim strText As String Dim i As Long Set oRng = ActiveDocument.Range i = 1 With oRng.Find Do While .Execute(FindText:="\[\[(*)\]\]", _ MatchWildcards:=True) strText = Replace(oRng.Text, "[", "") strText = Replace(strText, "]", "") ActiveDocument.Footnotes.Add oRng, CStr(i), strText oRng.Text = "" i = i + 1 oRng.Collapse 0 Loop End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
Thank you very much Graham Mayor!
The thing is that the double-square-bracket-notes have long been transformed into (static) footnotes and I have been editing the file a lot. But the code will certainly be of great value next time I have to convert from Latex to Word (although currently I don't feel like doing this anytime soon). And it will for sure help a lot of people facing similar problems. Meanwhile Stefan Bloom provided a macro that turns the static footnotes into regular dynamic ones: Quote:
https://www.msofficeforums.com/word/...te-levels.html Thank you very much for your help. I really do appreciate it. |
#10
|
|||
|
|||
Quote:
Is there any chance you can whip up code for a macro that does the following: Takes any string composed of a space, a left curly bracket, text (which may or may not include other curly brackets), a right curly bracket, and a period (such as " {Brennan, 2007, #74498}{George, 2012, #4564}{Maddow, 1977, #3456}." and... 1. Removes the " {Brennan, 2007, #74498}{George, 2012, #4564}{Maddow, 1977, #3456}" (including the first space); 2. Creates a new footnote *after* the period; and 3. Inserts "{Brennan, 2007, #74498}{George, 2012, #4564}{Maddow, 1977, #3456}." as the new footnote's text (with no space preceding the left curly bracket). If this is possible you'd, like, be my hero for millennia! |
#11
|
||||
|
||||
Not tested in the Mac version, but try
Code:
Sub Macro1() Dim oRng As Range Dim strText As String Dim i As Long Set oRng = ActiveDocument.Range i = 1 With oRng.Find Do While .Execute(FindText:=" {", _ MatchWildcards:=False) oRng.MoveEndUntil Chr(46) If oRng.Characters.Last = "}" Then oRng.End = oRng.End + 1 ActiveDocument.Footnotes.Add oRng, CStr(i), oRng.Text oRng.Text = "" i = i + 1 End If oRng.Collapse 0 Loop End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#12
|
|||
|
|||
Oh my! Thank you! This *almost* works perfectly. Unfortunately, the footnote numerals in the main text lack periods before them (since the original periods have been moved into the footnotes at the bottom of the page along with the citation strings).
Inserting periods manually is possible using find/replace, but in order to do this, the footnotes produced with your macro have to be run through this additional macro: https://www.msofficeforums.com/word-...tml#post100159 So: Any chance you could *either* tweak your macro to insert new periods before the footnote numerals in the main text, *or* incorporate that second macro I just linked to into your macro, *or* perhaps....both? I know I'm asking way to much. Thanks for what you've already done! |
#13
|
|||
|
|||
Hello
I have similar task to convert latex to docx. Now I need to move text to footnotes. Footnotes are separated by symbols \footnote{text to inserted to footnote}. I tried that macro, but it showed an error for me. Could somebody help? |
#14
|
|||
|
|||
Before you go down this route, have you tried converting the original tex file with pandoc?
|
#15
|
|||
|
|||
I've already got docx. I'm not familiar with pandoc. Now I just need to move text in brackets to footnotes.
|
Tags |
footnotes, vba |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word 2013 does not search text in footnotes unless I first click in a footnote | amz | Word | 1 | 11-18-2015 01:56 PM |
word 2007 - footnotes and text box | glggluig | Word | 1 | 08-10-2014 04:09 AM |
Putting text in color shapes | dianabanana | Word | 1 | 04-07-2014 08:07 PM |
Putting text to Excel | Leandro | Office | 2 | 03-01-2012 11:51 PM |
Selecting the macro document | lars | Word VBA | 0 | 08-19-2010 06:06 AM |