Word Macro

R

Rebecca

Season's Greetings.

I'm using MS Window XP and MS Office XP. I need a macro
for MS Word, but when it comes to programmimg in VBA I
don't know if I'm coming or going.

I will be deeply appreciative if you could patiently try
to understand my stumbling and lengthy explanation,
because the macro I desperately need will require some
very fancy programming, and least by my standards. Any
takers?

I have a document that has Hebrew and English words and
(greatly simplified) looks structually like this (because
Hebrew fonts can not be displayed in this post, I will use
made-up words after the numbers):

WTT Genesis 1:1

1. fghi
a. fghi

2. gfjk
a. k
b. gfj

3. lmnop
a. p
b. o
c. lmn

4. eee
a. eee

5. ddaaaa
a. a
b. aa
c. aaa
d. dda

WTT Genesis 1:2

1. bbbac
a. c
b. a

2. cbaa
a. cbaa

3. ecda
a. a
b. d
c. ec

and so on, and so forth. Each line consists of one
paragraph. After each number (say the "1. fghi," above in
Genesis 1:1) there is either an

a. ...,
or an a. ... and b. ...,
or an a. ..., b. ..., and c. ...,
or an a. ..., b. ..., c. ..., and d. ...

(each of the four letters are, of course, on a separate
line).

Now I need a macro to proceed from the top of the file to
the bottom of a file in "Document A," and after each
number "determine" if there is only an "a. ..." or
an "a. ..." plus a "b. ..." or whatever the combination
(a. through d.).

Then, to complicate matters, once the macro determines the
number of letters (a. through d.) in the sentences below
the numbered paragraph, to go back up to the same number
(say "1. fghi," above) copy the entire word ("fghi" in
Document A), and then paste this same word into another
file (in "Document B" -- thus, there will be two windows
open, side by side, A on the right and B on the left).

However, if there is an "a." and a "b." after the number,
the macro should go back up to the numbered sentence above
(say "2. gfjk" in verse 1:1), copy the word after the
letter, i.e., "gfjk," copy this word on the next line in
Document B, then delete everything but the last letter in
the word (i.e., delete "gfj" and leave the "k"), then jump
down one paragraph copy the same word again, but this time
delete the last letter (i.e., the "k") in the
word "gfjk." Thus, so far the paragraphs in Document B
should look like this:

fghi
k
gfj

One more thing. I need the macro to stop temporarily
whenever there are sentences with "c"s or "d"s in them, in
order to enable me to do something manually, then push a
key and get the program to start up again.

Phew! If you are still with me, could you please give me
some hints about how I can go about creating such a
macro? I'm sure there are better ways to perform the
operation I need done (or more lucid ways of explaning
what I need done), but I could use all the help I can get.

Regards,

Rebecca
 
C

Cindy M -WordMVP-

Hi Rebecca,

Hmmm, let's try to break this down into bite-sized steps and
see how we get along.

Before we can proceed with any code, however, we need to know
if you used Word's Autonumbering on this? Or were the numbers
typed as plain text? Or were some kind of fields used (such
as AutoNum or Seq)?
I'm using MS Window XP and MS Office XP. I need a macro
for MS Word, but when it comes to programmimg in VBA I
don't know if I'm coming or going.

I will be deeply appreciative if you could patiently try
to understand my stumbling and lengthy explanation,
because the macro I desperately need will require some
very fancy programming, and least by my standards. Any
takers?

I have a document that has Hebrew and English words and
(greatly simplified) looks structually like this (because
Hebrew fonts can not be displayed in this post, I will use
made-up words after the numbers):

WTT Genesis 1:1

1. fghi
a. fghi

2. gfjk
a. k
b. gfj

3. lmnop
a. p
b. o
c. lmn

4. eee
a. eee

5. ddaaaa
a. a
b. aa
c. aaa
d. dda

WTT Genesis 1:2

1. bbbac
a. c
b. a

2. cbaa
a. cbaa

3. ecda
a. a
b. d
c. ec

and so on, and so forth. Each line consists of one
paragraph. After each number (say the "1. fghi," above in
Genesis 1:1) there is either an

a. ...,
or an a. ... and b. ...,
or an a. ..., b. ..., and c. ...,
or an a. ..., b. ..., c. ..., and d. ...

(each of the four letters are, of course, on a separate
line).

Now I need a macro to proceed from the top of the file to
the bottom of a file in "Document A," and after each
number "determine" if there is only an "a. ..." or
an "a. ..." plus a "b. ..." or whatever the combination
(a. through d.).

Then, to complicate matters, once the macro determines the
number of letters (a. through d.) in the sentences below
the numbered paragraph, to go back up to the same number
(say "1. fghi," above) copy the entire word ("fghi" in
Document A), and then paste this same word into another
file (in "Document B" -- thus, there will be two windows
open, side by side, A on the right and B on the left).

However, if there is an "a." and a "b." after the number,
the macro should go back up to the numbered sentence above
(say "2. gfjk" in verse 1:1), copy the word after the
letter, i.e., "gfjk," copy this word on the next line in
Document B, then delete everything but the last letter in
the word (i.e., delete "gfj" and leave the "k"), then jump
down one paragraph copy the same word again, but this time
delete the last letter (i.e., the "k") in the
word "gfjk." Thus, so far the paragraphs in Document B
should look like this:

fghi
k
gfj

One more thing. I need the macro to stop temporarily
whenever there are sentences with "c"s or "d"s in them, in
order to enable me to do something manually, then push a
key and get the program to start up again.

Phew! If you are still with me, could you please give me
some hints about how I can go about creating such a
macro? I'm sure there are better ways to perform the
operation I need done (or more lucid ways of explaning
what I need done), but I could use all the help I can get.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
R

Rebecca

Dear Cindy,

No, I did not use WORD's autonumbering, nor are there any
fields in the documents.

Regards,

Rebecca
 
C

Cindy M -WordMVP-

Hi Rebecca,
I did not use WORD's autonumbering, nor are there any
fields in the documents.
OK, that won't make things any easier...

Looking at the sample you posted, I need to double check:
- ENTER was pressed at the end of every "outline" entry?
- and ENTER was pressed twice between numbered entries?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
R

Rebecca

Dear Cindy,

No, the original documents have two ENTERs only after the
chapters-verses and at the last enter in each chapter,
such as

WTT Genesis 1:1

1. blah, blah
a. blah, blah
b. blah, blah
2. blah, blah
a. blah, blah
3. blah
a. blah
b. blah
c. blah

WTT Genesis 1:2

1. blah, blah
a. blah
b. blah
2. blah, blah
a. blah
3. blah
a. blah
b. blah

etc.
 
C

Cindy M -WordMVP-

Hi Rebecca,
No, the original documents have two ENTERs only after the
chapters-verses and at the last enter in each chapter
OK :) Try the following, as a starter, to make sure the
logic is basically correct. This only processes the lines
containing (a). Once we're sure this is doing what you have
in mind, we can work out the other Cases.

(Watch out for the code breaking over due to the message
breaking differently as its processed through the Internet!)

Sub ParseText()
Dim rng As Word.Range
Dim para As Word.Paragraph
Dim docA As Word.Document
Dim docB As Word.Document
Dim bStart As Boolean
Dim rngB As Word.Range, rngTemp As Word.Range

Set docA = ActiveDocument
Set rng = docA.Range
Set docB = Documents.Add
Set rngB = docB.Range

'Process the document, group by group
Do While rng.Start <> rng.End - 1
'Get range for top outline level block
For Each para In rng.Paragraphs
If IsNumeric(para.Range.Characters(1)) _
And Not bStart Then
'Starts with a number = top level outline
Set rng = para.Range
bStart = True
ElseIf IsNumeric(para.Range.Characters(1)) _
And bStart Then
rng.End = para.Range.Start
bStart = False
Exit For
End If
Next para

'Process outline
Dim iNumLines As Integer

iNumLines = rng.Paragraphs.Count
Debug.Print iNumLines
Select Case iNumLines
Case 1
'Only the top level present
Case 2
'(a) only
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End
rngB.FormattedText = rngTemp.FormattedText
rngB.Collapse wdCollaseEnd
Case 3
'a + b
Case 4
'a, b, c
Case 5
'a, b, c, d
Case Else
MsgBox "More than 5 lines!"
End Select
rng.Collapse wdCollapseEnd
rng.End = docA.Range.End
Loop
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
R

Rebecca

Dear Cindy,

It seems to work OK, but "More than 5 lines!" pops up, and
after I push ESC the macro continues. I deeply appreciate
your help, Cindy.

Regards,

Rebecca
 
C

Cindy M -WordMVP-

Hi Rebecca,
It seems to work OK, but "More than 5 lines!" pops up, and
after I push ESC the macro continues.
Good! We'll need to track that down, once we get the other
parts working. Possibly, you've pressed ENTER quite a few
times between "Chapter headings"? I'll try modifying the
message to display where this is being picked up and you can
check how these places can be indentified...
However, if there is an "a." and a "b." after the number,
the macro should go back up to the numbered sentence above
(say "2. gfjk" in verse 1:1), copy the word after the
letter, i.e., "gfjk," copy this word on the next line in
Document B, then delete everything but the last letter in
the word (i.e., delete "gfj" and leave the "k"), then jump
down one paragraph copy the same word again, but this time
delete the last letter (i.e., the "k") in the
word "gfjk." Thus, so far the paragraphs in Document B
should look like this:

fghi
k
gfj
Looking at this, would you always get the same result if one
just "took apart" the top-level entry? Or do you have entry
sets where this would not pertain?
One more thing. I need the macro to stop temporarily
whenever there are sentences with "c"s or "d"s in them, in
order to enable me to do something manually, then push a
key and get the program to start up again.
What kind of things? There's no way, really, to "start up
again", although there are ways this could be emulated. But
it would help to know what kind of thing you need to do.
Also, should the macro process such groups at all, or just
skip them?

Here's the next step:

Sub ParseText()
Dim rng As Word.Range
Dim para As Word.Paragraph
Dim docA As Word.Document
Dim docB As Word.Document
Dim bStart As Boolean
Dim rngB As Word.Range, rngTemp As Word.Range

Set docA = ActiveDocument
Set rng = docA.Range
Set docB = Documents.Add
Set rngB = docB.Range

'Process the document, group by group
Do While rng.Start <> rng.End - 1
'Get range for top outline level block
For Each para In rng.Paragraphs
If IsNumeric(para.Range.Characters(1)) _
And Not bStart Then
'Starts with a number = top level outline
Set rng = para.Range
bStart = True
ElseIf IsNumeric(para.Range.Characters(1)) _
And bStart Then
rng.End = para.Range.Start
bStart = False
Exit For
End If
Next para
'The end of the document
If bStart Then rng.End = docA.Range.End - 1
'Process outline
Dim iNumLines As Integer

iNumLines = rng.Paragraphs.Count
Debug.Print iNumLines
Select Case iNumLines
Case 1
'Only the top level present
Case 2
'(a) only
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End
rngB.FormattedText = rngTemp.FormattedText
rngB.Collapse wdCollaseEnd
Case 3
'a + b
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End - 1
rngB.FormattedText =_
rngTemp.Characters(1).FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
rngB.FormattedText = _
rngTemp.Characters.Last.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollaseEnd
Case 4
'a, b, c
Case 5
'a, b, c, d
Case Else
MsgBox "More than 5 lines!" & vbCr & rng.Text
End Select
rng.Collapse wdCollapseEnd
rng.End = docA.Range.End
Loop
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
R

Rebecca

Dear Cindy,

