Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2025, 12:47 PM
chrisjj chrisjj is offline Word 2003 Auto-generate file links? Windows 7 64bit Word 2003 Auto-generate file links? Office 97-2003
Competent Performer
Word 2003 Auto-generate file links?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default Word 2003 Auto-generate file links?

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** ?
Reply With Quote
  #2  
Old 10-12-2025, 12:33 AM
gmayor's Avatar
gmayor gmayor is offline Word 2003 Auto-generate file links? Windows 10 Word 2003 Auto-generate file links? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
The code assumes that sPath is a genuine path accessible from the document.
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
Reply With Quote
  #3  
Old 10-12-2025, 02:48 AM
chrisjj chrisjj is offline Word 2003 Auto-generate file links? Windows 7 64bit Word 2003 Auto-generate file links? Office 97-2003
Competent Performer
Word 2003 Auto-generate file links?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

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
but cannot find how to set sAddr to the first captured expression e.g. X12345. Any idea?

Attached.
Attached Files
File Type: doc LinkAutoGen test.doc (23.5 KB, 1 views)
Reply With Quote
  #4  
Old 10-12-2025, 07:43 AM
gmayor's Avatar
gmayor gmayor is offline Word 2003 Auto-generate file links? Windows 10 Word 2003 Auto-generate file links? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 10-12-2025, 09:12 AM
chrisjj chrisjj is offline Word 2003 Auto-generate file links? Windows 7 64bit Word 2003 Auto-generate file links? Office 97-2003
Competent Performer
Word 2003 Auto-generate file links?
 
Join Date: Aug 2025
Posts: 120
chrisjj is on a distinguished road
Default

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.
Reply With Quote
Reply



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
Word 2003 Auto-generate file links? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft