Coming soon! The Kael'Nyrin Scrolls: The Atlas Edict

Captain Walker

AI is a genius at MS Word Macros

automation, code, macro, ms word, software

Estimated reading time at 200 wpm: 4 minutes

Yes – it’s true. I don’t need to know how to write code for Macros, when I have paid geniuses who can do the work. BTW some people don’t even know what a ‘macro’ is and they’ll be thinking of microscopes. If you’re one of those types, you should save your time for Instagram or engagement with your mirror.

Whether or not you agree our Fat Disclaimer applies

Right – so after distracting myself with expected BS, the issue was that if you do Research using AI (like Gemini), it will produced reference lists that refer to superscript text in the body of the research. The reference lists almost invariably contain title of the source and a hyperlink written out after it. Long hyperlinks distort WordPress pages. If you have 30 or so references and you’re lazy like me, you don’t want to be manually converting text to hyperlinks with CTRL K. But if you’re not like me – which is quite likely – then you sit there and spend 30 to 60 minutes doing it. You have freedom of individual choice in a so-called free and democratic society.

So I needed a solution. After some experimentation with Autohotkey and Python scripts I realised that wasn’t going well. So the idea I generated for Claude.ai was to create a Macro. This worked like a lovely dream first time! One shot – one kill – that’s the way I like it. And for those who don’t know this is not the first time I’ve used AI for macros. Jeeez!

I make my notes on this site so I can find them if needed any time. It may help some poor suffering soul out there.

The Macro

Sub ConvertGeminiReferences()
'
' Converts selected Gemini references to hyperlinks
' Removes numbering, extracts URL, makes description text clickable
'
    Dim selectedText As String
    Dim lines() As String
    Dim i As Integer
    Dim description As String
    Dim url As String
    Dim regEx As Object
    Dim matches As Object
    
    ' Check if text is selected
    If Selection.Type = wdSelectionIP Then
        MsgBox "Please select the references text first", vbInformation
        Exit Sub
    End If
    
    ' Get selected text
    selectedText = Selection.Text
    
    ' Split into lines
    lines = Split(selectedText, vbCr)
    
    ' Create regex object
    Set regEx = CreateObject("VBScript.RegExp")
    regEx.Global = True
    
    ' Delete current selection
    Selection.Delete
    
    ' Process each line
    For i = LBound(lines) To UBound(lines)
        If Len(Trim(lines(i))) > 0 Then
            
            ' Add line break if not first line
            If i > LBound(lines) Then
                Selection.TypeParagraph
            End If
            
            ' Remove leading numbering (e.g., "1. ", "2. ")
            regEx.Pattern = "^\s*\d+\.\s*"
            lines(i) = regEx.Replace(lines(i), "")
            
            ' Pattern to extract description and URL
            ' Matches text followed by optional comma/period, then http(s) URL
            regEx.Pattern = "^(.*?)[,.]?\s*(https?://\S+)"
            
            If regEx.Test(lines(i)) Then
                Set matches = regEx.Execute(lines(i))
                
                If matches.Count > 0 Then
                    description = Trim(matches(0).SubMatches(0))
                    url = Trim(matches(0).SubMatches(1))
                    
                    ' Remove trailing punctuation from URL
                    url = Replace(url, ")", "")
                    url = Replace(url, "]", "")
                    url = Replace(url, "¶", "")
                    
                    ' Insert as hyperlink
                    ActiveDocument.Hyperlinks.Add _
                        Anchor:=Selection.Range, _
                        Address:=url, _
                        TextToDisplay:=description
                Else
                    ' No match, insert as plain text
                    Selection.TypeText lines(i)
                End If
            Else
                ' No URL found, insert as plain text
                Selection.TypeText lines(i)
            End If
        End If
    Next i
    
    Set regEx = Nothing
    
End Sub

The Instructions

HOW TO INSTALL THE WORD MACRO
==============================

Method 1: Quick Install (Copy/Paste)
-------------------------------------
1. Open Word
2. Press Alt+F11 (opens VBA Editor)
3. In the Project pane, find "Normal" (your default template)
4. Right-click on "Normal" → Insert → Module
5. Open ConvertGeminiReferences.bas in Notepad
6. Copy ALL the code
7. Paste into the empty module window in VBA Editor
8. Press Alt+Q to return to Word


Method 2: Import File
---------------------
1. Open Word
2. Press Alt+F11
3. File → Import File
4. Select ConvertGeminiReferences.bas
5. Press Alt+Q to return to Word


ASSIGN A KEYBOARD SHORTCUT
---------------------------
1. In Word, go to File → Options → Customise Ribbon
2. Click "Keyboard shortcuts: Customise..." button at bottom
3. In Categories, scroll down and select "Macros"
4. In Macros list, select "ConvertGeminiReferences"
5. Click in "Press new shortcut key" box
6. Press your preferred shortcut (e.g., Ctrl+Shift+L)
7. Click "Assign"
8. Click "Close"


HOW TO USE
----------
1. Select your Gemini references in Word
2. Press your assigned shortcut (e.g., Ctrl+Shift+L)
3. References convert to hyperlinks instantly


ADVANTAGES OVER AUTOHOTKEY
---------------------------
- Runs natively in Word (more reliable)
- No external program needed
- Faster execution
- Better error handling
- Works even if Word loses focus

What’s next

Why are you still here? Nothing is next.