Home > Software engineering >  Use regular expressions to replace the WORD tail Spaces appear the strange phenomenon
Use regular expressions to replace the WORD tail Spaces appear the strange phenomenon

Time:11-16


Consult everybody warrior experts!
Use regular expressions to replace tail segment space, if a paragraph, followed by form is replaced is the text of paragraph will run to the table, in addition to other conditions are normal, mystery, please supervise your teacher for advice,

Sub delete paragraph tail Spaces ()
Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m %
The Set regx=CreateObject (" vbscript. Regexp ")
With regx
The Global=True: IgnoreCase=False: MultiLine=True
. The Pattern="+ (\ r \ s" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return,
For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal
For Each mt In the Execute (i.R ange. Text) 'traverse the matched Each Text
M=mt. FirstIndex: n=mt. Length - 1 'for the starting position of the (starting from zero), the Length of the
Set oRang=ActiveDocument. Range (i.R ange. Start + m, i.R ange. Start + m + n) 'locating to the text of the starting position in the whole article, end position
I.R ange. Text=Replace (i.R ange. The Text, "$1")
Next
Next
End With
End Sub

Before running the document


After running the document


CodePudding user response:

 delete Sub paragraph tail Spaces () 
Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m %, mRange As Range
The Set regx=CreateObject (" vbscript. Regexp ")
With regx
The Global=True: IgnoreCase=False: MultiLine=True
. The Pattern="\ s + $" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return,
For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal
The Set mRange=ActiveDocument. Range (i.R ange. Start, i.R ange. End - 1)
For Each mt. In the Execute (mRange. Text) 'traverse the matched Each Text
M=mt. FirstIndex: n=mt. Length of 'looking for the starting position of the (starting from zero), the Length of the
Set oRang=ActiveDocument. Range (mRange. Start + m, mRange. Start + m + n) 'locating to the text of the starting position in the whole article, end position
ORang. Text=. Replace (oRang. Text, "")
Next
Next
End With
End Sub

CodePudding user response:

I can you say the strange phenomenon of show here.
Don't use regular expressions, but directly to the original Text assigned to the Text, also will have the same question.
I tried for a long time, can only find a basic instead of the regular expression substitution method, you can try:
 delete Sub paragraph tail Spaces () 
Dim objFind As Find
The Set objFind=ActiveDocument. Content. The Find
ObjFind. Execute FindText:="^ w ^ 13", ReplaceWith:="^ 13", Replace:=wdReplaceAll
End Sub

This code is executed after also have a question, is to form a line above can't remove the Spaces, form inside can't remove the Spaces.
Other places probably no problem.

CodePudding user response:

But, why the WORD "interface to find replacement" space won't appear this kind of strange phenomenon,
I am a novice, here put forward this problem, main is to want to the great god help to solve this problem, use VBA regular expression, how can I avoid this strange phenomenon

CodePudding user response:

refer to the heavens and the earth would reply: 3/f
however, why the WORD interface "to find replacement" space won't appear this kind of strange phenomenon,
I am a novice, here put forward this problem, main is to want to the great god help to solve this problem, use VBA regular expression, how to avoid this kind of strange phenomenon

My WORD interface "to find replacement" ^ w ^ 13 "will have the same problem.
^ "w" is equivalent to a regular expression in the "\ s +";
"^ 13" is equivalent to a regular expression in the "\ r";
You direct search/replace Spaces or ^ "w" is, indeed, no problem, but ends with logo "^ 13" not line, only part of the search/replace.
So this should be the problem of Word this software design.

CodePudding user response:

Excuse me, great god, whether on the basis of the above questions from the code, by judging whether the next paragraph to form, to determine whether this piece of tail Spaces deleted?
If the idea works, please god help improve the code,

Thank!!!!!

CodePudding user response:

I.R ange. Tables. Count
Should be not in the form is equal to zero.

CodePudding user response:

Mypara. Range. Information (wdWithInTable)

CodePudding user response:

Can use the Select avoid between paragraphs and form a newline

 
Sub delete paragraph tail Spaces ()
Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m %
The Set regx=CreateObject (" vbscript. Regexp ")
With regx
The Global=True: IgnoreCase=False: MultiLine=True
. The Pattern="+ (\ r \ s" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return,
For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal
For Each mt In the Execute (i.R ange. Text) 'traverse the matched Each Text
M=mt. FirstIndex: n=mt. Length - 1 'for the starting position of the (starting from zero), the Length of the
Set oRang=ActiveDocument. Range (i.R ange. Start + m, i.R ange. Start + m + n) 'locating to the text of the starting position in the whole article, end position
I.R ange. Select
Selection. MoveLeft Unit:=wdCharacter, Count:=1, the Extend:=wdExtend
Selection. Range. The Text=Left (. Replace (i.R ange. The Text, "$1"), Len (. Replace (i.R ange. The Text, "$1")) - 1)
'avoid delete section of the tail line breaks
Next
Next
End With
End Sub

CodePudding user response:

Also can use the following code to avoid between paragraphs and form a newline

 
Sub delete paragraph tail Spaces ()
Dim I As com.lowagie.text.paragraph, mt, oRang As Range, n % m %
The Set regx=CreateObject (" vbscript. Regexp ")
With regx
The Global=True: IgnoreCase=False: MultiLine=True
. The Pattern="+ (\ r \ s" 'delete the blank space at the end of the paragraph, if there is no \ r, will as a space together with a carriage return,
For Each I In ActiveDocument. Paragraphs 'In Each paragraph traversal
For Each mt In the Execute (i.R ange. Text) 'traverse the matched Each Text
M=mt. FirstIndex: n=mt. Length - 1 'for the starting position of the (starting from zero), the Length of the
Set oRang=ActiveDocument. Range (i.R ange. Start + m, i.R ange. Start + m + n) 'locating to the text of the starting position in the whole article, end position
The Set myRange=i.R ange
MyRange. End=myRange. End - 1
MyRange. Text=Left (formativetext, Len (formativetext) - 1)
'avoid delete section of the tail line breaks
Next
Next
End With
End Sub
  •  Tags:  
  • VBA
  • Related