View Single Post
 
Old 12-23-2021, 12:11 AM
Hidayat Hidayat is offline Windows 10 Office 2016
Novice
 
Join Date: Dec 2021
Posts: 2
Hidayat is on a distinguished road
Default

Thank you Graham!!

I was manage to use the code and with some vba recording fit into my requirements.

Another issue came up, is there a way to reference the current selected open file as the current active window so that it runs in the background, visible = false?

Code:
Set wdDoc = Documents.Open(FileName:=strInFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True)
Updated code:
Code:
Sub Find_Replace_FilesInFolder()

Application.ScreenUpdating = False

Dim strInFolder As String, strOutFold As String, strFile As String, wdDoc As Document
strInFolder = GetFolder
If strInFolder = "" Then Exit Sub
strFile = Dir(strInFolder & "\*.doc*", vbNormal)

'Check for documents in the folder - exit if none found
If strFile <> "" Then strOutFold = strInFolder & "\Output\"

'Test for an existing outpfolder & create one if it doesn't already exist
If Dir(strOutFold, vbDirectory) = "" Then MkDir strOutFold
strFile = Dir(strInFolder & "\*.doc*", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strInFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True)
  
  With wdDoc
    With Selection.Find
    ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
    Selection.Find.Replacement.ClearFormatting

    'Old hyperlink
        .Text = _
            "www.yahoo.com"
    'New Hyperlink
        .Replacement.Text = _
            "www.google.com"
        .Forward = True
        End With
        
        Selection.Find.Execute Replace:=wdReplaceAll
        ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
        Selection.WholeStory
        Selection.Fields.Update
        
       'Save and close the document
    .SaveAs FileName:=strOutFold & .Name, AddToRecentFiles:=False
    .Close
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by Hidayat; 12-23-2021 at 02:34 AM.
Reply With Quote