Consulting

Results 1 to 8 of 8

Thread: Hyperlinks. MS Word. Looping through and modifying each as needed

  1. #1
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    5
    Location

    Hyperlinks. MS Word. Looping through and modifying each as needed

    I have a word doc with 800 hyperlinks. The owner wants ugly long hyperlinks display text to be short.
    I have written this code to loop through the links and display on a userform;
    the hyperlink address in textbox 1
    the hyperlink display text in textbox 4
    I have an input box that asks for the replacement display text.
    I want it to put the input box text into userform textbox 5.
    The big problem is the input box.

    I need to use the textbox like the input box, and I need the code to take the the input and modify the link. I will get to that.
    I just want it to let me put text in the box and assign the value to a variable, and then do the next.

    Thank you very much - I don't know ther proper way to insert code here.

    ///////////////////////////////////////////////////////////////////////////////////////

    Public i As Integer
    Sub aaaaastart_fix()
    Dim doc As Document
    Dim newdisp As String
    Dim link
    Dim r As Range
    Set doc = ActiveDocument
    UserForm1.Show vbModeless
    Set doc = ActiveDocument
    
    
    For i = 1 To doc.Hyperlinks.Count
    Application.ScreenUpdating = True
    
    
    If Left(doc.Hyperlinks(i).Address, 3) = "www" Or Left(doc.Hyperlinks(i).Address, 3) = "htt" Then
    
    
    UserForm1.TextBox3.Value = i
    UserForm1.TextBox1.Value = doc.Hyperlinks(i).Address
    UserForm1.TextBox4.Value = doc.Hyperlinks(i).TextToDisplay
    UserForm1.TextBox5.Value = ""
    UserForm1.TextBox5.SetFocus
    newdisp = InputBox("Enter Link Display text")
    ActiveDocument.ActiveWindow.SmallScroll Down:=1
    UserForm1.TextBox5.Value = newdisp ' *the box is not updated!*
    End If 
    
    
    Next
    Last edited by macropod; 04-17-2017 at 11:48 PM. Reason: Added code tags

  2. #2
    You don't need an input box. That's what the text box is for.

    Add two command buttons to your userform - 1 to quit and the other to change the value.
    Code the userform as follows

    Option Explicit
    
    Private Sub CommandButton1_Click()
        If TextBox5.Text = "" Then
            TextBox5.Text = TextBox4.Text
        End If
        Selection.Hyperlinks.Item(1).TextToDisplay = TextBox5.Text
        Unload Me
    End Sub
    
    
    Private Sub CommandButton2_Click()
        Unload Me
    End Sub
    The main code loop is as follows

    Option Explicit
    
    Sub aaaaastart_fix()
    Dim i As Integer
    Dim doc As Document
    Dim newdisp As String
    Dim link
    Dim r As Range
    Dim oFrm As UserForm1
    
    
        Set doc = ActiveDocument
    
    
        For i = 1 To doc.Hyperlinks.Count
            doc.Hyperlinks(i).Range.Select
            Set oFrm = New UserForm1
            If Left(doc.Hyperlinks(i).Address, 3) = "www" Or Left(doc.Hyperlinks(i).Address, 3) = "htt" Then
                With oFrm
                    .TextBox3.Value = i
                    .TextBox4.Value = doc.Hyperlinks(i).TextToDisplay
                    .TextBox5.Value = ""
                    .TextBox5.SetFocus
                    .Show
                End With
            End If
        Next i
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by bobbyrjw View Post
    I don't know ther proper way to insert code here.
    You do that by inserting a set of code tags into your post, via the # button on the posting menu, then inserting your code between them.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    5
    Location
    Thank you!

  5. #5
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    5
    Location
    Thank you very much. Lots to see in your code.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    Here is another way that uses a single form.

    Add a userform with a label lblIndex, a spinbutton SpinButton1, a label lblAddress, a textbox txtDisplay and a command button cmdSet

    In the form add this code:

    Option Explicit
    Private colLinks As New Collection
    Private Sub UserForm_Activate()
    Dim lngIndex As Long
      For lngIndex = 1 To ActiveDocument.Hyperlinks.Count
        'Add qualified hyperlinks to the collection
        If Left(ActiveDocument.Hyperlinks(lngIndex).Address, 3) = "www" Or _
           Left(ActiveDocument.Hyperlinks(lngIndex).Address, 3) = "htt" Then
          colLinks.Add ActiveDocument.Hyperlinks(lngIndex)
        End If
      Next
      If colLinks.Count > 0 Then
        lblIndex.Caption = 1
        lblIndex_Change
      Else
        MsgBox "There are no qualified links to process."
        Unload Me
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub lblIndex_Change()
      'This is a psuedo event
      colLinks.Item(Val(lblIndex)).Range.Select
      lblAddress = colLinks.Item(Val(lblIndex)).Address
      cmdSet.SetFocus
      With txtDisplay
        .Text = colLinks.Item(Val(lblIndex)).TextToDisplay
        .SelStart = 0
        .SelLength = Len(.Text)
        .SetFocus
      End With
      cmdSet.Caption = "SET\NEXT"
      If colLinks.Count = lblIndex Then cmdSet.Caption = "SET"
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub cmdSet_Click()
      colLinks.Item(Val(lblIndex)).TextToDisplay = txtDisplay
      If cmdSet.Caption = "SET" Then
        Unload
      Else
        lblIndex = 1
        lblIndex_Change
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub SpinButton1_SpinUp()
      If Val(lblIndex) < colLinks.Count Then
        lblIndex = lblIndex + 1
      Else
        lblIndex = 1
      End If
      lblIndex_Change
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub SpinButton1_SpinDown()
      If Val(lblIndex) > 1 Then
        lblIndex = lblIndex - 1
      Else
        lblIndex = colLinks.Count
      End If
      lblIndex_Change
    lbl_Exit:
      Exit Sub
    End Sub
    In a standard module add this code:

    Sub EditWebLinks()
    Dim oFrm As UserForm1
      Set oFrm = New UserForm1
      oFrm.Show vbModeless
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    5
    Location

    Thumbs up It works excellently

    Thank you - how to mark it solved?

    Quote Originally Posted by gmayor View Post
    You don't need an input box. That's what the text box is for.

    Add two command buttons to your userform - 1 to quit and the other to change the value.
    Code the userform as follows

    Option Explicit
    
    Private Sub CommandButton1_Click()
        If TextBox5.Text = "" Then
            TextBox5.Text = TextBox4.Text
        End If
        Selection.Hyperlinks.Item(1).TextToDisplay = TextBox5.Text
        Unload Me
    End Sub
    
    
    Private Sub CommandButton2_Click()
        Unload Me
    End Sub
    The main code loop is as follows

    Option Explicit
    
    Sub aaaaastart_fix()
    Dim i As Integer
    Dim doc As Document
    Dim newdisp As String
    Dim link
    Dim r As Range
    Dim oFrm As UserForm1
    
    
        Set doc = ActiveDocument
    
    
        For i = 1 To doc.Hyperlinks.Count
            doc.Hyperlinks(i).Range.Select
            Set oFrm = New UserForm1
            If Left(doc.Hyperlinks(i).Address, 3) = "www" Or Left(doc.Hyperlinks(i).Address, 3) = "htt" Then
                With oFrm
                    .TextBox3.Value = i
                    .TextBox4.Value = doc.Hyperlinks(i).TextToDisplay
                    .TextBox5.Value = ""
                    .TextBox5.SetFocus
                    .Show
                End With
            End If
        Next i
    End Sub

  8. #8
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    5
    Location

    interesting code.

    Thank you, this is over my head. I am going to print it and figure it out.

    Quote Originally Posted by gmaxey View Post
    Here is another way that uses a single form.

    Add a userform with a label lblIndex, a spinbutton SpinButton1, a label lblAddress, a textbox txtDisplay and a command button cmdSet

    In the form add this code:

    Option Explicit
    Private colLinks As New Collection
    Private Sub UserForm_Activate()
    Dim lngIndex As Long
      For lngIndex = 1 To ActiveDocument.Hyperlinks.Count
        'Add qualified hyperlinks to the collection
        If Left(ActiveDocument.Hyperlinks(lngIndex).Address, 3) = "www" Or _
           Left(ActiveDocument.Hyperlinks(lngIndex).Address, 3) = "htt" Then
          colLinks.Add ActiveDocument.Hyperlinks(lngIndex)
        End If
      Next
      If colLinks.Count > 0 Then
        lblIndex.Caption = 1
        lblIndex_Change
      Else
        MsgBox "There are no qualified links to process."
        Unload Me
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub lblIndex_Change()
      'This is a psuedo event
      colLinks.Item(Val(lblIndex)).Range.Select
      lblAddress = colLinks.Item(Val(lblIndex)).Address
      cmdSet.SetFocus
      With txtDisplay
        .Text = colLinks.Item(Val(lblIndex)).TextToDisplay
        .SelStart = 0
        .SelLength = Len(.Text)
        .SetFocus
      End With
      cmdSet.Caption = "SET\NEXT"
      If colLinks.Count = lblIndex Then cmdSet.Caption = "SET"
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub cmdSet_Click()
      colLinks.Item(Val(lblIndex)).TextToDisplay = txtDisplay
      If cmdSet.Caption = "SET" Then
        Unload
      Else
        lblIndex = 1
        lblIndex_Change
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub SpinButton1_SpinUp()
      If Val(lblIndex) < colLinks.Count Then
        lblIndex = lblIndex + 1
      Else
        lblIndex = 1
      End If
      lblIndex_Change
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub SpinButton1_SpinDown()
      If Val(lblIndex) > 1 Then
        lblIndex = lblIndex - 1
      Else
        lblIndex = colLinks.Count
      End If
      lblIndex_Change
    lbl_Exit:
      Exit Sub
    End Sub
    In a standard module add this code:

    Sub EditWebLinks()
    Dim oFrm As UserForm1
      Set oFrm = New UserForm1
      oFrm.Show vbModeless
    lbl_Exit:
      Exit Sub
    End Sub

Posting Permissions

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