#1
|
|||
|
|||
VBA code for Microsoft Word macro — select text and insert footnote
Hi everyone,
I am trying to automate a repeated task in MS Word by using a macro, which is as follows: In a Word document, I would like to select a word or a phrase using the mouse, and run a macro. The macro should (1) Insert a footnote at the end of my selected word or phrase, and (2) copy the selected text to the footnote, but italicised, and followed by a colon. Could anyone help me with the VB code for this? As a beginner, I don't really know where to start and am hoping that the community can help me with this. |
#2
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oFN As Footnote Set oFN = Selection.Range.Footnotes.Add(Selection.Range, , Selection.Text & ":") oFN.Range.Font.Italic = True End Sub |
#3
|
|||
|
|||
That's just the thing, Greg -- thanks ever so much. Just a couple of questions: (1) This italicises the whole phrase and the colon at the moment -- how could I ensure that the colon isn't italicised? (2) Is there a way in VB to get the cursor to focus on the footnote after the colon, i.e. at the point of insertion? I don't actually need the latter at the moment, but trying to learn what to do if I did need it!
|
#4
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oFN As Footnote Dim oRng As Word.Range Set oFN = Selection.Range.Footnotes.Add(Selection.Range, , Selection.Text & ": ") Set oRng = oFN.Range oRng.MoveEnd wdCharacter, -1 oRng.Font.Italic = True oRng.Collapse wdCollapseEnd oRng.Move wdCharacter, 2 oRng.Select End Sub |
#5
|
|||
|
|||
Hi, I had to change
oRng.MoveEnd wdCharacter, -2 (rather than -1), so that the colon was not italicised since we add a whitespace after the colon before that, but apart from that, it works perfectly. Thanks! |
#6
|
|||
|
|||
A final question -- is it possible to use string manipulation on the selected text before putting it in the footnote? The reason I ask is this: quite often, I have to select a word or phrase with the punctuation that follows, because I want the footnote reference number to appear immediately after the punctuation. However, I don't want the punctuation to appear in the text when it is copied to the footnote -- I just want that unitalicised colon instead. So, I wondered whether there is a way to check the Selection.Text to see if there is a final punctuation, and if there is, to ignore that and copy the rest. Thanks.
|
#7
|
|||
|
|||
Next time, please try to fully define your requirements up front?
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oFN As Footnote Dim oRng As Word.Range Dim strText As String strText = Selection.Text If Left(strText, 1) Like "[!?,.;:]" Then strText = Left(strText, Len(strText) - 1) End If Set oFN = Selection.Range.Footnotes.Add(Selection.Range, , strText & ": ") Set oRng = oFN.Range oRng.MoveEnd wdCharacter, -2 oRng.Font.Italic = True oRng.Collapse wdCollapseEnd oRng.Move wdCharacter, 2 oRng.Select End Sub |
#8
|
|||
|
|||
Thanks, Greg! I know -- it's just that I was trying to figure out the additional bits for myself, but obviously didn't do very well with that plan! This is perfect, though, and helps me see exactly how it should be done.
|
#9
|
|||
|
|||
Apologies for resurrecting this thread. This is almost the macro I need also, but I need endnotes instead of footnotes, and I need the page number of the initial selection to be put before the selected text in the endnote.
From working with this, it looks like I can just change the footnote references to endnote, but how do I put the page number of the selection in the footnote? Quote:
|
#10
|
|||
|
|||
Try:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oFN As Endnote Dim oRng As Word.Range Dim strText As String strText = Selection.Text Debug.Print Left(strText, 1) If Left(strText, 1) Like "[\!?,.;:]" Then strText = Left(strText, Len(strText) - 1) End If Set oFN = Selection.Range.Endnotes.Add(Selection.Range, , Selection.Information(wdActiveEndPageNumber) & " " & strText & ": ") Set oRng = oFN.Range oRng.MoveEnd wdCharacter, -2 oRng.Font.Italic = True oRng.Collapse wdCollapseEnd oRng.Move wdCharacter, 2 oRng.Select End Sub |
#11
|
|||
|
|||
That does the trick, Greg! Thank you! Repped.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Insert a footnote in PPT 2010 | YooNaa Kim | PowerPoint | 1 | 06-17-2012 12:04 PM |
Macro for microsoft Word | blukava | Word VBA | 7 | 05-27-2012 02:43 PM |
Macro to Insert Text Into Cells Having Multiple Lines | revans611 | Excel Programming | 4 | 10-24-2011 10:15 AM |
Formula to select description when code is entered | Natasha | Excel | 1 | 09-25-2011 12:59 PM |
Microsoft Word 2003/footnote compatibility | bonanzajellybean | Word | 1 | 05-31-2011 03:31 PM |