PDA

View Full Version : [SOLVED:] Need to determine if footnote reference exists



johnwangel
09-01-2016, 09:26 AM
Hello,
I'm a newbie to this forum, so this is my first post.

I am trying to write a macro that determines if the first "character" in a footnote (or endnote) is a "footnote reference" character.

The problem I'm trying to fix is that often the Word user deletes that first character, or sometimes it is there, but the "Footnote Reference" character style is not applied. In the first case, I would like to insert a reference, in the second case, I would like to apply the proper character style.

But I am unable to figure out what test I can do to determine if that character is actually a "reference" or if it's just a regular character.

Any ideas what I could use to figure that out?

Here's the code I have:



If {NEED THE TEST HERE TO INDICATE IT IS A REFERENCE} And Selection.Characters(1).Style <> "Endnote Reference" Then Selection.Style = "Endnote Reference"
Else If {NEED TEST HERE TO INDICATE IT IS NOT A REFERENCE} Then
Selection.Collapse wdCollapseStart
Selection.InsertCrossReference ReferenceType:=wdRefTypeEndnote, referencekind:=wdEndnoteNumber, referenceitem:=n
End If


Thanks for any suggestions!
John

gmaxey
09-01-2016, 05:41 PM
Perhaps something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFN As Footnote
Dim oRng As Range
For Each oFN In Footnotes
Set oRng = oFN.Range.Characters.First
On Error GoTo Err_Limit
Do Until AscW(oRng) = 13 Or oRng.Start = ActiveDocument.StoryRanges(wdFootnotesStory).Start
oRng.MoveStart wdCharacter, -1
oRng.MoveEnd wdCharacter, -1
Err_ReEntry:
Loop
If AscW(oRng) = 13 Then
oRng.MoveStart wdCharacter, 1
oRng.MoveEnd wdCharacter, 1
End If
If AscW(oRng) = 2 Then
oRng.Style = "Footnote Reference"
Else
oRng.Collapse wdCollapseStart
oFN.Reference.Copy
oRng.Paste
End If
Next
lbl_Exit:
Exit Sub
Err_Limit:
Resume Err_ReEntry
End Sub

johnwangel
09-02-2016, 06:53 AM
Thanks, Greg. I played around with it a bit more and figured out an easier method:



If Selection.Range = ActiveDocument.Endnotes(n).Reference Then IsNoteRef = True
If IsNoteRef = True And Selection.Characters(1).Style <> "Endnote Reference" Then
Selection.Style = "Endnote Reference"

ElseIf IsNoteRef = False Then
Selection.Collapse wdCollapseStart
Selection.InsertCrossReference ReferenceType:=wdRefTypeEndnote, ReferenceKind:=wdEndnoteNumber, ReferenceItem:=n
Selection.TypeText " "
ActiveDocument.Endnotes(n).Range.Characters(1).Select
Selection.Style = "Endnote Reference"

End If


John

gmaxey
09-02-2016, 07:34 AM
John,

Would you care to elaborate, how is it easier? First it doesn't compile n and IsNoteRef are not declared. What is n? What do you have to select in order for the code to do what you want done?

The code I posted loops through the footnotes collection and appears to apply the references style to the reference and restore the reference if it was deleted. Here is mod to replace the space between the ref and text if required.


Sub ScratchMacgro()
'A basic Word macro coded by Greg Maxey
Dim oFN As Footnote
Dim oRng As Range
For Each oFN In Footnotes
Set oRng = oFN.Range.Characters.First
oRng.Select
On Error GoTo Err_Limit
Do Until AscW(oRng) = 13 Or oRng.Start = ActiveDocument.StoryRanges(wdFootnotesStory).Start
oRng.MoveStart wdCharacter, -1
oRng.MoveEnd wdCharacter, -1
Err_ReEntry:
Loop
Debug.Print AscW(oRng)
If AscW(oRng) = 13 Then
oRng.MoveStart wdCharacter, 1
oRng.MoveEnd wdCharacter, 1
End If
If AscW(oRng) = 2 Then
oRng.Style = "Footnote Reference"
Else
oRng.Collapse wdCollapseStart
oFN.Reference.Copy
oRng.Paste
If Not oFN.Range.Characters.First.Previous = " " Then
oRng.InsertAfter " "
End If
End If
Next
lbl_Exit:
Exit Sub
Err_Limit:
Resume Err_ReEntry
End Sub

gmaxey
09-02-2016, 07:46 AM
As a test I created a document with 11 endnotes then deleted the "11 " from the endnote.
I selected endnote 11's text and ran your macro (changing n to 11).

Yes the 11 was restored in the endnote but it isn't the reference. It is now part of the endnote text as the pictures should illustrate.

johnwangel
09-06-2016, 07:15 AM
Thanks Greg. You're right, my method was placing the note reference inside the note text.

I couldn't get your macro to run correctly, but I like the method of copying and pasting the note reference, which works much better. So I added that into my macro.

(By the way, I was only posting a snippet above, because my macro actually does a lot of other things besides this and I didn't want to confuse the issue. n was previously declared as an integer - used to iterate through the footnotes and also to track the current number; and IsEndnoteRef as Boolean.)

John

gmaxey
09-06-2016, 06:57 PM
John,

Ok. Thanks for your reply.