سؤال

I have been playing with VBA code that permits automatic creation of word documents. In the example below, I write a Word paragraph 6 times. In order to do certain formatting (bullets, numbering, putting text in tables etc...) it appears necessary to make a second pass and apply formatting after text is created. Can this be done in a single pass like VBA allows us to do with bold or italics?

Example: - toggle numbering - write line

Sub Sample()
    Dim oWordApp As Object, oWordDoc As Object
    Dim i As Integer


    '~~> Establish an Word application object
    On Error Resume Next
    Set oWordApp = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        Set oWordApp = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0

    oWordApp.Visible = True

    Set oWordDoc = oWordApp.Documents.Add

    With oWordDoc
        For i = 0 To 5
            .Content.InsertAfter ("Paragraph " & i)
            .Content.InsertParagraphAfter
        Next

        ' Yields execution so that the operating system can process other events.
        DoEvents

        ' why does this have to be done after creating text? 
        .Paragraphs(1).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

        .Paragraphs(2).Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

    End With

    Set oWordApp = Nothing
    Set oWordDoc = Nothing
End Sub

example from Excel VBA for creating numbered list in Word

هل كانت مفيدة؟

المحلول

You can obviously do it. There are few possibilities but I believe the below solution give you a clue of how to use Document.Range(start,end) property.

 With oWordDoc
        For i = 0 To 5
            .Content.InsertAfter ("Paragraph " & i)
            .Content.InsertParagraphAfter
        Next

        ' Yields execution so that the operating system can process other events.
        DoEvents

 'apply numbering for 6 added paragraphs
   .Range(.Paragraphs(1).Range.Start, _
                .Paragraphs(6).Range.End) _
        .ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

'... the rest of your code here

نصائح أخرى

I had some trouble adding bullets, so here is the code that worked for me.

Note that this requires adding a reference to Microsoft Word 15.0 Object Library

Sub Generate()
Dim objWord
Dim objDoc
Dim temp3 As Word.ListTemplate
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection

'Change your text here:
objSelection.TypeText ("This is my text in Word Document using Excel")
objSelection.TypeParagraph
objSelection.TypeText ("Here is more text")
'--------------------------

Set temp3 = objWord.ListGalleries(wdNumberGallery).ListTemplates(1)
With temp3.ListLevels(1)
    .Font.Name = "Symbol"
    .Font.Size = 11
    .NumberFormat = ChrW(61623)
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = wdListNumberStyleArabic
    .NumberPosition = objWord.CentimetersToPoints(0.63)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = objWord.CentimetersToPoints(1.27)
    .TabPosition = wdUndefined
    .StartAt = 1
End With

'Apply formatting to our range
objDoc.Range.ListFormat.ApplyListTemplate ListTemplate:=temp3
End Sub

This will do it...

    Dim wdApp As Word.Application
    Set wdApp = New Word.Application


    With wdApp
        .Visible = True
        .Activate
        .Documents.Add
        ' turn on bullets
        .ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
        .Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdBulletGallery).ListTemplates(1), _
            continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .Font.Bold = False
            .Font.Name = "Century Gothic"
            .Font.Size = 12
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")
            .TypeParagraph
        End With

        ' turn off bullets
        .Selection.Range.ListFormat.RemoveNumbers wdBulletGallery

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")
            .TypeParagraph

        End With

        ' turn on outline numbers
        .ListGalleries(wdOutlineNumberGallery).ListTemplates(1).Name = ""
        .Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=.ListGalleries(wdOutlineNumberGallery).ListTemplates(1), _
            continuepreviouslist:=False, applyto:=wdListApplyToWholeList, defaultlistbehavior:=wdWord9ListBehavior

        With .Selection
            .ParagraphFormat.Alignment = wdAlignParagraphLeft
            .TypeText ("some details")
            .TypeParagraph
            .TypeText ("some details")

        End With

    End With
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top