1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Print Format Coding

Discussion in 'Business Applications' started by hcadar1, Jan 19, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. hcadar1

    hcadar1 Thread Starter

    Joined:
    Jan 19, 2006
    Messages:
    1
    Is there a way way to print the formatting style in word 2003.

    I have a template with several different styles defined and I want to print the font name, size, and other formatting settings for the style. Any ideas?
     
  2. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    Hey there, welcome to TSG (y)

    I'm a fool, so I'm going to say it's possible using VBA.

    That's entirely speculation.

    This is my assumption: within the Word Object Model, there is probably a Styles collection. Styles by definition have properties common to the Object Model (font name, weight, tab formatting, paragraph formatting, etc.), so if you built a list of style names, you could cycle through that list and generate a document/file/whatever you want listing the assignments of each property.

    I'd have to play to figure out the actual code.

    Are you comfortable with using VBA (or macros)?

    chris.
     
  3. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    [bump]

    Imma throw out some airware, just for giggles.

    Code:
    Private Sub OutputProperties(ByVal strStyleName As String)
      Dim sty As Style
      Dim prp As Property
    
      Set sty = ActiveDocument.Styles(strStyleName)
    
      For Each prp In sty.Properties
        With prp
          Debug.Print .Name
          Debug.Print .Description
        End With
      Next prp
    End Sub
    
    Public Sub ListMyStyleProperties()
      Dim strStyles()  As String
      Dim i            As Integer
    
      strStyles = Split("Some Style, _
                        styleName1, _
                        Random Style, _
                        Custom List 3, _
                        Other Style Names", _
                        ", ")
    
      For i = 0 To UBound(strStyles)
        OutputProperties strStyles(i)
      Next i
    End Sub
    
    Again, that's airware. But the only thing I'm uncertain about is the Properties property of a style.

    chris.
     
  4. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    [bump 2]

    Apparently Word doesn't have a Property object.

    Also, styles are cummulative, in that a style is usually based on another style, with the root being Normal. So I'm trying some recursive structure to get an adequate description of what the style actually uses.

    The following code:

    Code:
    Public Sub TestStuff(ByVal strStyleName As String)
      Dim sty As Style
      Static i As Integer
    
      With ActiveDocument.Styles(strStyleName)
        Debug.Print String(i, Chr$(9)) & "Style Name: " & .NameLocal
        Debug.Print String(i, Chr$(9)) & "Description: " & .Description
        If Len(.BaseStyle & vbNullString) > 0 Then
          i = i + 1
          Debug.Print String(i, Chr$(9)) & "Based on:"
          TestStuff .BaseStyle
        End If
      End With
    End Sub
    
    Produces this output:

    Code:
    Style Name: Body Text First Indent
    Description: Body Text + Indent:  First  0.15"
        Based on:
        Style Name: Body Text
        Description: Normal + Space  after 6 pt
            Based on:
            Style Name: Normal
            Description: Font: Times New Roman, 12 pt, English (U.S.), Flush left, Line spacing single, Widow/orphan control
    
    Now, that's a limited example. In the final version, I'd probably output to a document, and include a line formatted in the particular style. "The quick brown fox jumped over the lazy dog."

    Something like that.

    chris.
     
  5. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,086
    [bump 3]

    Here's the code I ended up with, assuming a lot about what you want to see for output.

    I'm not very knowledgeable about the Word model, so. There might be shortcuts/more efficient ways to add text to a document.

    Code:
    Private Sub OutputStyleDescriptions(ByVal strStyleName As String, _
                                        ByRef doc As Document)
      Dim rng As Range
      Dim sty As Style
    
      Static i As Integer
    
      If Len(strStyleName & vbNullString) > 0 Then
    
        Set doc = Word.ActiveDocument
        Set sty = doc.Styles(strStyleName)
    
        Set rng = doc.Paragraphs.Last.Range
        With sty
          rng.Text = String(i, Chr$(9)) & "Style Name: " & .NameLocal & vbCrLf & _
                     String(i, Chr$(9)) & "Description: " & .Description & vbCrLf & _
                     String(i, Chr$(9)) & "The quick brown fox jumped over the lazy dog."
          rng.Style = strStyleName
    
          If Len(.BaseStyle & vbNullString) > 0 Then
            doc.Paragraphs.Add
            Set rng = doc.Paragraphs.Last.Range
            i = i + 1
            rng.Text = vbCrLf & String(i, Chr$(9)) & "Based on:"
            doc.Paragraphs.Add
            OutputStyleDescriptions .BaseStyle, doc
          Else
            i = 0
          End If
        End With
      End If
    End Sub
    
    The above recursive routine gets called by the following wrapper:

    Code:
    Public Sub CheckStyles(ByVal strList As String, _
                           ByVal strDelimiter As String)
      Dim doc         As Document
      Dim saStyles()  As String
      Dim i           As Integer
    
      Set doc = Word.ActiveDocument
    
      doc.PageSetup.Orientation = wdOrientLandscape
    
      saStyles = Split(strList, strDelimiter)
    
      For i = 0 To UBound(saStyles)
        OutputStyleDescriptions saStyles(i), doc
        If i < UBound(saStyles) Then
          With doc
            With .Range
              .Characters(.End).InsertBreak wdPageBreak
            End With
            .Paragraphs.Add
          End With
        End If
      Next i
    End Sub
    
    Now, I don't know how to pass values to macros, so to run the routine I just use the Immediate Window with the following line (some styles we use):

    Code:
    CheckStyles "document map, footnote reference, footnote text, list bullet, macro text, message header, plain text, title, toa heading", ", "
    
    You could modify the wrapper to output every style in the document, but testing that printed 100+ styles, so I doubt you want to do that.

    Anyway.

    chris.
     

    Attached Files:

  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/435399

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice