Consulting

Results 1 to 8 of 8

Thread: Automatically adding hyperlinks.

  1. #1
    VBAX Newbie
    Joined
    Dec 2016
    Posts
    4
    Location

    Automatically adding hyperlinks.

    Hi,

    I have a word document that i use to keep track of bugs.
    The bug summaries are in a specific text format: "NNNNN: bug summary".. where NNNNN is the bug number.
    In each bug summary i'm creating a hyperlink to the bug page, the link for each bug has this format:
    "website_address/NNNNN"- tried to get the specific address but i can't post links here.
    The number NNNNN in the summary and in the link are the same.

    I want to create a VBA script that will search for the specific text format: "NNNNN: blah blah" and create a hyperlink to the address: "website_address/NNNNN", under the text NNNNN.

    And I am a beginner in VBA.

    Is it possible?
    Thanks!
    udi.

  2. #2
    I take it that autoformat with the convert web addresses to hyperlink option doesn't work for these addresses? That being the case how would the macro determine where 'blah blah' ends? The creation of the hyperlink is easy enough but determining where required text stops is required.
    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
    VBAX Newbie
    Joined
    Dec 2016
    Posts
    4
    Location
    Hi Graham,
    Thanks for the quick reply!
    I didn't know about the autoforamt option, looked it up and played around with it, useful tool but I don't see how it will help me solve my problem.

    As for the where the text start and where it ends, i'm using bullets to divide the different issues, so maybe this could detrimne where the texts ends?
    Attaching an example doc.
    Attached Files Attached Files

  4. #4
    I take it that the 'website_address' is a common address for all the links, and that you want to retain Blah, Blah etc? That being the case the following should do the trick. You can run it a second time, without affecting links already created. Replace my web site address with the web site_address referred to



    Option Explicit
    Sub AddLinks()
    Dim oPara As Paragraph
    Dim oRng As Range
    Dim oFld As Field
    Dim bFound As Boolean
    Const strWeb As String = "http://www.gmayor.com/" 'Replace address as required
        For Each oPara In ActiveDocument.Range.Paragraphs
            If oPara.Style = "List Paragraph" Then
                Set oRng = oPara.Range
                If InStr(1, oPara.Range, Chr(58)) > 0 Then
                    For Each oFld In oRng.Fields
                        If oFld.Type = wdFieldHyperlink Then
                            bFound = True
                            Exit For
                        End If
                    Next oFld
                    If Not bFound Then
                        oRng.Collapse 1
                        oRng.MoveEndUntil Chr(58)
                        oRng.Hyperlinks.Add Anchor:=oRng, _
                                            Address:=strWeb & oRng.Text, _
                                            TextToDisplay:=oRng.Text
                    End If
                End If
            End If
        Next oPara
    lbl_Exit:
        Exit Sub
    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

  5. #5
    VBAX Newbie
    Joined
    Dec 2016
    Posts
    4
    Location
    Thanks so much Graham! This is almost perfect!
    It works great onetime - all the number turned into the correct hyperlink! but after adding more lines (since the document is live and will keep changing), and running the script again, the script didn't do anything. Any chance of fixing it?

  6. #6
    The macro checks each paragraph to ensure that

    1. The Paragraph style is "List Paragraph" (Paragraphs with all other styles are ignored)
    2. The paragraph is then checked for a colon (after the number NNNNN)
    3. If both above are true then it checks the paragraph for an existing hyperlink in that paragraph. It it finds a link that paragraph is ignored.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    On further reflection, I see a line of my code is missing. The bFound variable should be set to False before each paragraph is checked. Otherwise the first instance sets the value as true and thus all paragraphs are ignored as having already been processed.

    Option Explicit 
    Sub AddLinks() 
        Dim oPara As Paragraph 
        Dim oRng As Range 
        Dim oFld As Field 
        Dim bFound As Boolean 
        Const strWeb As String = "http://www.gmayor.com/" 'Replace address as required
        For Each oPara In ActiveDocument.Range.Paragraphs 
        bFound = False
            If oPara.Style = "List Paragraph" Then 
                Set oRng = oPara.Range 
                If InStr(1, oPara.Range, Chr(58)) > 0 Then 
                    For Each oFld In oRng.Fields 
                        If oFld.Type = wdFieldHyperlink Then 
                            bFound = True 
                            Exit For 
                        End If 
                    Next oFld 
                    If Not bFound Then 
                        oRng.Collapse 1 
                        oRng.MoveEndUntil Chr(58) 
                        oRng.Hyperlinks.Add Anchor:=oRng, _ 
                        Address:=strWeb & oRng.Text, _ 
                        TextToDisplay:=oRng.Text 
                    End If 
                End If 
            End If 
        Next oPara 
    lbl_Exit: 
        Exit Sub 
    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

  8. #8
    VBAX Newbie
    Joined
    Dec 2016
    Posts
    4
    Location
    Sorry for the long delay!
    Thanks so much Graham!! It works perfectly!

Posting Permissions

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