VBA: How to add a hyperlink to text in a table cell

OZ1977

New Member
Joined
Oct 17, 2023
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I have a bit of VBA code to add text to particular cells in a table generated in Word:

With objTable
.Cell(4, 2).Range.Text = Chr(10) & "TEXT 1"
.Cell(4, 2).Range.Font.Bold = True
.Cell(5, 2).Range.Text = "TEXT 2"
.Cell(5, 2).Range.Font.Bold = True
.Cell(2, 3).Range.Text = "TEXT 3"
.Cell(2, 3).Range.Font.Bold = True
etc.
End With

Does anyone know how I need to adapt this to add hyperlinks, e.g hyperlink X to TEXT 1, hyperlink Y to TEXT 2, etc.?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi, welcome to the forum!

Maybe like this:
VBA Code:
With objTable
.DataBodyRange(4, 2).Hyperlinks.Add Anchor:=.DataBodyRange(4, 2), Address:="https://example.microsoft.com", TextToDisplay:=Chr(10) & "TEXT 1"
.DataBodyRange(4, 2).Font.Bold = True
End With
 
Upvote 0
Hi, welcome to the forum!

Maybe like this:
VBA Code:
With objTable
.DataBodyRange(4, 2).Hyperlinks.Add Anchor:=.DataBodyRange(4, 2), Address:="https://example.microsoft.com", TextToDisplay:=Chr(10) & "TEXT 1"
.DataBodyRange(4, 2).Font.Bold = True
End With
Thanks so much for the tip, Flashbond. Unfortunately, I get the error "Object doesn't support this property or method". Seems the Table object doesn't support this hyperlinking command. Any other idea?
 
Upvote 0
I see.. I have no idea. It is working for me:
1697610422524.png

1697610445327.png

The only problem I am facing is I can not alter the text value if the cell is occupied previously.
 
Upvote 0
Thanks so much for the fast feedback and for testing, Flashbond. But I am doing this in a Table object, generated in a Word document (not Excel). Possibly another solution is needed in a Word objTable? :unsure:
 
Upvote 0
I am not familiar with Word objects. Maybe you should wait for another answer.
 
Upvote 0
Thanks nevertheless, Flahsbond, for trying. Much appreciated!

Below is what I am trying at the moment but what seems not to work (FYI in a table Object of a Word document).
I'm trying to have the text displayed in Cell (2,2) of the Table object contain a hyperlink to a website:

1697614842426.png


Does someone know how to solve this?
 
Upvote 0

Forum statistics

Threads
1,215,181
Messages
6,123,513
Members
449,101
Latest member
mgro123

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