Results 1 to 15 of 15
  1. #1
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Macro to read custom style settings

    I’m writing a macro to list various settings for my custom styles in Word 2010. I’m having trouble reading some of these settings – in this case, left and right margin.

    For example, if I put the code below (from a Microsoft website) in a “for every style in activedocument.styles” loop then it returns “0 cm” for a style which has a margin set to 0.5 cm.

    Sub ShowRightIndent()
    Dim dlgParagraph As Dialog
    Set dlgParagraph = Dialogs(wdDialogFormatParagraph)
    MsgBox "Right indent = " & dlgParagraph.RightIndent
    End Sub

    It looks to me like the setting in the paragraph dialog can only be read for a selected paragraph, even though one can manually look up the settings for a style. This makes no sense to me, so I figure that I’m missing some vba here.

    Can anyone help?
    Stylus

  2. #2
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    237
    Thanks
    3
    Thanked 24 Times in 24 Posts
    Hi Stylus,

    I got the same results when running your macro, but the macro is checking the indent, not the margins. If I indent the right margin by .5, the macro indicates .5 correctly.

  3. #3
    3 Star Lounger
    Join Date
    Apr 2012
    Posts
    237
    Thanks
    3
    Thanked 24 Times in 24 Posts
    Try this for checking your margins.

    Sub ShowRightMargin()

    Dim sngMargin As Single

    sngMargin = Selection.Sections(1).PageSetup.RightMargin
    MsgBox PointsToInches(sngMargin) & " inches"

    End Sub

  4. #4
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for checking...

    About margin vs indent: I MEANT indent. D'oh! Must eat more d'ohnuts...

    Your result means that my surrounding code is off, because I've just run another test and I absolutely get a 0 for both left and right indent even when they are set otherwise.
    Stylus

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Try:
    Code:
    Sub Test()
    Dim StrFormat As String
    With Selection.Paragraphs(1).Range.ParagraphFormat
      StrFormat = "First Line Indent: " & PointsToInches(.FirstLineIndent) & vbCr
      StrFormat = StrFormat & "Right Indent: " & PointsToInches(.RightIndent) & vbCr
      StrFormat = StrFormat & "Left Indent: " & PointsToInches(.LeftIndent) & vbCr
      StrFormat = StrFormat & "Space After: " & PointsToInches(.SpaceAfter) & vbCr
      StrFormat = StrFormat & "SpaceBefore: " & PointsToInches(.SpaceBefore) & vbCr
      StrFormat = StrFormat & "Alignment: " & .Alignment & vbCr
      StrFormat = StrFormat & "Style: " & .Style
    End With
    MsgBox StrFormat
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks macropod,

    I tested your code by placing it in a loop with "For Each stlTestStyle In ActiveDocument.Styles".

    Your code only reads the formatting that applies to the paragraph where the cursor is located. The code displays that single paragraph's formatting, over and over again, for as many times as my test structure performed the loop. In other words, I am receiving the SELECTION'S formatting.

    My experience, so far, is that I can get the formatting of a selection, but can't figure out how to get it for a style. (And I still can't replicate BigMac56's results - when I run that code I get "0" regardless of the settings.)

    What I want all this for is to print out certain formatting settings for each custom style, regardless of whether that style is in use or not. I am checking styles, not a document.

    I suspect that reading the dialog box will be the way to find the settings for styles per se, as opposed to styles in a selected paragraph. Now if I can just figure out how to get it to work...
    Stylus

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Without seeing your code, I can't comment on why you're not getting the right results, but you definitely should not have any Selection references in there.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #8
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Macropod, your code has "With Selection.Paragraphs(1).Range.ParagraphFormat". My understanding is that selection returns info about a selected part of text or the cursor location.

    My test code is:

    Sub Test()
    Dim StrFormat As String
    Dim stlTestStyle As Style
    Dim i As Integer

    i = 0
    For Each stlTestStyle In ActiveDocument.Styles
    i = i + 1
    If i > 10 Then 'I add this in the test to prevent the macro from going through all 350+ styles

    Exit Sub
    End If

    With Selection.Paragraphs(1).Range.ParagraphFormat
    StrFormat = "First Line Indent: " & PointsToCentimeters(.FirstLineIndent) & vbCr
    StrFormat = StrFormat & "Right Indent: " & PointsToCentimeters(.RightIndent) & vbCr
    StrFormat = StrFormat & "Left Indent: " & PointsToCentimeters(.LeftIndent) & vbCr
    StrFormat = StrFormat & "Space After: " & PointsToCentimeters(.SpaceAfter) & vbCr
    StrFormat = StrFormat & "SpaceBefore: " & PointsToCentimeters(.SpaceBefore) & vbCr
    StrFormat = StrFormat & "Alignment: " & .Alignment & vbCr
    StrFormat = StrFormat & "Style: " & .Style
    End With
    MsgBox StrFormat
    Next stlTestStyle
    End Sub
    Stylus

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    hi Stylus,

    looping through the Styles collection will return the info about every Style, not just your custom Styles. Still, if that's what you want, try:
    Code:
    Sub Demo()
    Dim oSty As Style, StrSty As String
    With ActiveDocument
      For Each oSty In .Styles
        With oSty
          StrSty = ""
          If .Type = wdStyleTypeParagraph Then
            With .ParagraphFormat
              StrSty = "Style: " & ActiveDocument.Styles(oSty).NameLocal & vbTab
              StrSty = StrSty & StrSty & "Alignment: " & .Alignment & Chr(11)
              StrSty = StrSty & "First Line Indent: " & PointsToCentimeters(.FirstLineIndent) & Chr(11)
              StrSty = StrSty & "Right Indent: " & PointsToCentimeters(.RightIndent) & Chr(11)
              StrSty = StrSty & "Left Indent: " & PointsToCentimeters(.LeftIndent) & Chr(11)
              StrSty = StrSty & "Space After: " & PointsToCentimeters(.SpaceAfter) & Chr(11)
              StrSty = StrSty & "SpaceBefore: " & PointsToCentimeters(.SpaceBefore) & vbCr
            End With
          End If
        End With
        .Range.InsertAfter StrSty
      Next
    End With
    End Sub
    Last edited by macropod; 2012-07-09 at 07:36.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. The Following User Says Thank You to macropod For This Useful Post:

    Stylus (2012-07-01)

  11. #10
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I just noticed that your name is Paul, so henceforth

    Paul:

    Thanks for the code. That cracked it! I have things working nicely now. The loop I posted was just to show the principle. In actual fact i include all manner of tests to give me the choice of printing out all styles, custom styles only, my custom styles only, styles in use only, etc.
    Stylus

  12. #11
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    121
    Thanks
    3
    Thanked 20 Times in 18 Posts
    Great example of how to use a collection macropod! You menioned that it will process every style, but is there a way to detect custom (or "in use") styles within the collection? I like to have a record of style settings for templates, but the "print styles" includes unused styles. Your code would be a great start on having a tool to do just that.

  13. #12
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Hi Eric,

    You can test the 'in use' state of a Style, but Word tends to over-report. An alternative approach would be to do a 'Find' for a paragraph using the Style. As for 'custom' Styles, you can also test against the BuiltIn property, so that only user-created Styles are reported. There is no way of detecting whether a BuiltIn Style has been modified, though.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  14. #13
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Eric,

    Filtering for BuiltIn will limit the scope of the macro to your custom styles. On the net I found a good example of determining an "actual" in-use status on Greg Maxey's site: http://gregmaxey.mvps.org/word_tip_p...ort_addin.html. There are other examples out there...

    Paul,

    Now I'm chewing on the tab settings for the styles... It looks like they have to be enumerated to be read. Is there a clever way of returning the total number of tabs set in a given style? Knowing that would save me testing my way through the tab(s).
    Stylus

  15. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Simply getting the total number of tabs isn't going to achieve much. For meaningful results you could use something like:
    Code:
    Sub Demo()
    Dim oSty As Style, StrSty As String, TbStp As TabStop, SngTab As Single
    With ActiveDocument
      For Each oSty In .Styles
        With oSty
          StrSty = ""
          If .BuiltIn = False Then
            If .Type = wdStyleTypeParagraph Then
              With .ParagraphFormat
                StrSty = "Style: " & ActiveDocument.Styles(oSty).NameLocal & vbTab
                StrSty = StrSty & StrSty & "Alignment: " & .Alignment
                StrSty = StrSty & Chr(11) & "First Line Indent: " & PointsToCentimeters(.FirstLineIndent)
                StrSty = StrSty & Chr(11) & "Right Indent: " & PointsToCentimeters(.RightIndent)
                StrSty = StrSty & Chr(11) & "Left Indent: " & PointsToCentimeters(.LeftIndent)
                StrSty = StrSty & Chr(11) & "Space After: " & PointsToCentimeters(.SpaceAfter)
                StrSty = StrSty & Chr(11) & "Space Before: " & PointsToCentimeters(.SpaceBefore)
                For Each TbStp In .TabStops
                  StrSty = StrSty & Chr(11) & "TabStop Alignment: "
                  Select Case TbStp.Alignment
                    Case 0: StrSty = StrSty & "Left"
                    Case 1: StrSty = StrSty & "Center"
                    Case 2: StrSty = StrSty & "Right"
                    Case 3: StrSty = StrSty & "Decimal"
                    Case 4: StrSty = StrSty & "Bar"
                    Case 6: StrSty = StrSty & "List"
                  End Select
                  StrSty = StrSty & " @ " & PointsToCentimeters(TbStp.Position)
                Next
                StrSty = StrSty & vbCr
              End With
            End If
          End If
        End With
        .Range.InsertAfter StrSty
      Next
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  16. The Following User Says Thank You to macropod For This Useful Post:

    Stylus (2012-09-07)

  17. #15
    Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    86
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Paul

    I was occupied with another project, and neglected to thank you for this code!
    Stylus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •