Results 1 to 7 of 7

Thread: Looping through Word document for styles

  1. #1

    Thread Starter
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Looping through Word document for styles

    Hi,

    I want to loop through all the lines of a word document and get the style of that sentence, like Heading 1, Heading 2, normal etc.

    How can I do this?

    I know how to open a word document, but I don't know how to loop through all the sentences and grab the style.

    Thanks.
    vbcode1980
    I code C#....

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Looping through Word document for styles

    VB Code:
    1. Dim p As Paragraph
    2. For Each p In ActiveDocument.Paragraphs
    3.     Debug.Print p.Style, p.Range.Font.Name, p.Range.Font.Size
    4. Next
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Re: Looping through Word document for styles

    Is there a way to find out the name of the heading like "Heading 1" or "Heading 3"?
    I code C#....

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Looping through Word document for styles

    Is there a way to find out the name of the heading like "Heading 1" or "Heading 3"?
    That is exactly what p.Style gives you. I just threw in the font name and size in case you were interested.
    I ran the code I gave you against a document with Heading1 and Heading2 styles and it listed them as expected. What results are you getting?
    This world is not my home. I'm just passing through.

  5. #5

    Thread Starter
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Re: Looping through Word document for styles

    Hmm, I didn't get the heading names. Maybe there's something wrong with my test documents.

    I'll try again.
    Thanks
    I code C#....

  6. #6
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Looping through Word document for styles

    The code I gave you was a macro running in Word. It should be exactly the same in VB6 working on the ActiveDocument object.

    What result do you get if you run the macro in word? If it works ok then I'd guess that your Word object isn't pointing at the document you think it is.
    This world is not my home. I'm just passing through.

  7. #7

    Thread Starter
    Hyperactive Member vbcode1980's Avatar
    Join Date
    Nov 2005
    Location
    Anywhere the wind blows
    Posts
    365

    Re: Looping through Word document for styles

    Strange..

    Anyway, my boss wants it in .NET now, so I'm moving to the .NET forum.s
    Because if I use this code in .NET I get 'System._ComObject' for style...
    I code C#....

Posting Permissions

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



Click Here to Expand Forum to Full Width