WD: .Paragraphformat

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

WD: .Paragraphformat

Post by Rubberduckone »

Hi,

(Win7, WD 2010 UK)

Trying to create a sub to write a number of settings that I need to document styles used in number of templates. However .paragraphformat is causing me problems and I can't get it to work - despite various approaches. Suggestions are very welcome...

Public Sub Styles_Describe()
Dim oStyle As Style
Dim oStyles As Styles
Dim strStyle As String
Dim i

strStyle = ""

For i = 1 To ActiveDocument.Styles.Count
'Debug.Print ActiveDocument.Styles.Count

Set oStyle = ActiveDocument.Styles(i)

With oStyle

If .InUse Then
strStyle = strStyle & "Style name: " & .NameLocal & ","
strStyle = strStyle & "Description: " & .Description & ","
strStyle = strStyle & "Basestyle: " & .BaseStyle & ","
strStyle = strStyle & "BuiltIn: " & .BuiltIn & ","
strStyle = strStyle & "Type: " & .Type
'Debug.Print .NoSpaceBetweenParagraphsOfSameStyle

With .Font
strStyle = strStyle & "Font name: " & .Name & ","
strStyle = strStyle & "Font size: " & .Size & ","
strStyle = strStyle & "Font bold: " & .Bold & ","
strStyle = strStyle & "Font color: " & .Color & ","
strStyle = strStyle & "Font colorindex: " & .ColorIndex & ","
strStyle = strStyle & "Font kerning: " & .Kerning & ","
strStyle = strStyle & "Font spacing: " & .Spacing & ","
End With


'Debug.Print oStyle.NameLocal
'Debug.Print strStyle

IT FAILS HERE
With ActiveDocument.Styles(i).ParagraphFormat
strStyle = strStyle & "Outline level: " & .OutlineLevel & ","
strStyle = strStyle & "Alignment: " & .Alignment & ","
strStyle = strStyle & "LeftIndent: " & .LeftIndent & ","
strStyle = strStyle & "RightIndent: " & .RightIndent & ","
strStyle = strStyle & "FirstLineIndent: " & .FirstLineIndent & ","

strStyle = strStyle & "LineUnitBefore: " & .LineUnitBefore & ","
strStyle = strStyle & "LineUnitAfter: " & .LineUnitAfter & ","
strStyle = strStyle & "LineSpacing: " & .LineSpacing & ","
strStyle = strStyle & "SpaceBefore: " & .SpaceBefore & ","
strStyle = strStyle & "SpaceAfter: " & .SpaceAfter & ","
strStyle = strStyle & "KeepTogether: " & .KeepTogether & ","
strStyle = strStyle & "KeepWithNext: " & .KeepWithNext & ","
strStyle = strStyle & "PageBreakBefore: " & .PageBreakBefore & ","
strStyle = strStyle & "Style: " & .Style & ","

'Debug.Print strStyle
End With

With Selection
.EndOf Unit:=wdStory, Extend:=wdMove
.Text = "- - - - - - - - NEXT STYLE - - - - - - - - -"
.Text = strStyle
.Text = " "
End With
End If

End With

Next i

End Sub
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

ParagraphFormat is only available if the style is a paragraph style.
.Style makes Word crash, and there were some minor other problems.

Here is a lightly tested working version:

Code: Select all

