Differentiate between "character" and "hyperlinked character" in a string in MSWord(2003)

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12457
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Differentiate between "character" and "hyperlinked character" in a string in MSWord(2003)

Post by ChrisGreaves »

13c and my brain has frozen.

I am parsing strings of characters; some are just plain old characters, while some are characters with a hyperlink.
The attached document holds such a string within a table cell.
Each character represents a "command", and I have to process each such command, hence in my document, there are six commands, two of which are hyperlinked ("B" and "C").
The VBA code works:-

Code: Select all

Sub test()
    Dim rng As Range
    Set rng = ThisDocument.Tables(1).Rows(1).Cells(1).Range
    Dim lng As Long
    For lng = 1 To rng.Characters.Count
        If rng.Characters(lng).Hyperlinks.Count = 0 Then
            Debug.Print "plain character " & rng.Characters(lng)
        Else
            Debug.Print rng.Characters(lng).Hyperlinks(1).TextToDisplay & rng.Characters(lng).Hyperlinks(1).Address
        End If
    Next lng
End Sub
but I wonder if there is a neater/cleaner way of getting the job done than "rng.Characters(lng)"

rng.Characters" was just my first thought.
There is no multi-million dollar contract riding on this; I am aware that my old FORTRAN skills are showing up, yet again.
Thanks
Chris
You do not have the required permissions to view the files attached to this post.
If you are going through hell, keep going!

User avatar
Jay Freedman
Microsoft MVP
Posts: 1150
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Differentiate between "character" and "hyperlinked character" in a string in MSWord(2003)

Post by Jay Freedman »

If all you're interested in is the hyperlinks, not the other characters, this is simpler:

Code: Select all

Sub test2()
    Dim rng As Range
    Dim hl As Hyperlink
    Set rng = ThisDocument.Tables(1).Rows(1).Cells(1).Range
    For Each hl In rng.Hyperlinks
        Debug.Print hl.TextToDisplay & " " & hl.Address
    Next hl
End Sub

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 12457
Joined: 24 Jan 2010, 23:23
Location: paused.undefined.exposed

Re: Differentiate between "character" and "hyperlinked character" in a string in MSWord(2003)

Post by ChrisGreaves »

Jay Freedman wrote:
02 Sep 2021, 18:07
If all you're interested in is the hyperlinks, not the other characters, this is simpler:
Jay, thanks for this, but I have to process each symbol/character one by one, and every symbol counts.
The cell strings are commands in a Turing Machine; I decided to allow inline subroutines, hence the hyperlinks associated with a character.
At least now I can qualify as a professional programmer ("If it is working, then fix it or enhance i't).

The code works fine, but I know that my old-style programming often tends to ignore more elegant, modern techniques.
Thanks again
Chris
If you are going through hell, keep going!