Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2024, 09:00 AM
jabb jabb is offline Batch editing text in text box of multiple word documents Windows 10 Batch editing text in text box of multiple word documents Office 2019
Novice
Batch editing text in text box of multiple word documents
 
Join Date: Jan 2024
Posts: 2
jabb is on a distinguished road
Default Batch editing text in text box of multiple word documents

I am new on vba and I am trying to edit and replace text in text box of multiple word documents. All the solutions I found only replace text string in normal settings, not in text boxes. How do I go about this?
Reply With Quote
  #2  
Old 01-06-2024, 02:41 AM
gmaxey gmaxey is offline Batch editing text in text box of multiple word documents Windows 10 Batch editing text in text box of multiple word documents Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
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

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-08-2024, 05:39 AM
jabb jabb is offline Batch editing text in text box of multiple word documents Windows 10 Batch editing text in text box of multiple word documents Office 2019
Novice
Batch editing text in text box of multiple word documents
 
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
  #4  
Old 01-08-2024, 02:28 PM
Guessed's Avatar
Guessed Guessed is offline Batch editing text in text box of multiple word documents Windows 10 Batch editing text in text box of multiple word documents Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It is better for you to learn how to help yourself than for someone to do it all for you.

Greg's code shows you HOW to find the text but it does not actually change anything (it just selects it for an instant) because you weren't specific about what you wanted to find and what you wanted to replace it with.

Study some of the other solution examples you referred to in your first post to see how they are coded. You specifically need to look for lines that mention ".Replacement.Text" and "wdReplaceAll". Study the code in those samples and compare them with the code Greg gave you.

If you still can't work it out then by all means come back here for more guidance but you should demonstrate that you made some effort to understand the code first.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch change headers en footers in multiple documents Kipvis Word VBA 1 04-14-2023 07:55 AM
Batch Editing Word documents sakhtar6 Word VBA 6 03-02-2020 02:49 PM
Batch editing text in text box of multiple word documents Finding specific text in IF-fields across multiple Word documents Office_Worker Word VBA 7 10-19-2017 03:50 AM
batch extract all tables in multiple word documents ZaidaBa Word Tables 3 05-08-2017 10:22 PM
Batch editing text in text box of multiple word documents Inputing Text from Excel sheet to multiple Word documents and Visio File Jazz43 Office 1 05-30-2013 02:08 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:35 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