Public Sub Styles_Describe()
    Dim oStyle As Style
    Dim oStyles As Styles
    Dim strStyle As String
    Dim i As Long

    For i = 1 To ActiveDocument.Styles.Count
        'Debug.Print ActiveDocument.Styles.Count
        Set oStyle = ActiveDocument.Styles(i)
        With oStyle
            If .InUse Then
                strStyle = "Style name: " & .NameLocal & ", "
                strStyle = strStyle & "Description: " & .Description & ", "
                strStyle = strStyle & "Basestyle: " & .BaseStyle & ", "
                strStyle = strStyle & "BuiltIn: " & .BuiltIn & ", "
                strStyle = strStyle & "Type: " & .Type & ", "

                With .Font
                    strStyle = strStyle & "Font name: " & .Name & ", "
                    strStyle = strStyle & "Font size: " & .Size & ", "
                    strStyle = strStyle & "Font bold: " & .Bold & ", "
                    strStyle = strStyle & "Font color: " & .Color & ", "
                    strStyle = strStyle & "Font colorindex: " & .ColorIndex & ", "
                    strStyle = strStyle & "Font kerning: " & .Kerning & ", "
                    strStyle = strStyle & "Font spacing: " & .Spacing
                End With

                If .Type = wdStyleTypeParagraph Then
                    With .ParagraphFormat
                        strStyle = strStyle & ", Outline level: " & .OutlineLevel & ", "
                        strStyle = strStyle & "Alignment: " & .Alignment & ", "
                        strStyle = strStyle & "LeftIndent: " & .LeftIndent & ", "
                        strStyle = strStyle & "RightIndent: " & .RightIndent & ", "
                        strStyle = strStyle & "FirstLineIndent: " & .FirstLineIndent & ", "
                        strStyle = strStyle & "LineUnitBefore: " & .LineUnitBefore & ", "
                        strStyle = strStyle & "LineUnitAfter: " & .LineUnitAfter & ", "
                        strStyle = strStyle & "LineSpacing: " & .LineSpacing & ", "
                        strStyle = strStyle & "SpaceBefore: " & .SpaceBefore & ", "
                        strStyle = strStyle & "SpaceAfter: " & .SpaceAfter & ", "
                        strStyle = strStyle & "KeepTogether: " & .KeepTogether & ", "
                        strStyle = strStyle & "KeepWithNext: " & .KeepWithNext & ", "
                        strStyle = strStyle & "PageBreakBefore: " & .PageBreakBefore
                    End With
                End If

                With ActiveDocument.Content
                    .InsertParagraphAfter
                    .InsertAfter "- - - - - - - - NEXT STYLE - - - - - - - - -"
                    .InsertParagraphAfter
                    .InsertAfter strStyle
                    .InsertParagraphAfter
                End With
            End If
        End With
    Next i
End Sub
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Hi Hans,

Thanks for the adjustment. Just what I needed.

Kind of interesting that MS releases a version where .Style are able to crash Word entirely. Not the most elegant type of error handling.... :hairout:
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Hi Hans,

An additional question...

Looking at the .Description and .SpaceAfter properties.

In .Description there's the "Don't add space between paragraphs of the same style setting". In the documentation I've been trying to find which property this belongs to or whether it's a property of its own, but I cannot find it. Would you know which property it belongs to or how I can retrieve that particular setting???

Thanks,
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

The Style object has a property NoSpaceBetweenParagraphsOfSameStyle of type Boolean (True/False):
S254.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

:blush: :blush: :blush: Thanks - don't know why I didn't see that one at first - more :blush: :blush:
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Hi Hans,

In the code I also have something about 'font colours' as I'm using a lot of custom colours.


With .Font
...
strStyle = strStyle & "Font color: " & .Color & ","
strStyle = strStyle & "Font colorindex: " & .ColorIndex & ","
...
End With


The above returns '24-bit windows colours', which is mambo-jambo to me (eg. -738131969, 0,-16777216,-587137025)
In other contexts I can find RGB or HEX conversion functions, but for these 'Win24bit' colours I can't find anything.

Do you know of an easy way to convert those to RGB or HEX??
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

Color returns a 24-bit color number, you can use the Hex function to convert it to hexadecimal:

strStyle = strStyle & "Font color: " & Hex(.Color) & ","

ColorIndex returns one of the wdColorIndex constants; you'd have to create code to translate this to text. Near the beginning of your macro:

Code: Select all

    Dim strColors()
        strColors = Array("Auto or Other", "Black", "Blue", "Turqoise", "Bright Green", _
        "Pink", "Red", "Yellow", "White", "Dark Blue", "Teal", "Green", _
        "Violet", "Dark Red", "Dark Yellow", "Gray 50", "Gray 25")
And where you display the color index:

