Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-11-2016, 04:59 AM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 02-11-2016, 05:29 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,622
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

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)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-11-2016, 06:01 AM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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

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!
Reply With Quote
  #4  
Old 02-11-2016, 06:30 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 797
gmaxey will become famous soon enough
Default

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
You may find this interesting:
'http://gregmaxey.com/word_tip_pages/convert_reference_notes_to_dynamic_footnotes.html

A collaborative effort with Macropod.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-11-2016, 06:42 AM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default

Cool. This is exactly what I was looking for.
Thank you very much!
Reply With Quote
  #6  
Old 05-18-2016, 02:35 AM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 05-18-2016, 01:42 PM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default

So I guess gmaxey tried to point me to the problem:

Quote:
Originally Posted by gmaxey View Post

You may find this interesting:
'http://gregmaxey.com/word_tip_pages/convert_reference_notes_to_dynamic_footnotes.html

A collaborative effort with Macropod.
But apparently I was too dense to take the hint.
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.
Reply With Quote
  #8  
Old 05-19-2016, 04:01 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,622
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

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
If that doesn't work for you, post a sample from the document.
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 05-19-2016, 06:43 AM
mdhg mdhg is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2016
Posts: 10
mdhg is on a distinguished road
Default

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:
Originally Posted by Stefan Blom View Post
The following macro should do the job. It is best to try it on a copy of the original document first:

Code:
Sub ConvertCustomFootnotesToAutoFootnotes()
'Macro created by Stefan Blom, MVP, May 2016
Dim ftText As String
Dim r As Range
Dim ftCount As Long
Dim i As Long
ftCount = ActiveDocument.Footnotes.Count
For i = ftCount To 1 Step -1
ftText = ActiveDocument.Footnotes(i).Range.Text
Set r = ActiveDocument.Footnotes(i).Reference.Duplicate
ActiveDocument.Footnotes(i).Delete
ActiveDocument.Footnotes.Add Range:=r, Text:=ftText
Next i
End Sub

http://www.msofficeforums.com/word/3...te-levels.html

Thank you very much for your help. I really do appreciate it.
Reply With Quote
  #10  
Old 08-03-2017, 11:21 AM
jpkell jpkell is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Aug 2017
Posts: 2
jpkell is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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
If that doesn't work for you, post a sample from the document.
Hi gmayor, I'm looking for help creating a macro similar to the one you created above.

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!
Reply With Quote
  #11  
Old 08-03-2017, 08:24 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,622
gmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the roughgmayor is a jewel in the rough
Default

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)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #12  
Old 08-03-2017, 08:42 PM
jpkell jpkell is offline Mac OS X Office 2016 for Mac
Novice
 
Join Date: Aug 2017
Posts: 2
jpkell is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Not tested in the Mac version, but try
[...]
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: http://www.msofficeforums.com/word-v...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!
Reply With Quote
Reply

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


All times are GMT -7. The time now is 01:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft