order of paragraphs

L

Laura

hi!
With a text document, i have to give format to the paragraphs depending on
the first character of every one.
(if first character of the paragraph is a 'p' then-->paragraph bold,....
if first character of the paragraph is a 'd' then --> paragraph italic,... )
I'm programming a macro that uses the mail merge to filter an specific letter.
To know the correct order of the paragraphs, I do this:

For i = 1 To numParrafos

ActiveDocument.Paragraphs(i).Range.Select
Selection.HomeKey Unit:=wdLine
Selection.TypeText (i & "*")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
……..
Next i

But when I get the output document, and I order in alphabetical order, I get
an undesired order (because take the numbers as text).

Is there any way to convert a text-field to number-field?
If don’t: do you know any way to resolved the problem?
Thank you
 
D

Dave Lett

Hi Laura,

I'm not really sure what you're asking. Are you asking for help with
sorting? Are you asking for help with changing to bold/italic font based on
the first character? You ask about changing a text field to a number field,
but the sample code wouldn't insert a field? Essentially, I don't really
understand the problem.

I _think_ the problem is this.

You have a number of paragraphs. Each paragraph starts with either "p" or
"d". You want to group all the "p" paragraphs together and all the "d"
paragraphs together. Then, you want to cycle through each paragraph in the
document and put a number before it.

If this is your problem, then you can use something like the following:

Dim iPara As Integer

ActiveDocument.Content.Sort
For iPara = 1 To ActiveDocument.Paragraphs.Count
ActiveDocument.Paragraphs(iPara).Range.InsertBefore Text:=iPara & "*"
Next iPara

HTH,
Dave
 
L

Laura

hi Dave, I'l try to explain better my problem
when you put the number of the paragraph ,as you well say, you have to put:

For iPara = 1 To ActiveDocument.Paragraphs.Count
ActiveDocument.Paragraphs(iPara).Range.InsertBefore Text:=iPara & "*"
Next iPara

with this lines you get,for example:

1*p*hello dave
2*p*thank
' more liness
10*d*you very
11*p*much

then, i filter the data, for example: only the paragraphs that begin with
'p'....and apply the format that i want. I do this with mail merge, so now, i
have to put together all the sub-documents that i have get.

so i have something like this:

1*p*hello dave
2*p*thank
11*p*much
' more liness
10*d*you very

and finally i must order the paragraphs, but i get this output:

1*p*hello dave
10*d*you very
11*p*much
'more lines
2*p*thank

this is because i'm appling an text-alphabetical-order, and i need a
number-alphabetical-order.

text-alphabetical-order: 1-10-11-12-----2-20-21-----
numer-alphabetical-order: 1-2-----9-10-11

Do you know what i mean?

THANK YOU IN ADVANCE
 
D

Dave Lett

Hi Laura,

Yea, I think I know what you want. You start with paragraphs that have p or
d in front of them. You want to group those. Then, you want to insert
numbers before each paragraph.

Did you try the routine that I posted in the last message?

Dave
 
L

Laura

Hello!
Yes, I,ve try it and it doesn't work. When I do the mailmerge the field
takes text properties.
i think tha the solution is to specify in the combination that this field is
a number, but i don't know how to do it.

ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""order"""
 
D

Dave Lett

Hi again,

I don't quite understand. That is, you insert the numbers so that it's
organized numerically: 1 through x number of paragraphs. Then you, as you
say, "filter the data" and apply the format that you want and you do this
with a mailmerge. You don't need to do all of that. You apply the format
without filtering and without running a mailmerge.

Let's say that you start with the following (no formatting applied to
anything:

p*hello dave
p*thank
' more liness
d*you very
p*much

If you run the following routine,

Dim iPara As Integer

With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Text = "d*"
With .replacement
.ClearFormatting
.Text = ""
.Font.Italic = True
End With
.Execute Replace:=wdReplaceAll

.Text = "p*"
With .replacement
.ClearFormatting
.Text = ""
.Font.Bold = True
End With
.Execute Replace:=wdReplaceAll

.Text = "*"
With .replacement
.Text = "*"
.Font.Bold = False
.Font.Italic = False
End With
.Execute Replace:=wdReplaceAll
End With
End With

For iPara = 1 To ActiveDocument.Paragraphs.Count
ActiveDocument.Paragraphs(iPara).Range.InsertBefore Text:=iPara & "*"
Next iPara

With ActiveDocument
.Content.ConvertToTable Separator:="*", NumColumns:=3
.Tables(1).Sort
.Tables(1).ConvertToText Separator:="*"
End With


then you will end up with the following output, where "p" is bold and "d" is
italic
1*p*hello dave

2*p*thank

3*d*you very

4*p*much



HTH,

Dave
 
R

Russ

Laura,
Dave said in his first reply "Are you asking for help with changing to
bold/italic font based on the first character?

If that is one of your ultimate goals then maybe you can use some variation
of this code:
Dim aPara As Paragraph
For Each aPara In ActiveDocument.Paragraphs
Select Case aPara.Range.Characters(1)
Case "p"
aPara.Range.Font.Bold = True
Case "d"
aPara.Range.Font.Italic = True
End Select
Next aPara
 
L

Laura

Hi!
First of all i would like to say thank to you. I'm learning a lot in this
group

to simplify my first post I only write one field in the paragragh:
"1*p*hello", but really i have 6 fields in every paragraph (and every one
with different formats ) and a medium of 150 paragrphs per document, so I
thought that with mail merge it will be faster to format the paragraphs.
Isn't it?

I solved my problem with this code:
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""Lugar"" \# ""#0.00""", PreserveFormatting:=False

