![]() |
|
#1
|
|||
|
|||
|
Is there an in-built way or more likely add-on to auto-generate links from text matching my pattern, to external files? E.g. turn every **(Xnnnn)** into a link to **folder\file Xnnnn.txt** ? |
|
#2
|
||||
|
||||
|
There is no function that will automate this, but you should be able to achieve it with a macro e.g.
Code:
Sub Macro1()
Dim oRng As Range
Dim sFind As String
Dim sAddr As String
Dim sPath As String
sFind = "(Xnnnn)" 'the text to find
sAddr = Mid(sFind, 2, Len(sFind) - 2) 'remove the brackets from the searched text
sPath = "folder\file " & sAddr & ".txt" 'assemble the link path
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(sFind)
oRng.Hyperlinks.Add oRng, sPath, , , sFind
oRng.End = oRng.Hyperlinks(1).Range.End
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
Note that I have not tested it with Word 2003, but I am pretty sure all the code examples worked in that version.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#3
|
|||
|
|||
|
That's great. Thanks very much
I've enhanced to wildcard match: Code:
Sub Macro1()
Dim oRng As Range
Dim sFind As String
Dim sAddr As String
Dim sPath As String
sFind = "\((X[0-9]{5})\)"
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWildcards = True
Do While .Execute(sFind)
'sAddr = oRng.Text
sAddr = "???"
sPath = "folder\file " & sAddr & ".txt" 'assemble the link path
Debug.Print sPath
oRng.Hyperlinks.Add oRng, sPath, , , oRng.Text
oRng.End = oRng.Hyperlinks(1).Range.End
oRng.Collapse 0
Loop
End With
lbl_Exit:
Set oRng = Nothing
Exit Sub
End Sub
Attached. |
|
#4
|
||||
|
||||
|
sAddr is the found text, so change it back to sAddr = oRng.Text
then process that instead of sFind to remove the brackets e.g. Code:
sAddr = oRng.Text sAddr = Mid(sAddr, 2, Len(sAddr) - 2)
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#5
|
|||
|
|||
|
Thank but I want to set sAddr to the first captured expression e.g. X12345, so sAddr is under control of the regular expression pattern, rather than reply upon VBS string processing.
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Auto Generate a MS Word table based on the amount of data | fordmuntsinger | Mail Merge | 0 | 06-28-2023 03:56 PM |
Auto generate X pages from input
|
lipatin | Word VBA | 12 | 06-01-2018 12:13 AM |
| HTML Links do not work in MS Word 2003 | mswny | Word | 20 | 10-11-2014 02:25 PM |
| Auto Generate Invoice Number Word 2013 | Jonah245 | Word VBA | 3 | 09-04-2014 05:24 PM |
| Word 2003 OLE links | ctreharne | Word | 1 | 03-30-2014 04:13 PM |