MS Word - Table - Adding hyperlink

Mandark

New Member
Joined
Mar 18, 2014
Messages
43
Hello,

I have a simple table (4x10) in Word and I would like to add hyperlink to cells in a certain column. Can you please tell me why this code doesnt work? It says "command was not successful".
Code:
Sub something()


With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
    .Cell(i, 2).Select
    Selection.Range.Text = "Link"
    Selection.Hyperlinks.Add Anchor:=.Selection, Address:="www.microsoft.com" 
Next i
End With


End Sub

The code changes the text, but stops at the hyperlink part. Thank you for your suggestions.

M.
 
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Good ol' Word VBA...

I think the problem lies in your anchor. Try:
Code:
Sub something()

With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
    .Cell(i, 2).Select
    Selection.Hyperlinks.Add Anchor:=Selection.Range, _
      Address:="[URL="http://www.microsoft.com"]www.microsoft.com[/URL]", TextToDisplay:="Link"
Next i
End With

End Sub
 
Upvote 0
It's generally not a good idea to use Sections when you don't need to:
Code:
Sub Demo()
Dim i As Long
With ActiveDocument.Tables(1)
  For i = 2 To .Rows.Count
    With .Cell(i, 2)
      .Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"
    End With
  Next
End With
End Sub
 
Last edited:
Upvote 0
Didn't you mean "Selections" instead of "Sections". I know they shouldn't be used, but I had no idea how to reference that cell otherwise (my first time working with Word Tables in VBA).

I ended up using the code above yours because I wanted the address to be mix of a string and cell text in previous column (and your "With .Cell(i,2)" prevents that).

What is interesting to me and I don't know why is the use of With. Logically it should be working without it. But when I removed it and had just

Code:
Cell(i, 2).Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"

it didn't worked. The code changed cell 1,1 instead of 2,2. Any ideas why?

Thx.


It's generally not a good idea to use Sections when you don't need to:
Code:
Sub Demo()
Dim i As Long
With ActiveDocument.Tables(1)
  For i = 2 To .Rows.Count
    With .Cell(i, 2)
      .Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"
    End With
  Next
End With
End Sub
 
Last edited:
Upvote 0
The code returns what I want, put puts %0d at the end of it. It should be a carriage return, part of the CRLF sequence that Windows uses for line endings just to be different as usual.

But I don't know how to get rid of it.



Good ol' Word VBA...

I think the problem lies in your anchor. Try:
Code:
Sub something()

With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
    .Cell(i, 2).Select
    Selection.Hyperlinks.Add Anchor:=Selection.Range, _
      Address:="[URL="http://www.microsoft.com"]www.microsoft.com[/URL]", TextToDisplay:="Link"
Next i
End With

End Sub
 
Upvote 0
Didn't you mean "Selections" instead of "Sections".
Yes - typo.
I wanted the address to be mix of a string and cell text in previous column (and your "With .Cell(i,2)" prevents that).
No, it doesn't - the problem is you don't understand what the code does.
What is interesting to me and I don't know why is the use of With. Logically it should be working without it. But when I removed it and had just

Code:
Cell(i, 2).Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"

it didn't worked. The code changed cell 1,1 instead of 2,2. Any ideas why?
It doesn't work when you change the code that way because your assumption is wrong. If you don't want to use
Code:
    With .Cell(i, 2)
      .Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"
    End With
you would need to use:
Code:
    .Cell(i, 2).Range.Hyperlinks.Add Anchor:=.Cell(i, 2).Range, Address:="www.microsoft.com", TextToDisplay:="Link"
or:
Code:
    .Range.Hyperlinks.Add Anchor:=.Cell(i, 2).Range, Address:="www.microsoft.com", TextToDisplay:="Link"
 
Upvote 0
Ok, thanks. And how about this problem?

The code returns what I want, put puts %0d at the end of it. It should be a carriage return, part of the CRLF sequence that Windows uses for line endings just to be different as usual.

But I don't know how to get rid of it.
 
Upvote 0
So where are the urls coming from? Simply adding them as explicit text strings the way the demo code does can't add the extraneous characters.
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,044
Members
453,521
Latest member
Chris_Hed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top