Consulting

Results 1 to 4 of 4

Thread: VBA Word macro - find and replace from a list

  1. #1

    VBA Word macro - find and replace from a list

    Hi

    I am using a very simple code to find and replace a sentence, but there is a location name in the middle, which can be different (up to 300 versions) - is there a way to access a list using one piece of code, rather than copy and paste the code loads of times and change the variant?
    I have made the following simpler for ease, but normally there would be more text to find - it's the location that is the issue.

    Thanks in advance for any help

    Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "arriving at Paris at ([0-9])([0-9])."
            .Replacement.Text = "will arrive at Paris /1/2."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    
    
    Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "arriving at New York at ([0-9])([0-9])."
            .Replacement.Text = "will arrive at New York /1/2."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "arriving at Paris at ([0-9])([0-9])."
    .Replacement.Text = "will arrive at Paris /1/2."
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

  2. #2
    Seehttps://www.gmayor.com/document_batch_processes.htm and the replace from table list option
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks gmayor, but I'm not sure that's what i'm after.
    The sentence will be the same text, which will need to be altered either side of the destination (Paris etc...). I need to search for the sentence allowing for various destinations and then replace them all with alternative text, but keeping the destination the same as it was originally.

  4. #4
    gmayor - found a way around my issue at your website under using wildcards (wouldn't let me post a link!)

    Many thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •