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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbering Paragraphs

Status
Not open for further replies.

Linguist

Technical User
Jul 11, 2000
40
US
Is there a way to apply numbering to paragraphs like SELBULLET does for bullets? If there isn't a built-in function, any suggestions on how to simulate it?

Any suggestions will be appreciated.

Thanks
 
Use the Word object model to open your file and then use the methods of the object.
Once you have your file open, (Note that it does not have to be visible while doing this) select the text you want numbered and then use the following line:

Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdNumberGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _
wdListApplyToWholeList


This assumes ListTemplates(1) has the correct formatting that you want. As an example, here is how to set the formatting of the list template:

With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(0)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.25)
.TabPosition = InchesToPoints(0.25)
.ResetOnHigher = True
.StartAt = 1
With .Font
.Bold = True
.Italic = False
.StrikeThrough = False
.Subscript = False
.Superscript = False
.Shadow = False
.Outline = False
.Emboss = False
.Engrave = False
.AllCaps = False
.Hidden = False
.Underline = False
.ColorIndex = wdAuto
.Size = 8
.Animation = wdAnimationNone
.DoubleStrikeThrough = False
.Name = "MyList"
End With
.LinkedStyle = ""
End With


Simon
 
Thanks, Simon, for the suggested code.

I added the Word Object Model to my components and inserted your code into my mnuInsertNumbers_Click event and when I ran the program I received this error message: "ActiveX could not create object."

Question: Since I'm using a RichTextBox control, is there a compatibility problem with that code?

By the way, is there an Intermediate Level VB6 book that you would recommend?

Thanks,
-Bob
 
How are you trying to instantiate the object??

Can you post the code that you are using??

Simon
 
Simon,

Here is the code that I have inserted into my RichTextBox Numbering sub:

Code:
Private Sub mnuFormatNumber_Click()
    
With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.25)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.5)
        .TabPosition = InchesToPoints(0.5)
        .ResetOnHigher = True
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .Strikethrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = ""
End With
ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
    Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
        wdNumberGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _
        wdListApplyToWholeList, DefaultListBehavior:=wdWord9ListBehavior
End Sub

When I run this code, I get this error message: Run-time error: '429': ActiveX component can't create object

Thanks for your help.

-Bob

 
This is because you are trying to use a property of the Word object without having set the object first - the code I posted first was just taken from a Word Macro that I created, and so this did not need any other code to make it work. (I did not test it in VB!!)

This, however, is tested in VB, and it works!!!

Option Explicit

Private Sub mnuFormatNumber_Click()

Dim objWord As New Word.Application
Dim objDoc As Document

With objWord.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
'With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = InchesToPoints(0.25)
.Alignment = wdListLevelAlignLeft
.TextPosition = InchesToPoints(0.5)
.TabPosition = InchesToPoints(0.5)
.ResetOnHigher = True
.StartAt = 1
With .Font
.Bold = wdUndefined
.Italic = wdUndefined
.Strikethrough = wdUndefined
.Subscript = wdUndefined
.Superscript = wdUndefined
.Shadow = wdUndefined
.Outline = wdUndefined
.Emboss = wdUndefined
.Engrave = wdUndefined
.AllCaps = wdUndefined
.Hidden = wdUndefined
.Underline = wdUndefined
.Color = wdUndefined
.Size = wdUndefined
.Animation = wdUndefined
.DoubleStrikeThrough = wdUndefined
.Name = ""
End With
.LinkedStyle = ""
End With
ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""

Set objDoc = objWord.Documents.Open("C:\aa\test.doc")
objDoc.Select

Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
wdNumberGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _
wdListApplyToWholeList, DefaultListBehavior:=wdWord9ListBehavior

objDoc.Save
objDoc.Close

Set objDoc = Nothing
Set objWord = Nothing

End Sub


PS. You have to set a reference to the Word library in order for this code to work - but I guess you already knew that, as the program wouldn't have compiled otherwise!!

Simon
 
Simon,

How would I use your code in a stand-alone application, not while in Word2000 (VB6 not VBA)?

I am particularly confused about how to use these lines of code:

Dim objWord As New Word.Application
Dim objDoc As Document

With objWord.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)

Set objDoc = objWord.Documents.Open("C:\aa\test.doc")
objDoc.Select

objDoc.Save
objDoc.Close

Set objDoc = Nothing
Set objWord = Nothing


Thanks for your help.
-Bob
 
My code should work in VB6, not VBA. I wrote it in VB6.

' create a new word object
Dim objWord As New Word.Application
' set a word document variable reference
Dim objDoc As Document

' use a property of the word object
With objWord.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)

' set the document object to the document you want to work on
Set objDoc = objWord.Documents.Open("C:\aa\test.doc")
' select the entire document
objDoc.Select

' save the document
objDoc.Save
' close the document
objDoc.Close

' remove reference to the object, freeing memory resources
Set objDoc = Nothing
' remove reference to the object, freeing memory resources
Set objWord = Nothing



Simon
 
Thanks for the clarification. Since most of my experience is with ADO databases, I'm a novice with the Word Object Model.

I do have one final question: Since I'm already in a new document (rtfText - which is a child form of my main MDI form and is displayed when the application is activated), how do I apply this line of code?:

Set objDoc = objWord.Documents.Open("C:\aa\test.doc")

Thanks,
-Bob
 
Thanks for the clarification. Since most of my experience is with ADO databases, I'm a novice with the Word Object Model.

I do have one final question: Since I'm already in a new document (rtfText - which is a child form of my main MDI form and is displayed when the application is activated), how do I apply this line of code?:

Set objDoc = objWord.Documents.Open("C:\aa\test.doc")

Thanks,
-Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top