Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-23-2015, 01:40 PM
sldrellich sldrellich is offline VBA Search Table for Text/Select Text/Insert Hyperlink Windows 7 64bit VBA Search Table for Text/Select Text/Insert Hyperlink Office 2010 64bit
Novice
VBA Search Table for Text/Select Text/Insert Hyperlink
 
Join Date: Mar 2015
Posts: 2
sldrellich is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-24-2015, 12:57 AM
gmayor's Avatar
gmayor gmayor is offline VBA Search Table for Text/Select Text/Insert Hyperlink Windows 7 64bit VBA Search Table for Text/Select Text/Insert Hyperlink Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
and call that sub from the original code e.g.

Code:
AddHyperlink ActiveDocument, "Test A", "http:\\www.gmayor.com\WordPages.htm"
You can add any of the other parameters that you require.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-24-2015, 12:35 PM
sldrellich sldrellich is offline VBA Search Table for Text/Select Text/Insert Hyperlink Windows 7 64bit VBA Search Table for Text/Select Text/Insert Hyperlink Office 2010 64bit
Novice
VBA Search Table for Text/Select Text/Insert Hyperlink
 
Join Date: Mar 2015
Posts: 2
sldrellich is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 03-24-2015, 01:09 PM
gmaxey gmaxey is offline VBA Search Table for Text/Select Text/Insert Hyperlink Windows 7 32bit VBA Search Table for Text/Select Text/Insert Hyperlink Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


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 Search Table for Text/Select Text/Insert Hyperlink VBA code for Microsoft Word macro — select text and insert footnote ndnd Word VBA 10 01-06-2015 01:47 PM
VBA Search Table for Text/Select Text/Insert Hyperlink Macro to search warning text style and replace the text color rohanrohith Word VBA 3 11-27-2014 01:08 PM
VBA Search Table for Text/Select Text/Insert Hyperlink Select section of text and change text newbieX Word VBA 3 03-28-2014 04:21 PM
VBA Search Table for Text/Select Text/Insert Hyperlink Select Text in Table but Table Gets Selected Too RBusiness Word 1 06-07-2011 04:26 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft