Tricky text manipulation problem

G

Gem_man

This is driving me nuts because I cannot even get off the starting grid.

I want to be able to programmatically go to each paragraph in large document
that starts with "Yummy Fruit" and is in style Header 2 (Yummy Fruit may
appear else where but wont be in Heading 2)

Then copy only the text that is in bold from the paragraph below and copy it
to the paragraph above "Yummy Fruit"

It sounds easy but I am darned if i can figure it out.


This is what it looks like before the code would act on it:-


Yummy Fruit (In style Heading 2)
1. try eating (in non-bold) Oranges, Apples and Pears (in bold) they are
really good for you.

This is what I want:-

Oranges, Apples and Pears (anotherStyle)
Yummy Fruit (style Heading 2)
1. try eating (in non-bold) Oranges, Apples and Pears (in bold) they are
really good for you.

Has anyone got any brilliant ideas to get me started? I really would be most
grateful

Regards
 
J

Jean-Guy Marcil

Gem_man was telling us:
Gem_man nous racontait que :
This is driving me nuts because I cannot even get off the starting
grid.

I want to be able to programmatically go to each paragraph in large
document that starts with "Yummy Fruit" and is in style Header 2
(Yummy Fruit may appear else where but wont be in Heading 2)

Then copy only the text that is in bold from the paragraph below and
copy it to the paragraph above "Yummy Fruit"

It sounds easy but I am darned if i can figure it out.


This is what it looks like before the code would act on it:-


Yummy Fruit (In style Heading 2)
1. try eating (in non-bold) Oranges, Apples and Pears (in bold) they
are really good for you.

This is what I want:-

Oranges, Apples and Pears (anotherStyle)
Yummy Fruit (style Heading 2)
1. try eating (in non-bold) Oranges, Apples and Pears (in bold) they
are really good for you.

Has anyone got any brilliant ideas to get me started? I really would
be most grateful

Play arpound with someting like:

Sub Yummy()

Dim rgeDoc As Range
Dim rgePara As Range

Set rgeDoc = ActiveDocument.Range(0, 0)
With rgeDoc.Find
.Text = "Yummy Fruit"
.Style = ActiveDocument.Styles(wdStyleHeading2)
Do While .Execute
Set rgePara = .Parent.Paragraphs(1).Next.Range.Paragraphs(1).Range
With rgePara.Find
.Font.Bold = True
.Execute
If .Found Then
With rgeDoc.Duplicate
.InsertBefore Chr(13)
.Collapse wdCollapseStart
.Style = "Normal"
.FormattedText = rgePara.FormattedText
End With
End If
End With
rgeDoc.Start = rgeDoc.End
Loop
End With

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Gem_man

Superb! Many many thanks Jean-Guy

Jean-Guy Marcil said:
Gem_man was telling us:
Gem_man nous racontait que :


Play arpound with someting like:

Sub Yummy()

Dim rgeDoc As Range
Dim rgePara As Range

Set rgeDoc = ActiveDocument.Range(0, 0)
With rgeDoc.Find
.Text = "Yummy Fruit"
.Style = ActiveDocument.Styles(wdStyleHeading2)
Do While .Execute
Set rgePara = .Parent.Paragraphs(1).Next.Range.Paragraphs(1).Range
With rgePara.Find
.Font.Bold = True
.Execute
If .Found Then
With rgeDoc.Duplicate
.InsertBefore Chr(13)
.Collapse wdCollapseStart
.Style = "Normal"
.FormattedText = rgePara.FormattedText
End With
End If
End With
rgeDoc.Start = rgeDoc.End
Loop
End With

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Gem_man

Ok, I know this is cheeky, but acn anyone tell me how to get some text in
front of the text caption that has been moved to the paragraph above?

So it will now look like this?

Some suggested food Oranges, Apples and Pears (anotherStyle)
Yummy Fruit (style Heading 2)
1. try eating (in non-bold) Oranges, Apples and Pears (in bold) they
are really good for you.

Thank you very much in advance

Adrian
 
G

Greg Maxey

Try:

Sub Yummy()
Dim rgeDoc As Range
Dim rgePara As Range
Set rgeDoc = ActiveDocument.Range(0, 0)
With rgeDoc.Find
.Text = "Yummy Fruit"
.Style = ActiveDocument.Styles(wdStyleHeading2)
Do While .Execute
Set rgePara =
..Parent.Paragraphs(1).Next.Range.Paragraphs(1).Range
With rgePara.Find
.Font.Bold = True
.Execute
If .Found Then
With rgeDoc.Duplicate
.Collapse wdCollapseStart
.Move wdCharacter, -1
.Style = "Normal"
.InsertBefore Chr(13)
.FormattedText = rgePara.FormattedText
.InsertBefore "Some suggested foods "
End With
End If
End With
rgeDoc.Start = rgeDoc.End
Loop
End With
End Sub
 
G

Gem_man

Thanks Greg

Exactly as you had it didnt work quite right but with a bit of changing
around it works a treat.

Thanks for pointing me in the right direction

Regards
Adrian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top