logo
search
VBA & Macro Problems

How to Extract an Article Title from a Hyperlink URL in Word VBA

Chanuka GeekiyanageChanuka Geekiyanage Oct 22, 2026 869 views

Question details

The user wants to extract the article slug or text following the final slash of a hyperlink URL and format it into readable title text using a Word VBA macro.

How to Extract an Article Title from a Hyperlink URL in Word VBA
Product
Word
Device & OS
not provided
Scenario
Automating the extraction of readable article titles from document hyperlinks where URL lengths and formats may vary.
Observed behavior
The user needs a programmatic method to identify the last slash in a URL string, extract the subsequent text, and convert hyphens to spaces to create a clean title.
Before you start

Ensure your document is saved as a Macro-Enabled Document (.docm) and that the Developer tab is enabled in your ribbon to access the VBA Editor.

Solution 1Recommended

Use InStrRev and Mid Functions to Extract the URL Slug

Utilize VBA's built-in string manipulation functions to locate the final slash in the URL, extract the remaining text, and replace hyphens with spaces.

This is the most efficient method for extracting a title directly from a well-formatted URL slug. By working backwards to find the last slash, this approach correctly handles URLs of any length or folder depth.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications window.

2
Insert a New Module

Click on Insert in the top menu bar, then select Module to create a blank script window.

3
Write the Extraction Logic

Use the InStrRev function to find the position of the last slash. For example: lastSlash = InStrRev(hlink.Address, "/").

4
Extract the Slug

Use the Mid function to pull the text located after that slash: slug = Mid(hlink.Address, lastSlash + 1).

5
Format the Title

Convert the slug into readable text by replacing hyphens with spaces: title = Replace(slug, "-", " ").

Use InStrRev and Mid Functions to Extract the URL Slug
Handling URL Parameters: If your URLs frequently contain query strings (e.g., '?id=123') or fragments ('#section'), add an InStr check to identify these characters and trim them off before running the Replace function.

Automate Your Documents with WPS Office VBA

WPS Office provides robust support for VBA macros, allowing you to execute string manipulation scripts and batch-process hyperlinks just as easily as in Microsoft Word.

  1. 1. Download WPS Office: Visit the official WPS website, download, and install WPS Office for free.
  2. 2. Open Your Document: Launch WPS Writer and open the document containing the hyperlinks you wish to process.
  3. 3. Access the Developer Tools: Navigate to the Tools tab on the ribbon and click Developer to open the VBA Editor.
  4. 4. Run Your Macro: Paste your URL extraction script into a module and click Run to automatically convert your hyperlinks into readable titles.
High compatibility with Microsoft Word VBA macro syntaxLightweight architecture for fast macro executionEasily automate repetitive hyperlink editing tasks
microsoft office alternative - wps office

Frequently Asked Questions

How do I remove file extensions like .html from the extracted VBA title?

You can append an additional Replace function to your VBA code to strip out specific extensions. For example, add title = Replace(title, ".html", "") right after you replace the hyphens with spaces.

Why does hlink.Address return an empty string for some hyperlinks?

Hyperlinks that point to internal document bookmarks or cross-references may not contain a standard external URL, resulting in an empty address string. Always wrap your extraction logic in an If hlink.Address <> "" check to prevent runtime errors.

Can I process all hyperlinks in my Word document at once?

Yes, you can automate this by placing your extraction logic inside a For Each loop. Using For Each hlink In ActiveDocument.Hyperlinks will iterate through every link in the document sequentially.