selecting and moving text

P

presknight

I need to loop through a document and each line gets broken into 3
possible parts, beginning is bold, possible middle is italic, and end
is regular. I need to switch the the locations of the bold and regular
parts then reformat so the beginning is again bold and the end is again
regular...easier seen using html tags because i can't show bold and
italics here but i'd like to do it in word.

before
<b> the quick</b> <i>brown</i> fox jumped

after
<b>fox jumped</b> <i>brown</i> the quick
 
D

Dave Lett

Are you dealing with names? If so, have a look at Example 1 of the article
"Finding and replacing characters using wildcards" at
http://word.mvps.org/FAQs/General/index.htm

You can also use that site to find the answer to your question about
formatting the different parts of your find/replace.

HTH,
Dave
 
P

presknight

It's for a polish-english technical dictionary so sometimes the entries
are single words, but they can be around ten words or so, and the words
don't repeat, so replacing with wildcards won't work. I have: phrase -
catagory - translation. with the phrase in bold, catagory in italics,
and the translation in regular. I have the english-polish part, now i
need the polish-english part, so i need to swap the phrase and
translation, then reformat to bold and regular leaving the catagory
alone.


Dave Lett napisal(a):
 
D

Dave Lett

Hi,

Is the character that separates your items in phrase - category -
translation consistent? If so, you can select all the text that you want to
switch around, convert it to a table, select and move the columns, select
and format the text in the columns, and then convert it back to text from
the table. Follow?

HTH,
Dave
 
P

presknight

sorry, but the only difference between the text is the formatting.
bold, italics, and regular. there are no separating characters.
the number of words differs, and sometimes there is no italics group.

I've been reading a little on vba and I think I have to make each line
into three range groups, swap the first and last, then reformat from
bold to regular, but i haven't quite learned how yet.


Dave Lett napisal(a):
 
D

Dave Lett

Hi again,

Does each phrase - category - translation occupy one and only one paragraph,
at least?

Dave
 
P

presknight

yeah, phrase - cat - trans group is on its own line. so if the bold is
marked as range, then the italics, then the rest of the line is normal.

oh and thanks a lot for your interest and help.

-pres

Dave Lett napisal(a):
 
D

Dave Lett

Okay,
It looks like you can do this with a simple series of search/replace.

Try the following routine on a sample document:

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = ""
.Font.Bold = True
With .Replacement
.ClearFormatting
.Text = "<b>^&</b>"
End With
.Execute Replace:=wdReplaceAll


.ClearFormatting
.Text = ""
.Font.Italic = True
With .Replacement
.ClearFormatting
.Text = "<i>^&</i>"
End With
.Execute Replace:=wdReplaceAll

.ClearFormatting
.Text = "(\<b\>*\</b\>)*(\<i\>*\</i\>)(*)(^13)"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Text = "<bold>\3<bold> \2 \1 \4"
End With
.Execute Replace:=wdReplaceAll

.ClearFormatting
.Text = "\<b\>(*)\</b\>"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Font.Italic = False
.Font.Bold = False
.Text = "\1"
End With
.Execute Replace:=wdReplaceAll

.ClearFormatting
.Text = "\<bold\>(*)\<bold\>"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Font.Bold = True
.Text = "\1"
End With
.Execute Replace:=wdReplaceAll

.ClearFormatting
.Text = "\<*\>"
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Text = ""
End With
.Execute Replace:=wdReplaceAll
End With
End With


HTH,
Dave
 
P

pres

good morning dave,
this works weel except for one little problem. about 5% of the entries
don't follow the format of phrase - cat - translation. but only have
phrase - translation. so there is no catagory group in italics. when
this happens, we lose some of the lines. i'll try and see what i can
do with your code, or if you have more time and ideas that would be
great.

thanks a lot

-pres


Dave Lett napisal(a):
 
D

Dave Lett

Good morning,

Well, we can do this paragraph by paragraph, but that will take the routine
quite a bit longer than using search/replace. The goal of the following
routine is to get the text into a format that you can convert it to a table
(manually) and then move the columns around (manually) and format the
columns (manually). The following routine does the grunt work by inserting a
single tab after bold text in each paragraph. If there is italic formatting
in the paragraph, then it inserts a table after that text, too. If there is
NOT italic formatting (no category), then it inserts an additional tab
character after the bold text (therefore, two tabs after the bold text in
paragraphs without italic formatting). You have to have two tabs to account
for the missing category text. When the routine is done, you're ready to do
the manual work listed above. CAVEAT: This routine assumes that there is
nothing else, really, in the document other than
phrase - category - translation

If that's true, then we could add code to deal with the stuff I have listed
as manual

Dim oRngBold As Range
Dim oRngItal As Range
Dim oRngReg As Range
Dim oPara As Paragraph

Selection.HomeKey Unit:=wdStory

For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Bold = True
If .Execute Then
Set oRngBold = Selection.Range
'''after you find bold formatting
'''insert a tab after that range
oRngBold.InsertAfter Text:=vbTab
End If
End With

oPara.Range.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
If .Execute Then
Set oRngItal = Selection.Range
Set oRngReg = ActiveDocument.Range(Start:=oRngItal.End,
End:=oPara.Range.End - 1)
'''after you find italic formatting
'''insert a tab after that range
oRngItal.InsertAfter Text:=vbTab
Else
Set oRngReg = ActiveDocument.Range(Start:=oRngBold.End,
End:=oPara.Range.End - 1)
'''if you don't find italic formatting
'''insert a tab after the bold range to account
'''for the missing category text
oRngBold.InsertAfter Text:=vbTab
End If
End With
Next oPara

HTH,
Dave
 
D

Dave Lett

Hi Pres,

It ain't pretty, but it seems to work based on the limitations I specified
in the earlier post:

Dim oRngBold As Range
Dim oRngItal As Range
Dim oRngReg As Range
Dim oPara As Paragraph

Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop

Selection.HomeKey Unit:=wdStory

For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Bold = True
If .Execute Then
Set oRngBold = Selection.Range
'''after you find bold formatting
'''insert a tab after that range
oRngBold.InsertAfter Text:=vbTab
End If
End With

oPara.Range.Select
With Selection.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
If .Execute Then
Set oRngItal = Selection.Range
Set oRngReg = ActiveDocument.Range(Start:=oRngItal.End,
End:=oPara.Range.End - 1)
'''after you find italic formatting
'''insert a tab after that range
oRngItal.InsertAfter Text:=vbTab
Else
Set oRngReg = ActiveDocument.Range(Start:=oRngBold.End,
End:=oPara.Range.End - 1)
'''if you don't find italic formatting
'''insert a tab after the bold range to account
'''for the missing category text
oRngBold.InsertAfter Text:=vbTab
End If
End With
Next oPara

With ActiveDocument
.Range.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=3
With .Tables(1)
'''move the phrase column to column three
.Columns.First.Select
'''remove bold formatting from the phrase column
Selection.Font.Bold = False
Selection.Cut
.Columns.Last.Cells(1).Select
Selection.MoveRight
Selection.Paste

'''move the category column to the second column
.Columns.First.Select
Selection.Cut
.Columns.First.Cells(1).Select
Selection.MoveRight
Selection.Paste

'''make the first column bold
.Columns.First.Select
Selection.Font.Bold = True

'''convert back to text
.ConvertToText Separator:=" "
End With

End With

HTH,
Dave
 
D

Dave Lett

Thanks, that what I keep telling my wife!
Just glad to hear that it solves your needs.

Dave
 

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