Help fixing a hyperlinking macro

S

supersub15

Hi all,

I amended the macro below to fit our needs and need help finalizing
it.

1. Currently, it only looks for I00A0097. How can I modify it to
introduce wildcards? I want to look for anything that looks like
I00A**** (where each asterisk represents a number).

2. Although I did specify that it looks for selections with the style
"HotSpot", it is modifying any style.

Any help is appreciated.

Thanks
Carlos


****************************************
Sub AddURLHyperlinks()

AddURLHyperlinks2 "I00A0097", "FileIO/I00A0097.htm"

End Sub

*******************************************

Sub AddURLHyperlinks2(Dlink As String, HLink As String)

Dim j As Integer
Selection.HomeKey Unit:=wdStory
Do Until j = True

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("HotSpot")
With Selection.Find
.Text = Dlink
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End With
Selection.Find.Execute
If Selection.Find.Found = False Then
Exit Sub
End If
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=HLink, SubAddress:="", ScreenTip:=HLink,
TextToDisplay:=""

Loop

End Sub
 
S

supersub15

I've been working on this since I first posted the question, and got
this far:

Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)

If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"

I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.

Any way to capture the actual Find.Text (example: "I00A0097" instead
of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?

Oh, and the style search is still not working.

Thanks for your help.

Carlos
 
G

Graham Mayor

I suspect

Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With

would fit the bill

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

supersub15

Thanks Graham,

The change works great. However, I have one problem.

Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.

I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined, if
at all possible.

Thanks.
Carlos





I suspect

Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
    .HomeKey Unit:=wdStory
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        Do While .Execute(findText:=Dlink, MatchWildcards:=True)
            Set oRng = Selection.Range
                Hlink = "FileIO/" & oRng.Text & ".htm"
                ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
                Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
                TextToDisplay:=""
            oRng.Collapse wdCollapseEnd
        Loop
    End With
End With

would fit the bill

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


I've been working on this since I first posted the question, and got
this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097" instead
of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -

- Show quoted text -
 
G

Graham Mayor

The macro as quoted will look for any number sequence like
I00A####
and replace it with
FileIO/I00A####.htm
which is what you requested
It would be fairly simple to create arrays to hold the values of DLINK and
HLINK. Can you supply some information on how those differences would look
and how many variations we are talking about, so as to determine the best
approach?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Thanks Graham,

The change works great. However, I have one problem.

Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.

I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined, if
at all possible.

Thanks.
Carlos





I suspect

Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With

would fit the bill

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


I've been working on this since I first posted the question, and got
this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097" instead
of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -

- Show quoted text -
 
S

supersub15

Sure.

Dlink = I00A[0-9]{4} Hlink = "FileIO/" & oRng.Text & ".htm"
Dlink = I00D[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I01P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00Y[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"

And the most complicated would be (this is an example):

Dlink = I00D0228_I00A0228_CSPRT1.htm Hlink = "Output/" &
oRng.Text & ".htm"

It has a range of file names, but all have 3 components, separated by
an underscore (like the example above). It probably conflicts with
some of the ones above, so I'm not sure how to separate the 2 (the
ones above don't have underscores, if that helps).

Sorry for the confusion, Graham.

Carlos


The macro as quoted will look for any number sequence like
I00A####
and replace it with
FileIO/I00A####.htm
which is what you requested
It would be fairly simple to create arrays to hold the values of DLINK and
HLINK. Can you supply some information on how those differences would look
and how many variations we are talking about, so as to determine the best
approach?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Thanks Graham,
The change works great. However, I have one problem.
Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.
I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined, if
at all possible.
Thanks.
Carlos

I suspect
Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With
would fit the bill
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
I've been working on this since I first posted the question, and got
this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097" instead
of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
G

Graham Mayor

The following should point the way and works for the examples you have
quoted. As entries 1-4 all have the same path, I have lumped them all
together when defining Hlink.

Dim Dlink(5) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range

Dlink(0) = "I00A[0-9]{4}[ ]"
Dlink(1) = "I00D[0-9]{4}[ ]"
Dlink(2) = "I00P[0-9]{4}[ ]"
Dlink(3) = "I01P[0-9]{4}[ ]"
Dlink(4) = "I00Y[0-9]{4}[ ]"
Dlink(5) = "I00D[0-9]{4}_I00A[0-9]{4}_[A-Z]{5}[0-9].htm"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i), MatchWildcards:=True)
Set oRng = Selection.Range
Select Case i
Case Is = 0 'first item in the list
Hlink = "FileIO/" & oRng.Text & ".htm"
Case Is = 1, 2, 3, 4 'items 1 to 4
Hlink = "DataCards/" & oRng.Text & ".htm"
Case Is = 5 'item 5
Hlink = "Output/" & oRng.Text
Case Else
End Select
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Sure.

Dlink = I00A[0-9]{4} Hlink = "FileIO/" & oRng.Text & ".htm"
Dlink = I00D[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I01P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00Y[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"

And the most complicated would be (this is an example):

Dlink = I00D0228_I00A0228_CSPRT1.htm Hlink = "Output/" &
oRng.Text & ".htm"

It has a range of file names, but all have 3 components, separated by
an underscore (like the example above). It probably conflicts with
some of the ones above, so I'm not sure how to separate the 2 (the
ones above don't have underscores, if that helps).

Sorry for the confusion, Graham.

Carlos


The macro as quoted will look for any number sequence like
I00A####
and replace it with
FileIO/I00A####.htm
which is what you requested
It would be fairly simple to create arrays to hold the values of
DLINK and HLINK. Can you supply some information on how those
differences would look and how many variations we are talking about,
so as to determine the best approach?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Thanks Graham,
The change works great. However, I have one problem.
Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.
I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined, if
at all possible.

On Feb 25, 3:12 am, "Graham Mayor" <[email protected]>
wrote:
I suspect
Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With
would fit the bill
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
I've been working on this since I first posted the question, and
got this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097"
instead of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
S

supersub15

Thanks Graham.

I tried the new macro and it works perfectly.

I have 2 questions:
1. I tried adding the font name to make the search more accurate, but
it didn't work. Is the syntax wrong?
With .Find
.ClearFormatting
.Font.Name = "Body Text"
etc.

2. I found out that there will be more combinations, which I need to
isolate through the words preceding them. Example: "See xxxxxxxx".
I want the macro to look for that string and hyperlink only
"xxxxxxxx".

I tried modifying the code, but it's hyperlinking the entire string
(i.e. "See xxxxxxxx"):

Dim Dlink(2) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range
Dim oRng2 As String

Dlink(0) = "See xxxxxxxx"
Dlink(1) = "See yyyyyyyy"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Font.Size = 10
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i),
MatchWildcards:=True)
Select Case i
Case Is = 0 'first item in the list
Set oRng = Selection.Range
oRng2 = Right(oRng, 8)
Hlink = oRng2 & ".htm"
Case Is = 1
Set oRng = Selection.Range
oRng2 = Right(oRng, 8)
Hlink = oRng2 & ".htm"
Case Else
End Select
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
End With


The following should point the way and works for the examples you have
quoted. As entries 1-4 all have the same path, I have lumped them all
together when defining Hlink.

Dim Dlink(5) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range

Dlink(0) = "I00A[0-9]{4}[ ]"
Dlink(1) = "I00D[0-9]{4}[ ]"
Dlink(2) = "I00P[0-9]{4}[ ]"
Dlink(3) = "I01P[0-9]{4}[ ]"
Dlink(4) = "I00Y[0-9]{4}[ ]"
Dlink(5) = "I00D[0-9]{4}_I00A[0-9]{4}_[A-Z]{5}[0-9].htm"

With Selection
    For i = LBound(Dlink) To UBound(Dlink)
    .HomeKey Unit:=wdStory
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(findText:=Dlink(i), MatchWildcards:=True)
                Set oRng = Selection.Range
                Select Case i
                    Case Is = 0 'first item in the list
                        Hlink = "FileIO/" & oRng.Text & ".htm"
                    Case Is = 1, 2, 3, 4 'items 1 to 4
                        Hlink = "DataCards/" & oRng.Text & ".htm"
                    Case Is = 5 'item 5
                        Hlink = "Output/" & oRng.Text
                    Case Else
                End Select
                ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
                Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
                TextToDisplay:=""
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    Next i
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Dlink = I00A[0-9]{4}  Hlink = "FileIO/" & oRng.Text & ".htm"
Dlink = I00D[0-9]{4}  Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00P[0-9]{4}  Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I01P[0-9]{4}  Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00Y[0-9]{4}  Hlink = "DataCards/" & oRng.Text & ".htm"
And the most complicated would be (this is an example):
Dlink = I00D0228_I00A0228_CSPRT1.htm      Hlink = "Output/" &
oRng.Text & ".htm"
It has a range of file names, but all have 3 components, separated by
an underscore (like the example above). It probably conflicts with
some of the ones above, so I'm not sure how to separate the 2 (the
ones above don't have underscores, if that helps).
Sorry for the confusion, Graham.

The macro as quoted will look for any number sequence like
I00A####
and replace it with
FileIO/I00A####.htm
which is what you requested
It would be fairly simple to create arrays to hold the values of
DLINK and HLINK. Can you supply some information on how those
differences would look and how many variations we are talking about,
so as to determine the best approach?
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
Thanks Graham,
The change works great. However, I have one problem.
Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.
I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined, if
at all possible.
Thanks.
Carlos
On Feb 25, 3:12 am, "Graham Mayor" <[email protected]>
wrote:
I suspect
Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With
would fit the bill
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
I've been working on this since I first posted the question, and
got this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097"
instead of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
G

Graham Mayor

1 I think you will find Body Text is a style and not a font!
2. You are over complicating things. You don't need the case statements if
all the results are treated the same. As for hyperlinking all of the found
text - that's because you are assigning the hyperlink to oRng which is the
whole of the found text. You could move the start of the range and it will
assign the hyperling to the new range.

Dim Dlink(2) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range

Dlink(0) = "See xxxxxxxx"
Dlink(1) = "See yyyyyyyy"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i), _
MatchWildcards:=True, Wrap:=wdFindContinue)
Set oRng = Selection.Range
oRng.Start = oRng.Start + 4
Hlink = oRng & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
Selection.HomeKey wdStory
Loop
End With
Next i
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Thanks Graham.

