PDA

View Full Version : [SOLVED:] Move Paragraph Back - Delete Paragraph Mark



dj44
09-15-2016, 08:56 AM
Hi folks,

good thursday all :)

Now I'm pretty sure this is meant to be simple but i cant seem to get it to work.

17087

If paragraph contains "alaska" just move it 1 or 2 characters to the left so i can get rid of the paragraph mark.





Sub MoveParagraph()


Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs

If InStr(oPara.Range.Text, "alaska") > 0 Then

'oPara.Range.Move Unit:=wdParagraph, Count:=-2

'oPara.Range.Move Unit:=wdCharacter, Count:=-2

'oPara.Range.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdMove

End If
Next
End Sub


I tried to move the paragraph and the text but no such luck

thank you for any ideas

gmaxey
09-15-2016, 12:28 PM
What are you actually trying to achieve? If you get rid of a paragraph mark you get rid of a the paragraph.

If you are trying pull the specific single word "Alaska" into the proceeding paragraph then I suppose something like this would do:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Range
Dim lngIndex As Long
On Error GoTo lbl_Exit
For lngIndex = 1 To ActiveDocument.Paragraphs.Count - 1
Set oPar = ActiveDocument.Paragraphs(lngIndex + 1)
If InStr(oPar.Range.Text, "Alaska") > 0 Then
Set oRng = ActiveDocument.Paragraphs(lngIndex).Range
oRng.Collapse wdCollapseEnd
oRng.End = oRng.End - 1
oRng.Text = " " & oPar.Range.Text
oPar.Range.Delete
Set oRng = ActiveDocument.Paragraphs(lngIndex + 1).Range
oRng.Delete
End If
Next
lbl_Exit:
Exit Sub
End Sub

dj44
09-15-2016, 01:36 PM
Hi Greg,

I've been banging my head against the keyboard.

I can't understand why the paragraph range would not move up to the previous line.
That's what the documnetation showed me, so i thought it would be simple.


Actually you are correct i am trying to format my documents that have words on the next line, some how a paragraph mark appeard there spontaneously, so i got to work thinking that cant be right, oh dear, well i still havent been able to solve it using my code.

Thank you for helping to solve this peculiar problem of badly pasted text.

I appreciate your help Greg, and the code is more complex, but i'll make some notes on it so next time i may be better able to solve a situation involving paragraphs gone haywire


Have a good evening :)
TThanks again

gmaxey
09-15-2016, 02:05 PM
Select the "Move" method in your code and press F1 for help. Move is intended to move a defined range, not for rearranging text in a document. Here is another way:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Range
For Each oPar In ActiveDocument.Range.Paragraphs
If InStr(oPar.Range, "Alaska") > 0 Then
Set oRng = oPar.Range
oRng.Move wdCharacter, -2
oRng.InsertAfter " " & Left(oPar.Range.Text, Len(oPar.Range.Text) - 1)
oPar.Range.Delete
End If
Next
lbl_Exit:
Exit Sub
End Sub

dj44
09-15-2016, 02:23 PM
Hi Greg,

great tip the F1 - it takes me directly to the page.

Well I have been using the object library to do searches as well - but there is so much to learn.

My bad - i thought all the oPara's were defined ranges in my code, but then i'm terrible with loops as you know :grinhalo:

Thank you for the alternate version as well , overgenerous to a fault.

Just in case i got stuck in the future I can move as many characters or words without getting tangled up again.


enjoy your evening :)