I'm having trouble formulating a macro/VBA to remove only certain manual line breaks in a document. The document has the repeated structure shown below. If I knock out all line breaks, I lose the Styles, so I need to always retain the last break of each Normal Style block. Any help/code appreciated.
I would also like to be able to replace the Normal style of the quote text (but not the Normal text following the Heading 3 Quote Reference) with a different style (say Style1). I'd guess this would best be done as a separate document-level operation.
Alan
Macro to Replace Manual Linebreaks
-
- BronzeLounger
- Posts: 1545
- Joined: 26 Jan 2010, 11:36
- Location: Melbourne, Australia
Macro to Replace Manual Linebreaks
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78894
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro to Replace Manual Linebreaks
Your screenshot shows paragraph breaks, not manual line breaks:
Deleting a line break won't mess with styles, so may I assume that you meant paragraph breaks?
Deleting a line break won't mess with styles, so may I assume that you meant paragraph breaks?
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1545
- Joined: 26 Jan 2010, 11:36
- Location: Melbourne, Australia
Re: Macro to Replace Manual Linebreaks
Sorry, I DID mean manually inserted paragraph marks, NOT line breaks. There are no line breaks in the document.
Alan
Alan
-
- Administrator
- Posts: 78894
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro to Replace Manual Linebreaks
See if this does what you want - please test on a copy of the document.
Code: Select all
Sub RemoveParaBreaks()
Dim i As Long
Dim rng As Range
Application.ScreenUpdating = False
For i = ActiveDocument.Paragraphs.Count - 1 To 1 Step -1
If ActiveDocument.Paragraphs(i).Style = "Normal" And ActiveDocument.Paragraphs(i + 1).Style = "Normal" Then
Set rng = ActiveDocument.Paragraphs(i).Range
rng.Characters(rng.Characters.Count).Delete
End If
Next i
Application.ScreenUpdating = True
End Sub
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78894
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro to Replace Manual Linebreaks
Can you explain your other request about quote text in more detail? What exactly do you mean by quote text?
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1545
- Joined: 26 Jan 2010, 11:36
- Location: Melbourne, Australia
Re: Macro to Replace Manual Linebreaks
Worked like a charm across all 120 pages! (
Not that there was ever any doubt
) Now to figure out how you dun it!
EDIT: I should add that I had to ensure that each para mark to be deleted was preceded by a <space> to ensure words didn't run together.
The other request is how to change the style of essentially every second block of Normal text. That is, there are blocks sitting between Quote Title (Heading2) and Quote Reference (Heading3) - this is what I called the "quote text". I want to change its style (across the document) to (let's say) Quote style.
But the other blocks of Normal text, between Quote Reference (Heading3) and the next Date (Heading1) I just want to leave as Normal.
thanks
Alan
![Whisper :whisper:](./images/smilies/whisper.gif)
![Smile :smile:](./images/smilies/smile.gif)
EDIT: I should add that I had to ensure that each para mark to be deleted was preceded by a <space> to ensure words didn't run together.
The other request is how to change the style of essentially every second block of Normal text. That is, there are blocks sitting between Quote Title (Heading2) and Quote Reference (Heading3) - this is what I called the "quote text". I want to change its style (across the document) to (let's say) Quote style.
But the other blocks of Normal text, between Quote Reference (Heading3) and the next Date (Heading1) I just want to leave as Normal.
thanks
Alan
Last edited by AlanMiller on 08 Feb 2017, 14:22, edited 1 time in total.
-
- Administrator
- Posts: 78894
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro to Replace Manual Linebreaks
Assuming that you have already removed the paragraph breaks between the Normal style paragraphs:
Code: Select all
Sub ChangeQuotes()
Application.ScreenUpdating = False
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = ""
.Style = wdStyleHeading2
.Wrap = wdFindStop
Do While .Execute
Selection.Paragraphs(1).Next.Style = "Quote"
Loop
End With
Application.ScreenUpdating = True
End Sub
Best wishes,
Hans
Hans
-
- BronzeLounger
- Posts: 1545
- Joined: 26 Jan 2010, 11:36
- Location: Melbourne, Australia
Re: Macro to Replace Manual Linebreaks
Worked like magic again. Many thanks Hans. These have both given me food for study - I'm so rusty on Word I need a tetanus booster.
I'd never have thought to work from "bottom up" in your first macro. Very clever indeed.
Alan
I'd never have thought to work from "bottom up" in your first macro. Very clever indeed.
Alan