Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Scripting.Dictionary (VBA 6)

    I want to make a list of the style in use in a document. I'm using the Dictionary object to hold the style names, which allows me to see if the style already exists. For some reason, Body Text as the paragraph style can be added over and over. If I use try to add the string "Body Text" more than once, an error occurs staying the key is already used.... exactly as I would expect.

    The document I'm testing with has 149 paragraphs; about 125 of them are Body Text. Yet Body Text gets added to the dictionary 125 times, not once as I would expect.

    My code follows. Have I overlooked something? I've commented out the If test, and p.style is added to the dictionary many times, despite the repeated value of Body Text. If if add "Body Text" (as in the line that's commented out), the expected error is raised.

    Sub ParagraphStylesInUse()
    Dim p As Paragraph
    Dim oStyleList As Scripting.Dictionary
    Dim x
    Set oStyleList = New Scripting.Dictionary

    For Each p In ActiveDocument.Paragraphs
    'If Not oStyleList.Exists(p.Style) Then
    oStyleList.Add p.Style, "Paragraph" ' Never an error, multiple adds of Body Text
    ' oStyleList.Add "Body Text", "Paragraph" ' Error - key in use
    'End If
    Next p

    For Each x In oStyleList
    Debug.Print x
    Next
    End Sub

  2. #2
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Scripting.Dictionary (VBA 6)

    I just solved this myself. I added a variable to hold the style name: sStyle = p.style, then used the variable in the If and .Add statements. I got the expected results... on mention of each style.

    Is this behavior to be expected?

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Scripting.Dictionary (VBA 6)

    It's confusion between an object (Style) and its default property (NameLocal).

    A Style is an object with many properties, some of which refer to the context in which it is used, so the Style of each paragraph is different as an object.
    The instruction

    oStyleList.Add p.Style.NameLocal, "Paragraph"

    adds the entire Style object to oStyleList.

    The instruction

    sStyle = p.Style

    assigns the default property of the Style object, its NameLocal property, to the string variable sStyle. When you use

    oStyleList.Add sStyle, "Paragraph"

    you're adding the style name to oStyleList, not the entire object. You could also omit the variable and use

    oStyleList.Add p.Style.NameLocal, "Paragraph"

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts

    Re: Scripting.Dictionary (VBA 6)

    I see. Thank you.

Posting Permissions

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