PDA

View Full Version : [SOLVED:] trim space at start of Hyperlink text



JPG
03-07-2021, 09:42 AM
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.
28060

gmaxey
03-07-2021, 09:48 AM
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

JPG
03-07-2021, 09:57 AM
Excellent macro

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

Jon

JPG
03-07-2021, 11:32 AM
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.

macropod
03-07-2021, 07:13 PM
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

JPG
03-08-2021, 02:00 AM
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.

JPG
03-08-2021, 02:32 AM
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.

macropod
03-08-2021, 03:17 AM
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.

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.

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.

JPG
03-08-2021, 04:08 AM
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.