# MS Word - Table - Adding hyperlink



## Mandark (Sep 4, 2015)

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".

```
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.


----------



## CalcSux78 (Sep 4, 2015)

Good ol' Word VBA...

I think the problem lies in your anchor.  Try:

```
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
```


----------



## Macropod (Sep 4, 2015)

It's generally not a good idea to use Sections when you don't need to:

```
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
```


----------



## Mandark (Sep 7, 2015)

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 


```
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.




Macropod said:


> It's generally not a good idea to use Sections when you don't need to:
> 
> ```
> Sub Demo()
> ...


----------



## Mandark (Sep 7, 2015)

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.





CalcSux78 said:


> Good ol' Word VBA...
> 
> I think the problem lies in your anchor.  Try:
> 
> ...


----------



## Macropod (Sep 7, 2015)

Mandark said:


> Didn't you mean "Selections" instead of "Sections".


Yes - typo.


Mandark said:


> 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.


Mandark said:


> 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
> 
> 
> ```
> ...


It doesn't work when you change the code that way because your assumption is wrong. If you don't want to use

```
With .Cell(i, 2)
      .Range.Hyperlinks.Add Anchor:=.Range, Address:="www.microsoft.com", TextToDisplay:="Link"
    End With
```
you would need to use:

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

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


----------



## Mandark (Sep 9, 2015)

Ok, thanks. And how about this problem?



Mandark said:


> 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.


----------



## Macropod (Sep 9, 2015)

Puts '%0d at the end of' _what_?


----------



## Mandark (Sep 10, 2015)

At the end of the URL.



Macropod said:


> Puts '%0d at the end of' _what_?


----------



## Macropod (Sep 10, 2015)

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.


----------

