Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2019, 10:30 PM
barrskog barrskog is offline Rookie help with actually writing a macro Windows 10 Rookie help with actually writing a macro Office 2019
Novice
Rookie help with actually writing a macro
 
Join Date: Nov 2019
Posts: 2
barrskog is on a distinguished road
Default Rookie help with actually writing a macro

Hi!

I'm a Word and programming rookie, but eager to learn more. I'm looking to create a simple find and replace macro, and I've been searching the forums (and googling for other tips) and there's of course plenty of solutions out there. However, I can get the macro to work even when copying the text and replace the Words in the macro that I want to replace. So obviously I'm missing out on something.

The Words I want to replace is:
qq,ohb,krp,ljd,§
With:
<olp>,<spn>,<vocalized_noise>,<noise>,(nothing, ie removed)

The macro I've find which I Think should do this is the one below. However, I'm getting red lines and syntax errors when posting it in the editor. I would be very happy if someone could Point out whats the problem and help me get a macro that can be copy pasted. Yes - I'm that rookie.


Sub MultiReplace()
Dim StrOld As String, StrNew As String
Dim RngFind As Range, RngTxt As Range, i As Long
StrOld = &quot;qq,ohb,krp,ljd,§&quot;
StrNew = &quot;<olp>,<spn>,<vocalized_noise>,<noise>,&qu ot;
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrOld, &quot;,&quot)
Set RngFind = RngTxt.Duplicate
With RngFind.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = Split(StrOld, &quot;,&quot(i)
.Replacement.Text = Split(StrNew, &quot;,&quot(i)
.Format = False
.MatchWholeWord = True
.MatchAllWordForms = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next


End Sub
Reply With Quote
  #2  
Old 11-04-2019, 05:05 AM
gmayor's Avatar
gmayor gmayor is offline Rookie help with actually writing a macro Windows 10 Rookie help with actually writing a macro Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Code:
Sub MultiReplace()
Dim StrOld() As Variant, StrNew() As Variant
Dim RngFind As Range, RngTxt As Range, i As Long
    StrOld = Array("qq", "ohb", "krp", "ljd", "§")
    StrNew = Array("<olp>", "<spn>", "<vocalized_noise>", "<noise>", "")
    Set RngTxt = Selection.Range
    For i = 0 To UBound(StrOld)
        Set RngFind = RngTxt.Duplicate
        With RngFind.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = StrOld(i)
            .Replacement.Text = StrNew(i)
            .Format = False
            .MatchWholeWord = True
            .MatchAllWordForms = False
            .MatchWildcards = False
            .Execute Replace:=wdReplaceAll
        End With
    Next
    Set RngFind = Nothing: Set RngTxt = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-04-2019, 09:55 PM
barrskog barrskog is offline Rookie help with actually writing a macro Windows 10 Rookie help with actually writing a macro Office 2019
Novice
Rookie help with actually writing a macro
 
Join Date: Nov 2019
Posts: 2
barrskog is on a distinguished road
Default

Big thanks - worked like a charm!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a Macro which will search for the content of a cell rather than it's value Sears36 Excel Programming 1 05-11-2017 07:55 AM
Need help writing a macro to bring images to the front readheads Word 0 11-23-2013 07:37 AM
Writing a Macro in VBA johnny51981 Project 1 06-22-2011 02:17 AM
automatic start of macro + help on writing helper function vsempoux Word 3 10-09-2009 03:01 AM
Macro Writing cweachter Excel 0 08-09-2008 08:30 PM

Other Forums: Access Forums

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