Sorry to give you such a perplexing and frustrating
request, and I believe I have failed to articulate the
problem coherently. I sent you two e-mails that may help
you visualize the material I need to analyze. I ran the
macro you wrote, but there seems to be a slight problem.

Hebrew words are backwards (they go right to left), and
the articles (such as our English "the") and the like are
at the far right. Let me try to explain again through
these examples below.

Gen 1:1

1. abbT
a. T
b. abb

After the macro is run, DOC 2 should look like this:

T
abb

(the "T" at the end of the word "aabT" is copied and
pasted on the first line of DOC 2, and then the rest of
the word before the "T" -- "abb" is copied onto the second
line of DOC 2).

Another example:

2. abxiitq
a. abixiitq

After the macro is run, DOC 2 so far should look like this
(because they is only an "a."):

T
abb
abiitq

Now with a numbered "section" with three letters (a, b,
and c):

3. pityrFB
a. T
b. F
c. pityr

After the macro is run, DOC 2 so far should look like this:

T
abb
abiitq
B
F
pityr

Notice that the "B" came first (in "3." above), then the
next letter "F" followed, and finally the rest of the word
after the number 3. (pityr).

I hope this explanation helped.

Rebecca
 
C

Cindy M -WordMVP-

Hi Rebecca,
I sent you two e-mails that may help
you visualize the material I need to analyze
Please do NOT send unsolicited emails. They get deleted
without being read.
Now with a numbered "section" with three letters (a, b,
and c):

3. pityrFB
a. T
b. F
c. pityr

After the macro is run, DOC 2 so far should look like this:

T
abb
abiitq
B
F
pityr
Is this correct? B or T? (a) is a T?

Should the macro simply be taking each lettered (as opposed
to numbered) line, in order, into the new document? That's
what all your examples have shown, really...

It's very important you provide a clear, concise set of
logical rules. These groups are actually here to help you
learn, but without understanding exactly what needs to be
done, it's not possible to get you started on your own. And
our time for providing FREE help is not unlimited...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
R

Rebecca

Dear Cindy,

I didn't realize the files I sent you would be
automatically deleted. I thought they might have greatly
helped you visualize what needs to be done. But I will
try to explain again, and I promise for the last time.

Between each Bible verses, say from GEN 1:1 to GEN 1:1,
there are numbers corresponding roughly to the number of
Hebrew words in each verse. Now, while there are actually
11 words in GEN 1:1, the database I am usuing lists only 7
numbers because articles and conjunctions (always only one
letter) and the like are attached to the right of some
Hebrew words. For example (and these words are made up
and the articles are capitalized),

GEN 1:1

1. abbxT
a. T
b. abb

The fact that there is an "a." and a "b." warns me that
there is an article after the Hebrew word after the
number "1" -- that is, the "T" before the Hebrew
word "abbx" in "1." above. The macro will go to the end
of the "abbxT" and copy the "T", delete it, paste it into
DOC 2, go back to DOC 1, copy the "abbx", past it into DOC
2 on the second line. So the result will look like this:

T
abbx

Note that after the "b." the word "abb" is different from
the word "abbx" in "1" above.

Now after some numbers there is only an "a", such as

2. tabboa
a. tabboa

In this case all the macro has to do is copy the "tabboa"
after the number "2.", and then paste it in DOC 2 on the
next or third line. So the result will look like this:

T
abbx
tabboa

Finally after some numbers there is an "a", "b", and "c"
after the number, such as

3. cggazTB
a. B
b. T
cgga

In this case the macro should copy the "B", delete it,
paste it in DOC 2, go back to DOC 1 and copy the "T",
delete it, and past it in DOC 2, go back and copy
the "cggaz", and copy it in DOC 2. So it should look like
this:

T
abbx
tabboa
B
T
cggaz

Some verses have numbers with a "d" but they are very few
(thank God), so I will go and do it by hand, so to speak.
So the only problem involves three letters, a, b, and c.

Below is a sample from GEN 1

WTT Genesis 1:1


