Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 10-08-2025, 03:24 AM
calinanix calinanix is offline Insert hyperlink to text replacement after Find & Replace? Windows 11 Insert hyperlink to text replacement after Find & Replace? Office 2021
Novice
Insert hyperlink to text replacement after Find & Replace?
 
Join Date: Sep 2025
Posts: 7
calinanix is on a distinguished road
Default

Dear Greg, or anyone who sees this thread,


Sorry to bother you again regarding this matter. I tried to construct a macro extending Paul's F&R macro with yours. I wanted to run the insert hyperlink code only if the column for hyperlink input (column H) is not empty. Since I wanted the code to run from Excel, I tried to construct the code using a string list rather than a Listbox object & ADODB connection because I wanted to run the macro from Excel.


However, I seem to always get an error: Run-time error 4198 "Command Failed" whenever I try to run the macro. What have I done wrong with the code?


Code:
Option Explicit
Sub FRandHyperlink()

'Macropod ©2012 from https://www.msofficeforums.com/word-vba/12803-find-replace.html#post34254
'gregmaxey © 2025 from https://www.msofficeforums.com/word-vba/53797-insert-hyperlink-text-replacement-after-find-replace.html

Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
Dim xlApp As Object, xlWkBk As Object, StrWkBkNm As String, StrWkSht As String
Dim iDataRow As Long, xlFList As String, xlRList As String, xlHList As String, i As Long
Dim xlFItem As String, xlRItem As String, xlHItem As String
StrWkBkNm = "C:\Users\calista.hadiwarsito\Documents\Simplifikasi PDR\Macro Testing\Input Template Tester.xlsm"
StrWkSht = "Data Input"
If Dir(StrWkBkNm) = "" Then
  MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
  Exit Sub
End If
On Error Resume Next
'Start Excel
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
  MsgBox "Can't start Excel.", vbExclamation
  Exit Sub
End If
On Error GoTo 0
With xlApp
  'Hide our Excel session
  .Visible = False
  ' The file is available, so open it.
  Set xlWkBk = .Workbooks.Open(StrWkBkNm, False, True)
  If xlWkBk Is Nothing Then
    MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
    .Quit: Set xlApp = Nothing: Exit Sub
  End If
  ' Process the workbook.
  With xlWkBk
    'Ensure the worksheet exists
    If SheetExists(xlWkBk, StrWkSht) = True Then
      With .Worksheets(StrWkSht)
        ' Find the last-used row in column A.
        iDataRow = Sheets("Data Input").Cells(.Rows.Count, "D").End(xlUp).Row ' -4162 = xlUp
        ' Capture the F/R data.
        For i = 1 To iDataRow
          ' Skip over empty fields to preserve the underlying cell contents.
          If Trim(.Range("D" & i)) <> vbNullString And Trim(.Range("E" & i)) = "FR" Then
            xlFList = xlFList & "|" & Trim(.Range("D" & i))
            xlRList = xlRList & "|" & Trim(.Range("H" & i))
            xlHList = xlHList & "|" & Trim(.Range("I" & i))
          End If
        Next
      End With
    Else
      MsgBox "Cannot find the designated worksheet: " & StrWkSht, vbExclamation
    End If
  .Close False
  End With
  .Quit
End With
' Release Excel object memory
Set xlWkBk = Nothing: Set xlApp = Nothing
'Exit if there are no data
If xlFList = "" Then Exit Sub

    Set wdDoc = Documents.Open(ThisWorkbook.Path & "\Template PDR 3_full.docx", AddToRecentFiles:=True, Visible:=True)
    'Process each word from the F/R List
    With wdDoc
    Options.DefaultHighlightColorIndex = wdYellow
    End With
      With wdDoc.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .MatchWholeWord = True
        .MatchCase = True
        .Wrap = wdFindContinue
        For i = 1 To UBound(Split(xlFList, "|"))
          xlFItem = Split(xlFList, "|")(i)
          xlHItem = Split(xlHList, "|")(i)
          xlRItem = Split(xlRList, "|")(i)
          If xlHItem <> "" Then
            .Text = xlFItem
            While .Execute
                wdDoc.Hyperlinks.Add Anchor:=wdDoc.Range, _
                Address:=xlHItem, _
                ScreenTip:="", _
                TextToDisplay:=xlRItem
            Wend
          Else
            .Text = xlFItem
            .Replacement.Text = xlRItem
            .Execute Replace:=wdReplaceAll
          End If
        Next
      End With
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
 

Tags
find and replace, hyperlink



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using find/replace to insert a word while retaining wildcard-found text paulkaye Word 2 11-13-2022 03:17 AM
Insert hyperlink to text replacement after Find &amp; Replace? Find-replace using part of what was found in the replacement text paulkaye Word 3 12-22-2014 02:52 AM
Insert hyperlink to text replacement after Find &amp; Replace? Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text QA_Compliance_Advisor Word VBA 11 09-23-2014 04:40 AM
Hyperlink/Data Insert & replace jclinton Word 1 09-19-2012 07:22 PM
Insert hyperlink to text replacement after Find &amp; Replace? Insert text at the end of a sentence Find/Replace AlmostFriday Word 6 06-17-2012 05:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:33 AM.


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