#1
|
|||
|
|||
VBA Search Table for Text/Select Text/Insert Hyperlink
I have a large merged document that contains a multi-column-multi-row table. I have a macro set up to run the merge and save the document, but I would like to add script to search for certain text in the table, select that text and insert a hyperlink to a specific document in a file. I'm VERY inexperienced with VBA and everything I do know has been self-taught. I have tried several ways to get this to work - and it seems like there should be a way.... but I need help.
Additionally, if possible, is there a way to create a macro to insert a document as an endnote? Any help would be greatly appreciated. If more information is needed, please let me know. Last edited by sldrellich; 03-23-2015 at 01:48 PM. Reason: Updated question |
#2
|
||||
|
||||
You could use something like
Code:
Sub AddHyperlink(oDoc As Document, _ strText As String, _ strLink As String) Dim oTable As Table Dim oRng As Range Set oTable = oDoc.Tables(1) Set oRng = oTable.Range With oRng.Find Do While .Execute(FindText:=strText) oDoc.Hyperlinks.Add Anchor:=oRng, _ Address:=strLink, _ SubAddress:="", _ ScreenTip:="", _ TextToDisplay:=oRng.Text, _ Target:="" oRng.Collapse 0 Exit Do Loop End With lbl_Exit: Exit Sub End Sub Code:
AddHyperlink ActiveDocument, "Test A", "http:\\www.gmayor.com\WordPages.htm"
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
I appreciate your reply and I'm very sorry if I sound like an idiot, but I'm a little confused.
Here is specifically what I'd like to do: Search all tables for every instance of the text "App. A." Select that text Add a hyperlink to the text to a document located at C:\\My Documents\File\Appendix A.docx. This will be repeated for every appendix in the folder (from Appendix A to Appendix VV). Again, I truly appreciate your help and I'm sorry to be such a novice. |
#4
|
|||
|
|||
Code:
Sub ForNovive() AddHyperlink ActiveDocument, "App A.", "C:\\My Documents\File\Appendix A.docx" End Sub Sub AddHyperlink(oDoc As Document, _ strText As String, _ strLink As String) Dim oTbl As Table Dim oHL As Hyperlink Dim oRng As Range, oHLRange For Each oTbl In oDoc.Tables Set oRng = oTbl.Range With oRng.Find .Wrap = wdFindStop Do While .Execute(FindText:=strText) Set oHL = oDoc.Hyperlinks.Add(Anchor:=oRng.Duplicate, _ Address:=strLink, SubAddress:="", _ ScreenTip:="", TextToDisplay:=oRng.Duplicate.Text, _ Target:="") oRng.Start = oHL.Range.End Loop End With Next oTbl lbl_Exit: Exit Sub End Sub |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Microsoft Word macro to find text, select all text between brackets, and delete | helal1990 | Word VBA | 4 | 02-05-2015 03:52 PM |
VBA code for Microsoft Word macro — select text and insert footnote | ndnd | Word VBA | 10 | 01-06-2015 01:47 PM |
Macro to search warning text style and replace the text color | rohanrohith | Word VBA | 3 | 11-27-2014 01:08 PM |
Select section of text and change text | newbieX | Word VBA | 3 | 03-28-2014 04:21 PM |
Select Text in Table but Table Gets Selected Too | RBusiness | Word | 1 | 06-07-2011 04:26 PM |