Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-18-2022, 11:23 PM
JasonLZH JasonLZH is offline VBA to Replace Text in Multiple Text Boxes Windows 10 VBA to Replace Text in Multiple Text Boxes Office 2019
Novice
VBA to Replace Text in Multiple Text Boxes
 
Join Date: May 2022
Posts: 1
JasonLZH is on a distinguished road
Default VBA to Replace Text in Multiple Text Boxes

Hi, guys. I'm new to Word VBA and I would like to ask some questions here.



Let's say my word file has a few words named "[Name1]" and these words are located in multiple text boxes in a word file. Then, I want to change [Name1] to a person's name, and that's all.

I can replace these words if I do it manually, but it failed when I tried to use the following VBA codes:

Code:
Sub Macro2_Backup()

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "[Name1]"
        .Replacement.Text = "Mike"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
End Sub
I tried to find some ideas online but none of them clearly solved my doubts. Therefore, I would like to ask if there is someone who can show me a simple VBA code to replace words across multiple text boxes in a Word File.
Reply With Quote
  #2  
Old 05-19-2022, 06:26 AM
macropod's Avatar
macropod macropod is offline VBA to Replace Text in Multiple Text Boxes Windows 10 VBA to Replace Text in Multiple Text Boxes Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If you configure the Word file (template?) with just a single, bookmarked, instance of [Name1], then cross-reference that bookmark wherever else [Name1] should appear, then all you'd need to do is change that one instance of [Name1] and have Word refresh the cross-references (e.g. via a Print Preview).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-22-2022, 05:01 AM
jpl jpl is offline VBA to Replace Text in Multiple Text Boxes Windows 7 64bit VBA to Replace Text in Multiple Text Boxes Office 2010 32bit
Advanced Beginner
 
Join Date: Jan 2016
Location: France
Posts: 33
jpl is on a distinguished road
Default

Bonjour
Je ne sais pas si ce qui suit répond à votre question.
Dans un document Word, j'ai placé plusieurs boîtes de texte dont le texte contient une ou plusieurs fois le mot [Name1].
Dans toutes les boites de texte, la macro suivante a remplacé toutes les occurences de ce mot par le mot Mike.


(traduction automatique)
Hello
I don't know if the following answers your question.
In a Word document, I placed several text boxes whose text contains one or more times the word [Name1].
In all text boxes, the following macro replaced all occurrences of this word with the word Mike.


Code:
Sub test()
    Dim Sh As Shape
    For Each Sh In ActiveDocument.Shapes
      If Sh.Type = msoTextBox Then
        With Sh.TextFrame.TextRange.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Execute FindText:="[Name1]", _
                   ReplaceWith:="Mike", _
                   Replace:=wdReplaceAll
        End With
      End If
    Next Sh
  End Sub

Last edited by jpl; 05-22-2022 at 11:41 AM. Reason: Correction de la macro
Reply With Quote
  #4  
Old 05-22-2022, 02:24 PM
Charles Kenyon Charles Kenyon is offline VBA to Replace Text in Multiple Text Boxes Windows 10 VBA to Replace Text in Multiple Text Boxes Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,082
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

"Text Box" has multiple, conflicting, meanings in end-user Word jargon.
Without using any macro, you can use mapped Content Controls.
Reply With Quote
  #5  
Old 05-23-2022, 01:06 AM
macropod's Avatar
macropod macropod is offline VBA to Replace Text in Multiple Text Boxes Windows 10 VBA to Replace Text in Multiple Text Boxes Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Now cross-posted at: vba - Replace specific words/characters/symbol in shapes - Stack Overflow
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
replace, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to find text, replace with multiple lines of text noslenwerd Word VBA 3 12-31-2019 11:04 AM
Select all text in multiple text boxes Virgule Word 0 06-07-2017 12:11 PM
VBA to Replace Text in Multiple Text Boxes Word 2013 Text boxes on everything, can't select across multiple boxes 1Kurgan1 Word 2 10-31-2016 01:20 AM
VBA to Replace Text in Multiple Text Boxes Drawing lines between text boxes which have actual text within the text boxes RHODIUM Word 6 10-01-2016 04:43 PM
Link text of multiple text boxes to one witchcraftz PowerPoint 0 08-28-2014 10:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:08 PM.


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