Consulting

Results 1 to 9 of 9

Thread: trim space at start of Hyperlink text

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    trim space at start of Hyperlink text

    I have inherited a large file with a format issue I can't solve.

    In the attached sample file you will see that the Hyperlink is extended at the start to include a space. I want to trim off the space.
    A macro would be helpful.
    Sample Hyperlink.zip

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim lngHL As Long
      For lngHL = ActiveDocument.Hyperlinks.Count To 1 Step -1
        ActiveDocument.Hyperlinks(lngHL).TextToDisplay = Trim(ActiveDocument.Hyperlinks(lngHL).TextToDisplay)
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Excellent macro

    I was going down a cheat route and this is much safer, thank you Greg.

    Jon

  4. #4
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Hmm I just find trouble sorry about this. Now after the macro I am finding Hyperlinks can get merged. Is there a way to put a space between two "joined" Hyperlinks.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    For example:
    Sub HlnkFix()
    Application.ScreenUpdating = False
    Dim h As Long, Str As String
    With ActiveDocument
      For h = .Hyperlinks.Count To 1 Step -1
        With .Hyperlinks(h)
          Str = .TextToDisplay
          If Left(Str, 1) = " " Then .Range.Characters.First.Previous.InsertAfter " "
          With .Range.Characters.Last.Next
            If Right(Str, 1) = " " Then .InsertBefore " "
            If .Fields.Count > 0 Then .InsertBefore " "
          End With
          If Trim(Str) <> Str Then .TextToDisplay = Trim(Str)
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by macropod; 03-08-2021 at 03:14 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    So, this trims back the Hyperlink blue and leaves a space. So then I can remove the space after ( with another parse.
    But what I needed also was splitting concatenated Hyperlinks made by the previous macro.

    Thanks for this macro though.

    Something really strange. After running your macro once, I tried it again and got an error. I have encountered this with Word macros on another occasion and the only fix was a reboot. See you in a few mins.

  7. #7
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    I am getting Run-time error '4198':
    Sorry about this I don't know what the difference is...


    I added On Error Resume Next and now it does not error.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by JPG View Post
    what I needed also was splitting concatenated Hyperlinks made by the previous macro.
    It would have been better to check the results of Greg's macro before saving your document. See the updated code.
    Quote Originally Posted by JPG View Post
    Something really strange. After running your macro once, I tried it again and got an error. I have encountered this with Word macros on another occasion and the only fix was a reboot.
    That suggests a faulty Office installation. Try repairing it.
    Quote Originally Posted by JPG View Post
    I am getting Run-time error '4198':
    ...
    I added On Error Resume Next and now it does not error.
    It would probably be more helpful to identify the cause of the error so it can be addressed. Without knowing what line of code you were getting that with and what it was actually processing at the time, it's impossible to know how to address it.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Thanks for the update.
    All was ok I did it on a test document.
    Will have to look at repair of Word some other time. It naggs me to upgrade as I am using 2010 version of Word.

    Thanks for your help. I can work with these macros to fix these issue. Let call this solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •