How manipulate a style list in word with VBA Excel?

Colmillo

New Member
Joined
Sep 7, 2016
Messages
3
(Hi everyone!) English is not my mother language, so forgive my writing errors and be my guest on a correction, will be apreciated. Let me introduce you in my issue. I'm having some troubles manipulating style list in word from vba in excel. I have a word template, an excel with data and this code (or at least part of it):
Rich (BB code):
Rich (BB code):
Rich (BB code):
<code style="margin: **** padding: **** border: **** font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">Function WriteParagraphWord(wdApp As Word.Application, ByVal myWorksheet As Worksheet, ByVal myParagraph As Integer) As Boolean
    Dim mySelection As selection
    Set mySelection = wdApp.selection

    WriteParagraphWord = True

    Dim colStyle As Integer
    colStyle = FindColumn(myWorksheet, "Style")

    mySelection.Paragraphs.Style = CStr(myWorksheet.Cells(myParagraph, colStyle).Value)


    Dim colSpaceAfter As Integer
    colSpaceAfter = FindColumn(myWorksheet, "SpaceAfter")

    If myWorksheet.Cells(myParagraph, colSpaceAfter).Value <> "default" Then
    mySelection.ParagraphFormat.SpaceAfter = myWorksheet.Cells(myParagraph, colSpaceAfter).Value
    End If

    Dim colBold As Integer
    colBold = FindColumn(myWorksheet, "Bold")

    If myWorksheet.Cells(myParagraph, colBold).Value = "All" Then
    mySelection.Font.Bold = True
    End If

    Dim colList As Integer
    colList = FindColumn(myWorksheet, "List")

    Dim tipoList As String
    typeList = myWorksheet.Cells(myParagraph, colList).Value

    Select Case typeList
    Case "dot"
       Set listDot = wdApp.ActiveDocument.ListTemplates(7)
       mySelection.Range.ListFormat.ApplyListTemplate ListTemplate:=listDot
       Dim colNiveList As Integer
       colLvlList = FindColumn(myWorksheet, "LvlList")
       Dim lvlList As Integer
       lvlList = CInt(myWorksheet.Cells(myParagraph, colLvlList).Value)
       mySelection.Range.ListFormat.ListIndent '<=== PROBLEM IS HERE AND ONWARD (Other tries)

'      If lvlList = 1 Then mySelection.Range.ListFormat.ListIndent
'        If lvlList = 2 Then
'            mySelection.Range.ListFormat.ListIndent
'            mySelection.Range.ListFormat.ListIndent
'        End If
'        Dim nLvl As Integer
'        For nLvl = 0 To lvlList
'            mySelection.Range.ListFormat.ListIndent
'        Next nLvl

    End Select

    Dim colTipo As Integer
    colTipo = FindColumn(myWorksheet, "Tipo")

    Select Case myWorksheet.Cells(myParagraph, colTipo).Value
    Case "Image"
    WriteParagraphWord = WriteImage(mySelection, myWorksheet, myParagraph)
    Case "Text"
    WriteParagraphWord = WriteText(mySelection, myWorksheet, myParagraph)
    Case "Equation"
    WriteParagraphWord = WriteEcuacion(mySelection, myWorksheet, myParagraph)
    Case "Table"
    WriteParagraphWord = WriteTable(mySelection, myWorksheet, myParagraph)
    End Select

    If myWorksheet.Cells(myParagraph, colBold).Value = "All" Then
    mySelection.Font.Bold = False
    End If

    mySelection.TypeParagraph

    'If typeList <> "default" Then mySelection.Range.ListFormat.ListOutdent
End Function</code>
In the template the style list has a dot for level 1 no tabulation, circle for level 2 w/tabulation of 0.5cm, square for level 3 w/tabulation of 1cm, and so on.
After applied the ListIndent... first item sometimes keep his indent, but always has a dot. The others items are fine, a circle and tabulation. If I tried to indent twice, the first item keep the dot, but the indent is fine, but the others items has level 3, not 2, with square (not the circle was expected). And some times it make some abnormal format like put numbers to second and onward with no tabulation.
Can anyone tell me what i'm doing wrong?
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Instead of trying to get Word to change the formatting by applying different list indents, you'd do better to define some appropriate paragraph Styles in a Word document template with the appropriate formatting, then create your document from that template and use those Styles.

PS: When posting code, please use the code tags, inserted via the # button on the posting menu, to encompass your code and thereby maintain its formatting.
 
Last edited:
Upvote 0
Instead of trying to get Word to change the formatting by applying different list indents, you'd do better to define some appropriate paragraph Styles in a Word document template with the appropriate formatting, then create your document from that template and use those Styles.

PS: When posting code, please use the code tags, inserted via the # button on the posting menu, to encompass your code and thereby maintain its formatting.
Thanks for the answer, sorry for the code tags. I'm using a template word, but some lists in the document need more indent that others, the style list is defined and using the indent in manual way, works perfect. Maybe I need define another style list. Thanks again!
 
Upvote 0
I'm using a template word, but some lists in the document need more indent that others, the style list is defined and using the indent in manual way, works perfect. Maybe I need define another style list.
What you should be doing is defining Styles for all the indents you might need and adding them to the template. Whether you employ all those Styles in a given document is inconsequential.
 
Upvote 0

Forum statistics

Threads
1,215,400
Messages
6,124,702
Members
449,180
Latest member
craigus51286

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top