So, I give my field a number format, and when I sort the document
(activeDocument.Content.Sort), I get the correct order
 
R

Russ

Howdy,
It's still a little confusing. Could you show an small example of the data
with six fields per paragraph? And explain how you might want to format each
field? I was confused because you said you were trying to format the whole
paragraph bold for instance when it started with a 'p', but now you're
saying a paragraph might have six distinctly different formats within the
paragraph?
 
L

Laura

Hi Russ. Here comes the example. It´s complex to explain

My start point is a document ‘encode’:

P*6.5*Identificación de Elementos*Los elementos integrantes de la
configuración física de cualquier producto, se identifican con designadores
de referencia, que estructurados *Ejemplo*Esto es un ejemplo
1*a*CTAEA-SJ-SE-4*Designador correspondiente al Bastidor Energía *Sala de
equipos*
1*b*CTAEA-SJ-FN-21W99P1*Designador correspondiente al Conector*Sistema
Control de Tráfico
D*Plano 1**En función del grado jerárquico del producto, se utilizan
designadores con estructura perteneciente a uno de los siguientes niveles:*
2*a*Nivel Sistema/Subsistema*Este sistema…*

Where the first character of every paragraph, indicates the kind of format
it will have. It will take different values: p,d,1,2,3,a,x,c,s…

The paragraphs have this fields (separated by the asteristic simbol)
kind*number*title*explanation.

Number--> bold.
title must --> bold and underline
explanation--> without bold neither underline

so I thought that I must use mail merge (using this document as data source).
To know the correct order of each sentence, I've added at the beginning of
each paragraph, the number of order.

1*P*6.5*Identificación de Elementos*Los elementos integrantes de la
configuración física de cualquier producto, se identifican con designadores
de referencia*Ejemplo*Esto es un ejemplo
2*1*a*CTEA-SJ*Designador correspondiente al Bastidor Energía *Sala de equipos*
3*1*b*CTAA-SJ-FN*Designador correspondiente al Conector*Sistema Control de
Tráfico
…

And i do the filter of each kind of paragraphs (p,d,1,….), but when I join
together the sub-documents obtained with the combination, I found a problem:
the “number†field is text so when I order the paragraphs, get a wrong order:
1-10-11-12----2-3-4

For my macro it’s very important that the code executes as fast as possible.
Do you think that mail merge is a good option (taking in count that every
documents can have hundred of paragraphs) or that I have do it with a
select-case clause
 
R

Russ

Laura,
There is an expression... When you have a hammer, everything looks like a
nail. You are familiar with Mail Merge fields and I am familiar with using
find and replace to automate formatting with code. I think you could also
use Select Case, if the whole paragraph is formatted the same after testing
the first character; and I hope we agree on what Word considers to be a
paragraph. You can try both methods to see which is faster. Report back, if
using mail merge is significantly faster. I am not even sure how you can
format something with mail merge, unless you are talking about using markup
tags like in html code?
And by fields are you talking about mail merge fields that have special
field characters that look like << >>?

You can use this code to compare how much time your different subroutines
run to completion.
Dim StartTime As Single
StartTime = Timer
'...Run code here
MsgBox "Time taken was: " & (Timer - StartTime) & " seconds"
 

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