Log in

View Full Version : [SOLVED:] VBA: How to add a hyperlink to a cell in a table in a Word doc generated from Excel



OZ77
10-18-2023, 12:53 PM
Hi all,


I am new here and new to VBA coding. I am using and modifying an Excel VBA subroutine that generates, from an Excel file, a Word document with tables populated with information. Now I'm looking to add a snipet of code that lets me turn text in selected cells into hyperlinks.


This is what imo relevant parts of the current code look like:



(...)
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
(...)
Set wrdApp = CreateObject("Word.Application")
Set wrdDoc = wrdApp.Documents.Add
wrdDoc.PageSetup.Orientation = wdOrientLandscape
(...)
Set objTable = wrdDoc.Tables(wrdDoc.Tables.Count)
objTable.Style = "Table Grid"
(...)
With objTable
.Cell(2, 2).Range.Text = Chr(10) & "TEXT A"
.Cell(2, 2).Range.Font.Bold = True
.Cell(4, 2).Range.Text = "TEXT B"
.Cell(4, 2).Range.Font.Bold = True
(...)


Now I'd like to encode that e.g. hyperlink X is added to TEXT A in Cell (2,2).


Does someone know which code to use?

Aussiebear
10-18-2023, 02:28 PM
Welcome to VBAX OZ77. Have a read here. https://groups.google.com/g/microsoft.public.word.vba.general/c/3tU7eLfJKTo

OZ77
10-19-2023, 06:14 AM
Thanks so much, Aussiebear. That was very useful!

Aussiebear
10-20-2023, 03:42 AM
If you are happy with the result can you mark this thread as Solved please? Go to Thread tools and select Mark thread as Solved.