strStyle = strStyle & "Font colorindex: " & strColors(.ColorIndex) & ","
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Thanks Hans,

I've tried to implement this, but are a bit puzzled by the results. The hex(.Color) sometimes returns an 8-characters alpha numeric code and sometimes just a 6-characters alpha numeric code.

When it's 8 it seems 'always' to be FF in front (I guess that's the 'Alpha'?? - assumed to be what, if nothing is stated??)

Then I've been looking for a conversion function from Hex2RGB, but can't seem to find anything native in Word. So I've been googling a bit trying to find alternatives and came across this suggestion going directly from the Win-color to RGB:

Function fcnLongToRGB(ByRef lngColor As Long) As String
Dim lngRed As Long, lngGreen As Long, lngBlue As Long
lngRed = lngColor Mod 256
lngGreen = (lngColor \ 256) Mod 256
lngBlue = (lngColor \ 256 \ 256) Mod 256
fcnLongToRGB = "RGB(" & lngRed & "," & lngGreen & "," & lngBlue & ")"
End Function

(by gregmaxey - http://www.msofficeforums.com/word-vba/ ... s-vba.html)

At first glance it looked as if it would do the trick, but unfortunately when testing a style that has RGB(144,144,144) which by Word is returned in .color as 947419 - the function returned RGB(219,116,14) which is way off.

Doesn't Word by itself have any color conversion functions or Windows for that matter, that can be called directly from within Word??

:scratch:
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

The 8-character color codes are not RGB colors but system colors such as the "button face" color and the "tooltip" color. They are represented by negative numbers, and their hexadecimal code starts with FF. You cannot use Greg Maxey's function for such colors.

The value of RGB(144,144,144) is not 947419 but 9474192, and this is correctly translated back to RGB(144,144,144) by Greg Maxey's function:
S0581.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Hmmm...

Can't get my head around this one. :bash:

Inputs when the style is modified are either RGB or HSL. Output for the style as .color is a number, not related to the other two and as far as I can tell Word has no functions that can do a seamless conversion.

I need somehow to be able to transform the .color number into an RGB code and was hoping Greg's function could do the trick - guess not.

Do you have an alternative suggestion that can do the job??
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

I don't see the problem...
S0584.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Just want to make sure we have the same understanding here :smile:

If I understand your last response correct, you...
1. check a black value ('Initial')
2. assign a new RGB value
3. check that ('New color')
4. assign a third color
5. check that ('Final')


My task is slightly different:
1. I have an existing template, with styles in colors
2. I would like to check that the coloring used is correct (comparing RGB values)
3. Code returns 'Initial color' in Selection.font.color format... and if I understood your previous responses correct, I could not rely on Greg's function to return the correct RGB at this stage??

...so initially I do not need to change the colors in any way. I just need to be able to retrieve a list of styles with reliable RGB values.
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WD: .Paragraphformat

Post by HansV »

If a "real" RGB color has been assigned to the text or style, you can retrieve it using .Font.Color, and use Greg Maxey's function to correctly display its RGB value.

But Word 2007 and later also use color themes, with a set of theme colors. Theme colors are coded differently. The disadvantage is that you can't read the RGB values directly, the advantage is that if you choose a different color theme, the theme colors in your document will be updated automatically.
S255.jpg
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

I'm not using the theme colors at all. :clapping:

So I think I'll give .Font.Color and Greg a shot. Thanks for the insights - highly appreciated :thankyou:
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: WD: .Paragraphformat

Post by Rudi »

If you are going to shoot Greg, use the the paint ball with color RGB(255,0,0). It gives a very realistic look!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Rubberduckone
Lounger
Posts: 34
Joined: 28 Oct 2011, 09:05

Re: WD: .Paragraphformat

Post by Rubberduckone »

Don't you think 255,0,0 is a bit to much 'Ketchup', maybe the red should be toned down a little 180-200.. :laugh: :blackhole:
Thanks and best regards
RD

A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: WD: .Paragraphformat

Post by Rudi »

:laugh: Agreed....
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.