Consulting

Results 1 to 5 of 5

Thread: Macro needed to collect style and formatting information

  1. #1
    VBAX Newbie Digga's Avatar
    Joined
    Feb 2005
    Posts
    2
    Location

    Macro needed to collect style and formatting information

    Hi All

    I have searched EE, MVP sites and the Word groups on MS and cannot find what I'm after.

    I'm trying to find/write a macro to do the following:

    1. Capture all styles and formatting used in a document and not the builtin or available styles, this should include all storyranges.
    2. Create a new document and insert the styles used in the source document along with an example of the style and it's settings.
    3 . Save the file as sourceDocumentNameStyleList.doc

    Hopefully a Wordvba guru can help me out here :-) I'm ok at VBA, but not too familiar with the Word object model.

    Thanks guy 'n' gals

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    This can be done.

    Here is code that will list, in a new doc, the styles in a document that are NOT built-in styles.

    This would have to be expanded to also run a check for each paragraph to see if it uses a built-in style, or not.

    There is a possible problem of course. If, like most users, the document has those "empty" paragraphs, when people use an "extra" Enter key to make space between paragraphs - because they do not know how to properly use Styles, each one of those "empty paragraphs will beparsed and checked as well.

    So you could add a string check to see if the Range.Text of each checked paragrpah is "".

    Can you take it from here, or do you need help with this?

    [vba]Sub ShowStyles()
    Dim aStyle As Style
    Dim i As Integer
    Dim ThisDoc As Document
    Dim ThatDoc As Document

    Set ThisDoc = ActiveDocument
    Application.Documents.Add
    Set ThatDoc = ActiveDocument
    ThisDoc.Activate
    For Each aStyle In ThisDoc.Styles
    If aStyle.BuiltIn <> True Then
    i = i + 1
    ThatDoc.Activate
    With Selection
    .TypeText Text:="SampleText of the Style named: " & aStyle.NameLocal
    .Style = aStyle
    .TypeParagraph
    End With
    ThisDoc.Activate
    End If
    Next
    ThatDoc.Activate
    Selection.HomeKey unit:=wdStory
    Selection.TypeText Text:="There are " & i & _
    " Styles that are NOT built-in."
    Selection.TypeParagraph
    Selection.TypeParagraph
    ThisDoc.Activate
    Set ThisDoc = Nothing
    Set ThatDoc = Nothing
    End Sub[/vba]

  3. #3
    VBAX Newbie Digga's Avatar
    Joined
    Feb 2005
    Posts
    2
    Location
    Hi Gerry

    Thanks for the code sample, bloddy superb I cannot thank you enough.

    I picked this up this morning and after a couple of hours of 'fiddling' and testing with the users, I've settled on this functionality below. One change from the spec I posted, was to use all used styles in the document, not just the notBuiltIn - if you get what I mean. Also the descriptive text at the top now carries the wdStyleNormal format rather than picking up the format of the first style transferred, and the transferred style description is set to wdStyleNormal too.

    Again, thank you for your help, this would of taken me a lot lot longer to sort out.

    Oh yes one more thing, public thanks goes to Dreamboat, who knew of my predicament and invited me to join this forum. (You need a bunch of flowers emoticon sweetie)

    [VBA] Sub ListStyles()
    Dim aStyle As Style
    Dim i As Integer
    Dim sourceDoc As Document
    Dim sourceDocName As String
    Dim targetDoc As Document
    Dim intPos As Integer
    Dim sourceDocPath As String

    Application.ScreenUpdating = False

    Set sourceDoc = ActiveDocument
    sourceDocPath = ActiveDocument.Path
    sourceDocName = ActiveDocument.Name
    intPos = InStrRev(sourceDocName, ".")
    sourceDocName = sourceDoc.Name
    Application.Documents.Add

    Set targetDoc = ActiveDocument
    sourceDoc.Activate

    For Each aStyle In sourceDoc.Styles

    If aStyle.InUse = True Then

    i = i + 1
    targetDoc.Activate

    With Selection
    .TypeText Text:="SampleText of the Style named: " & aStyle.NameLocal
    .Style = aStyle
    .TypeParagraph
    .TypeText Text:="Description: " & aStyle.Description
    .Style = wdStyleNormal
    .TypeParagraph
    .TypeParagraph
    End With

    sourceDoc.Activate

    End If

    Next

    targetDoc.Activate
    Selection.HomeKey unit:=wdStory
    Selection.TypeText Text:="There are " & i & " Styles used in " & sourceDocName & "."
    Selection.TypeParagraph
    Selection.TypeParagraph
    Selection.HomeKey unit:=wdStory
    Selection.MoveDown unit:=wdParagraph, Count:=2, Extend:=wdExtend
    Selection.Style = wdStyleNormal

    sourceDocName = Left(sourceDocName, intPos - 1)
    sourceDocName = sourceDocName & "_StyleList.doc"
    Application.ScreenUpdating = True

    sourceDocName = sourceDocPath & "\" & sourceDocName

    ActiveDocument.SaveAs FileName:=sourceDocName, FileFormat:=wdFormatText

    Selection.HomeKey unit:=wdStory
    targetDoc.Activate

    Set sourceDoc = Nothing
    Set targetDoc = Nothing

    End Sub [/VBA]

  4. #4
    VBAX Regular
    Joined
    Aug 2004
    Location
    On a 100 acre hobby farm in beautiful west Quebec.
    Posts
    87
    Location
    This came in handy for a document I've been working on. However, since I use a lot of character styles, I modified Digga's code a bit to avoid carrying the character formatting:
    [VBA] With Selection
    .Style = aStyle
    .TypeText Text:="SampleText of the Style named: " & aStyle.NameLocal
    .TypeParagraph
    .Font.Reset
    .Style = wdStyleNormal
    .TypeText Text:="Description: " & aStyle.Description
    .ParagraphFormat.LeftIndent = 36
    .ParagraphFormat.SpaceAfter = 24
    .TypeParagraph
    .Style = wdStyleNormal
    End With
    [/VBA]
    This sets the style first (to ensure the sample text is set properly if the style is character instead of paragraph), then resets the font before adding the description. (I indented the description to differentiate it a bit more and used SpaceAfter instead of an extra paragraph.) Finally, by setting the style to Normal at the end, the next style sample won't inherit the paragraph formatting if it happens to be a character style.
    I assume something similar might be needed to deal with table styles.
    Eric

    Experience is not what happens to a man; it is what a man does with what happens to him. ? Aldous Huxley

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, you can add Table styles (are they not great? Love 'em) to the compilation.

Posting Permissions

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