PDA

View Full Version : Why doesn't this simple 'Replace' VBA work?



jjokol
03-14-2012, 10:20 AM
I'm using this code to work down through a document and remove the paragraphs I want to remove. Once it's removed a paragraph mark, it should jump to the next paragraph mark and then, if I execute the code again, remove the paragraph mark, etc.

But it doesn't work like that. It removes a paragraph mark then jumps to the next paragraph and, when I've excuted the code again, ignores the paragraph mark I'm on but removes the one after that! Can't work out why.

]
Sub Macro1()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
End Sub

Talis
03-15-2012, 10:48 AM
Why are you doing this? If you have copied a PDF document to the Clipboard and Pasted into Word you can get a paragraph mark at the end of each line. If this is the scenario you're working in there is a much better - although not perfect - way of dealing with it. If you want instructions for this then say so.

What you've outlined above looks to me as though there is no need for VBA. Why not do a simple Find/Replace and hit Replace rather than Replace All?
Find: ^13 Replace: <space>

BoatwrenchV8
03-15-2012, 04:04 PM
jjokol,

I commented out some of your code and it seems to work for me and suspect as this was macro recorded code and agree with Talis that a
...simple Find/Replace and hit Replace rather than Replace All... would probably work ok. I think your code, with the sections commented out will be equivlent to hitting replace and not replace all. Thoughts?

Option Explicit
Sub Macro1()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
' If .Find.Forward = True Then
' .Collapse Direction:=wdCollapseEnd
' Else
' .Collapse Direction:=wdCollapseStart
' End If
' .Find.Execute

End With

' Selection.Find.Execute
' With Selection
' If .Find.Forward = True Then
' .Collapse Direction:=wdCollapseStart
' Else
' .Collapse Direction:=wdCollapseEnd
' End If
' .Find.Execute Replace:=wdReplaceOne
' If .Find.Forward = True Then
' .Collapse Direction:=wdCollapseEnd
' Else
' .Collapse Direction:=wdCollapseStart
' End If
' .Find.Execute
' End With
End Sub