PDA

View Full Version : Automatically adding hyperlinks.



udix
12-28-2016, 02:42 AM
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.

gmayor
12-28-2016, 02:48 AM
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.

udix
12-28-2016, 03:47 AM
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.

gmayor
12-28-2016, 05:40 AM
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

udix
12-28-2016, 06:26 AM
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?

gmayor
12-28-2016, 08:44 AM
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.

gmayor
12-28-2016, 09:21 PM
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

udix
01-02-2017, 02:44 AM
Sorry for the long delay!
Thanks so much Graham!! It works perfectly!