Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word automation question 1

Status
Not open for further replies.

daimaou

Programmer
Apr 4, 2001
154
PT
I have this problem:

I need to created a numbered list on ms word, this part I know how to do, here's the code I've used:

Set myltemp = ListGalleries(wdOutlineNumberGallery).ListTemplates(2)
myltemp.ListLevels(1).TabPosition = InchesToPoints(0.6)
myltemp.ListLevels(2).TabPosition = InchesToPoints(0.6)

myltemp.ListLevels(1).TrailingCharacter = wdTrailingTab
myltemp.ListLevels(2).TrailingCharacter = wdTrailingTab

myltemp.ListLevels(1).NumberPosition = InchesToPoints(0)
myltemp.ListLevels(2).NumberPosition = InchesToPoints(0)

ListTemplate:=myltemp

ActiveDocument.Paragraphs(initt).Range.ListFormat.ApplyListTemplate ListTemplate:=myltemp

Then I'll just add the items:

ActiveDocument.Range.InsertAfter ("Text 1")
ActiveDocument.Range.InsertParagraphAfter

and so on...

But now, how can I get out of the numbered list and write normal text? because from this point everything I write will be added as an item to the list...

I'd like to get something like:
1-text1
2-text2
3-text3

writing some text....

and not:
1-text1
2-text2
3-text3
4-
5-writing some text....

I'd really appreciate if anyone could help
 
First you need to get a range at the end of the list. I would use a Range variable.
Dim rng As Word.Range
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
Then you need to insert a new paragraph:
rng.InsertParagraphAfter
Then, format the paragraph with the Normal (or some other) style:
rng.ParagraphFormat.Style = "Normal"
You can then use the rng object to continue inserting text:
rng.InsertParagraphAfter
rng.InsertAfter "writing some text..." Rick Sprague
 
Thanks Rick, that does it.

However I have another question, when you do:

Set rng = ActiveDocument.Range

now each time you add some text to your document will rng automatically be updated to the end position?

I mean if you have

1. something

if u do Set rng = ActiveDocument.Range, rng will be on the position after the letter "g" right?

If I then add another line:

1. something
2. another thing

Where will be rng? at the end of the 2nd line or at the end of the first one?

This may seem a stupid question, but I started thinking about this and I'd like to know the correct answer :)
 
Daimaou,

When you do Set rng = ActiveDocument.Range, rng covers the entire document, from beginning to end. Each time you do rng.InsertAfter, the range still starts at the same place, but the end is moved after the inserted text.

After you do rng.Collapse wdCollapseEnd, the range is left after the last character, and before the paragraph mark at the end.

If you then do rng.InsertAfter "something", the range includes the word "something". Basically, the range keeps getting bigger as you do InsertAfter's.

It's possible for your range to contain many paragraphs. Be careful! If you do something like rng.Style = "Normal", the Normal style will be applied to every paragraph mark within the range! You almost always want to collapse the range before you apply a style.

It took me a long time to work out how ranges work, and how to apply styles and fonts to them. I never did figure out how to do lists; you're ahead of me there. And I don't know the rules for InsertBefore, either. I wish Microsoft had given us some better help documentation or example code! Rick Sprague
 
Thanks Rick, I finally seem to understand how the range works. I totally agree with you when you say Microsoft hasn't provided us good documentation on this.
It's really hard to find out examples or explanations for stuff like this.

Maybe this will change some day, but I doubt it...

And John I use that method quiet often also, but the problem is when I need to know something more that just isn't enough and it's really hard to find it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top