Click here to Skip to main content
14,775,279 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I want to read a word document and get all the formatting styles in that document.
My problem is to find the styles in that document.

Here is my code
Dim doc As New word.Document
Dim docapp As New word.Application
Dim para_tag As word.Paragraph
Dim paracount As Integer

For Each para_tag In docapp.ActiveDocument.Paragraphs
      MsgBox(para_tag.Style)
Next


Plz give some other suggestion..
Thanks...
Posted
Comments
Prasad Khandekar 29-Mar-13 17:05pm
   
Have you tried using OPen XML SDK 2.0 for Office, available here (http://www.microsoft.com/en-in/download/details.aspx?id=5124[^]) on Micrsoft download site? You can use Styles collection of ActiveDocument to enumerate all styles.

For Each styl In ActiveDocument.Styles
Console.Write(styl.NameLocale)
Next

1 solution

Code seems to be OK, but need some corrections:
Using early binding
Dim sFileName As String = "C:\Test.doc"
Dim docapp As New Word.Application
Dim doc As Word.Document = docapp.Documents.Open(sFileName)
Dim para_tag As Word.Paragraph
Dim paracount As Integer

For Each para_tag In doc.Paragraphs
     paracount += 1 
     MsgBox(para_tag.Style.ToString(), MsgBoxStyle.OKOnly, "Para no.:" & paracount.ToString())
Next


Using late binding
Dim sFileName As String = "C:\Test.doc"
Dim docapp As Object = CreateObject("Word.Application")
Dim doc As Object = docapp.Documents.Open(sFileName)
Dim para_tag As Object
Dim paracount As Integer

For Each para_tag In doc.Paragraphs
     paracount += 1 
     MsgBox(para_tag.Style.ToString(), MsgBoxStyle.OKOnly, "Para no.:" & paracount.ToString())
Next


I suggest you to add Try...Catch...End Try[^] block.


[EDIT]
Ok, test it:
Module Module1

    Sub Main()
        Dim sFileName As String = "C:\MyDocument.doc"
        EnumWrdPara(sFileName)
        Console.ReadKey()

    End Sub


    Sub EnumWrdPara(sDocFullName AS String)
        Dim docapp As Object = Nothing
        Dim doc As Object = Nothing
        Dim para_tag As Object = Nothing
        Dim paracount As Integer = 0


        Try
            docapp = CreateObject("Word.Application")
            doc = docapp.Documents.Open(sDocFullName)
            For Each para_tag In doc.Paragraphs
                paracount += 1
                Console.WriteLine("Paragraph no.: {0} - style: (1)", paracount.ToString(), para_tag.Style.ToString)
            Next


        Catch ex As System.NullReferenceException
            Console.WriteLine(ex.Message)

        Catch ex As IO.IOException
            Console.WriteLine(ex.Message)

        Catch ex As Exception
            Console.WriteLine(ex.Message)

        Finally
            para_tag = Nothing
            doc.Close(SaveChanges:=False)
            doc = Nothing
            docapp.Quit()
            docapp = Nothing

        End Try

    End Sub

End Module


It works, it has been checked. ;)
[/EDIT]
   
v2
Comments
ckulasekaran 30-Mar-13 0:23am
   
Thank U for response. But it display "system.__comobject" not para style.
Maciej Los 30-Mar-13 4:53am
   
Ok, check now ;)
ckulasekaran 30-Mar-13 7:11am
   
Thank u mac. I got word styles in below coding.
For Each para_style In doc.Paragraphs
Dim obj As Object
obj = para_style.range.style
MsgBox(obj.namelocal)
Next
Maciej Los 30-Mar-13 7:49am
   
You're welcome. It would be good to mark this post as "solved". ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900