Creating VBA script in MS-Word 2010 to replace multiple line breaks with comma(,)

StackOverflow https://stackoverflow.com/questions/23495749

  •  16-07-2023
  •  | 
  •  

Pregunta

1.I am trying to create a VBA script to check quality of the documents.
2.If there are any spaces or linebreaks after a word all should be replaced by comma(,).i.e:I don't want any spaces to be present before comma(,)
Example:

Before running VBA script
abc[space]
[linebreak]
,

After running VBA script
abc ,

Desired Result which I am expecting
abc,

I have tried below code,but i am not getting any clue to further work on it,The below VBA script detects only one linebreak.
I want to create a VBA script that detects multiple linebreaks and give me the desired result.

    Sub Replace1()
    '
    ' Replace1 Macro
    '
    '
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "^p,"
    .Replacement.Text = ","
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
¿Fue útil?

Solución

Change you find text like so:

.Text = "[ ^13]{1,},"

and set wild cards to true:

.MatchWildcards = True

Also make sure the cursor starts at the beginning of the document. You can do so by adding this before Find begins in your VBA:

Selection.HomeKey Unit:=wdStory

Explanation: [ ^13] matches a space OR a paragraph marker (character ^13). {1,} looks for one or more repetitions and, unlike @, it is greedy.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top