View Single Post
 
Old 01-08-2024, 05:39 AM
jabb jabb is offline Windows 10 Office 2019
Novice
 
Join Date: Jan 2024
Posts: 2
jabb is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
You have to target the right storyrange:


Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.StoryRanges(wdTextFrameStory)
  Do
    With oRng.Find
      .Text = "Test"
      While .Execute
        oRng.Select
      Wend
    End With
    Set oRng = oRng.NextStoryRange
  Loop Until oRng Is Nothing
lbl_Exit:
  Exit Sub
End Sub
I was trying to add codes to replace the text that your code selects as target with .Replecement.Text = “something”. It does not replace it. It might be a very basic problem but I can’t seem to solve it. Also added a folder path search to change every doc in that folder. What I have in my code might have a lot of holes in it.

Code:
 Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strFile As String
  Dim strFolder As String
  Dim objDoc As Document
  '  Initialization
  strFolder = InputBox("Folder Path")
  strFile = Dir(strFolder & "*.docx", vbNormal)
  '  Process each file in the folder.
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=strFolder & strFile)
    Set objDoc = ActiveDocument
    With objDoc
Dim oRng As Range
  Set oRng = ActiveDocument.StoryRanges(wdTextFrameStory)
  Do
    With oRng.Find
      .Text = "Test"
      While .Execute
        oRng.Select
      Wend
    End With
    Set oRng = oRng.NextStoryRange
  Loop Until oRng Is Nothing
lbl_Exit:
  Exit Sub
End With
 
  Wend
   
End Sub
Reply With Quote