I tried the new macro and it works perfectly.

I have 2 questions:
1. I tried adding the font name to make the search more accurate, but
it didn't work. Is the syntax wrong?
With .Find
.ClearFormatting
.Font.Name = "Body Text"
etc.

2. I found out that there will be more combinations, which I need to
isolate through the words preceding them. Example: "See xxxxxxxx".
I want the macro to look for that string and hyperlink only
"xxxxxxxx".

I tried modifying the code, but it's hyperlinking the entire string
(i.e. "See xxxxxxxx"):

Dim Dlink(2) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range
Dim oRng2 As String

Dlink(0) = "See xxxxxxxx"
Dlink(1) = "See yyyyyyyy"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Font.Size = 10
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i),
MatchWildcards:=True)
Select Case i
Case Is = 0 'first item in the list
Set oRng = Selection.Range
oRng2 = Right(oRng, 8)
Hlink = oRng2 & ".htm"
Case Is = 1
Set oRng = Selection.Range
oRng2 = Right(oRng, 8)
Hlink = oRng2 & ".htm"
Case Else
End Select
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
End With


The following should point the way and works for the examples you
have quoted. As entries 1-4 all have the same path, I have lumped
them all together when defining Hlink.

Dim Dlink(5) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range

Dlink(0) = "I00A[0-9]{4}[ ]"
Dlink(1) = "I00D[0-9]{4}[ ]"
Dlink(2) = "I00P[0-9]{4}[ ]"
Dlink(3) = "I01P[0-9]{4}[ ]"
Dlink(4) = "I00Y[0-9]{4}[ ]"
Dlink(5) = "I00D[0-9]{4}_I00A[0-9]{4}_[A-Z]{5}[0-9].htm"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i), MatchWildcards:=True)
Set oRng = Selection.Range
Select Case i
Case Is = 0 'first item in the list
Hlink = "FileIO/" & oRng.Text & ".htm"
Case Is = 1, 2, 3, 4 'items 1 to 4
Hlink = "DataCards/" & oRng.Text & ".htm"
Case Is = 5 'item 5
Hlink = "Output/" & oRng.Text
Case Else
End Select
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Dlink = I00A[0-9]{4} Hlink = "FileIO/" & oRng.Text & ".htm"
Dlink = I00D[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I01P[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
Dlink = I00Y[0-9]{4} Hlink = "DataCards/" & oRng.Text & ".htm"
And the most complicated would be (this is an example):
Dlink = I00D0228_I00A0228_CSPRT1.htm Hlink = "Output/" &
oRng.Text & ".htm"
It has a range of file names, but all have 3 components, separated
by an underscore (like the example above). It probably conflicts
with some of the ones above, so I'm not sure how to separate the 2
(the ones above don't have underscores, if that helps).
Sorry for the confusion, Graham.

On Feb 26, 3:42 am, "Graham Mayor" <[email protected]>
wrote:
The macro as quoted will look for any number sequence like
I00A####
and replace it with
FileIO/I00A####.htm
which is what you requested
It would be fairly simple to create arrays to hold the values of
DLINK and HLINK. Can you supply some information on how those
differences would look and how many variations we are talking
about, so as to determine the best approach?
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
Thanks Graham,
The change works great. However, I have one problem.
Right now, I can specify one Dlink and one Hlink. What if I have
multiple combinations? That's why my initial macro had one macro
calling another, and I could just add multiple combinations on
separate lines.
I know that I can repeat the macro as many times as I want with as
many combinations as I want, but I like things to be streamlined,
if at all possible.

On Feb 25, 3:12 am, "Graham Mayor" <[email protected]>
wrote:
I suspect
Dim Dlink As String
Dim Hlink As String
Dim oRng As Range
Dlink = "I00A[0-9]{4}"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink, MatchWildcards:=True)
Set oRng = Selection.Range
Hlink = "FileIO/" & oRng.Text & ".htm"
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
End With
would fit the bill
My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
supersub15 wrote:
I've been working on this since I first posted the question, and
got this far:
Dlink = "I00A^#^#^#^#" (search without wildcards)
OR
Dlink = "I00A[0-9][0-9][0-9][0-9]" (search with wildcards)
If I specify:
Repl = Selection.Find.Text
HLink = "FileIO/" & Repl & ".htm"
I get "^#^#^#^#" or "[0-9][0-9][0-9][0-9]" as Repl.
Any way to capture the actual Find.Text (example: "I00A0097"
instead of "^#^#^#^#" or "[0-9][0-9][0-9][0-9]")?
Oh, and the style search is still not working.
Thanks for your help.
Carlos- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
S

supersub15

The search criteria changed again, so I combined the 2 versions
provided earlier into the one below. 2 problems:

1. Dlink(2) loops indefinitely. How can I break the loop?
2. For Dlink(2), the part after the underscore (_) can take the
following formats: XSOCF, CSPRT1, CSPRT2, CSFED, CSTAXR, CSFREC,
CSQREC, CSRREC, CST4RS, NSPRT1, CSCHEQ, CSDMMY, CSMRKT, CSHOUS,
CST4AP, CST4RF, CST4RS, CST99R, CST99I, XSOCFA, CSPRT1A, XSOCFB,
CSPRT1B,
Any suggestions on how to combine these into one or maybe two Dlink
entries, or do I have to create one for each (or every 2 or so)?

********************************************************8
Dim Dlink(2) As Variant
Dim Hlink As String
Dim i As Long
Dim oRng As Range

Dlink(0) = "See I00A[0-9]{4}"
Dlink(1) = "See the control card example - I[0-9]{2}[!A][0-9]{4}"
Dlink(2) = "[A-Z]{4}[0-9]{4}_[A-Z]{5}"

With Selection
For i = LBound(Dlink) To UBound(Dlink)
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=Dlink(i),
MatchWildcards:=True, Wrap:=wdFindContinue)
Set oRng = Selection.Range
Select Case i
Case Is = 0 'first item in the list
oRng.Start = oRng.Start + 4
Hlink = "FileIO/" & oRng.Text & ".htm"
Case Is = 1 'item 1 in the list
oRng.Start = oRng.Start + 31
Hlink = "DataCards/" & oRng.Text & ".htm"
Case Is = 2 'item 2 in the list
Hlink = "Output/" & oRng.Text & ".htm"
Case Else
End Select
ActiveDocument.Hyperlinks.Add Anchor:=oRng, _
Address:=Hlink, SubAddress:="", ScreenTip:=Hlink, _
TextToDisplay:=""
oRng.Collapse wdCollapseEnd
Loop
End With
Next i
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top