1. tyvarb
a. b
b. tyvar
2. arb
a.
3. ~yhla
a. ~yhla
4. ta
a. ta
5. ~ymvh
a. h
b. ~ymv
6. taw
a. w
b. ta
7. #rah
a. h
b. #ra

WTT Genesis 1:2


1. #rahw
a. w
b. h
c. #ra
2. htyh
a. hyh
3. wht
a. wht
4. whbw
a. w
b. whb
5. $vxw
a. w
b. $vx
6. l[
a. l[
7. ynp
a. hnp
8. ~wht
a. ~wht
9. xwrw
a. w
b. xwr
10. ~yhla
a. ~yhla
11. tpxrm
a. @xr
12. l[
a. l[
13. ynp
a. hnp
14. ~ymh
a. h
b. ~ym

WTT Genesis 1:3

1. rmayw
a. w
b. rma

etc.

I sincerely hope this has clarified the problem.

Regards,

Rebecca
 
C

Cindy M -WordMVP-

Hi Rebecca,
sincerely hope this has clarified the problem.
I think (hope) so.

Try the following. Note that it corresponds to your problem
DESCRIPTION, and not to the text samples you've posted,
here. In all the text samples I've copied/pasted for
testing there's been a single space in each "empty"
paragraph line. I'm assuming truly empty paragraphs in the
code.

Sub ParseText()
Dim rng As Word.Range
Dim para As Word.Paragraph
Dim docA As Word.Document
Dim docB As Word.Document
Dim bStart As Boolean
Dim rngB As Word.Range, rngTemp As Word.Range

Set docA = ActiveDocument
Set rng = docA.Range
Set docB = Documents.Add
Set rngB = docB.Range

'Process the document, group by group
Do While rng.Start <> rng.End - 1
'Get range for top outline level block
For Each para In rng.Paragraphs
If IsNumeric(para.Range.Characters(1)) And _
Not bStart Then
'Starts with a number = top level outline
Set rng = para.Range
bStart = True
ElseIf IsNumeric(para.Range.Characters(1)) _
And bStart Then
'We've hit the next top outline block
rng.End = para.Range.Start
bStart = False
Exit For
ElseIf Len(para.Range.Text) <= 1 And bStart Then
'We've hit empty paragraph -
'end of this set of outlines
rng.End = para.Range.Start
bStart = False
Exit For
End If
Next para
'bStart is still true, the end of the document
If bStart Then rng.End = docA.Range.End - 1
'Process outline
Dim iNumLines As Integer

iNumLines = rng.Paragraphs.Count
'Debug.Print iNumLines
Select Case iNumLines
Case 1
'Only the top level present
Case 2
'(a) only
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End
rngB.FormattedText = rngTemp.FormattedText
rngB.Collapse wdCollapseEnd
Case 3
'a + b
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End - 1
rngB.FormattedText = _
rngTemp.Characters.Last.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
rngTemp.MoveEnd wdCharacter, -1
rngB.FormattedText = rngTemp.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
Case 4
'a, b, c
Set rngTemp = rng.Duplicate
rngTemp.MoveStartUntil Cset:=" "
rngTemp.MoveStart wdCharacter, 1
rngTemp.End = rng.Paragraphs(1).Range.End - 1
rngB.FormattedText = _
rngTemp.Characters.Last.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
rngTemp.MoveEnd wdCharacter, -1
rngB.FormattedText = _
rngTemp.Characters.Last.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
rngTemp.MoveEnd wdCharacter, -1
rngB.FormattedText = rngTemp.FormattedText
rngB.InsertAfter vbCr
rngB.Collapse wdCollapseEnd
Case 5
'a, b, c, d
Case Else
MsgBox "More than 5 lines!" & vbCr & rng.Text
End Select
rng.Collapse wdCollapseEnd
rng.End = docA.Range.End
Loop
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
R

Rebecca

Dear Cindy,

Thanks a million. The macro works like a charm. I envy
your programming wizardry. Again, thanks for your sincere
help.

Rebecca
 

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