PDA

View Full Version : Superscripts in hyperlink textodisplay?



njaim
11-03-2012, 08:09 PM
I'm trying to get a microsoft word macro that goes through a selection of numbers that are superscripts and then turns them into a hyperlink to a bookmark in the document...

So if there is a 45 as a superscripted number, the macro selects it, changes it into a hyperlink with "#ref_c3n" & currentnumber and then the text to display is currentnumber as well. Only problem I'm having is that I don't know how to get the text to display as a superscript after that.

It only does this to the number specified (because it will start over at 1 after that)

Here's what I have now, anyone know how to help?



Sub TestMe()
Dim current

current = 39

abba:
If current < 146 Then
Selection.Find.ClearFormatting
With Selection.Find.Font
.Superscript = True
.Subscript = False
End With
With Selection.Find
.Text = current
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:="#ref_c3n" & current, ScreenTip:="", TextToDisplay:=current
current = current + 1
Else
End
End If
GoTo abba
End Sub

njaim
11-03-2012, 08:25 PM
Err. So I tried to replace the texttodisplay:=current to textodisplay:=selection.range, and this worked but only on half the numbers for some reason?

macropod
11-04-2012, 01:43 AM
Try the following - it also allows for the possibility that there is more than one of the same supersrcipted number.
Sub TestMe()
Application.ScreenUpdating = False
Dim i As Long, wdDoc As Document, Rng As Range
Set wdDoc = ActiveDocument
For i = 39 To 146
With wdDoc.Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Superscript = True
.Text = i
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found = True
.Duplicate.Hyperlinks.Add Anchor:=.Duplicate, Address:="", _
SubAddress:="#ref_c3n" & i, ScreenTip:="", TextToDisplay:=i
Set Rng = .Duplicate.Hyperlinks(1).Range
Rng.Font.Superscript = True
.Start = Rng.End
.Find.Execute
Loop
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub

njaim
11-04-2012, 02:30 AM
Hrm, this works amazing, except I actually need it to only focus on where my cursor is to the end of the chain of numbers. There are other superscripts of the same numbers, but I don't want to link those. And there are actually like 2500 of these, but I'm just testing lower numbers until it works to avoid crashing.

I tried to change this myself, but I'm so confused now...

njaim
11-04-2012, 02:31 AM
Also, thank you for the help. It's HUGELY appreciated. I haven't programmed with VB for 10 years or so, and I've just started getting into VBA as a way to avoid incredibly monotonous and long winded tasks.

macropod
11-04-2012, 04:37 AM
Perhaps this would suit your needs better. Any superscripted numbers in the selected range will be hyperlinked.
Sub TestMe()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
Set Rng = .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Superscript = True
.Text = "[0-9]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If Not .InRange(Rng) Then Exit Do
ActiveDocument.Hyperlinks.Add Anchor:=.Range, Address:="", _
SubAddress:="#ref_c3n" & .Text, ScreenTip:="", TextToDisplay:=.Text
.Range.Font.Superscript = True
.Start = .End
.Find.Execute
Loop
End With
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub