Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Word
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 29-Mar-13 4:26am
Comments
Prasad Khandekar at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Wink | ;)
[/EDIT]
  Permalink  
v2
Comments
ckulasekaran at 30-Mar-13 0:23am
   
Thank U for response. But it display "system.__comobject" not para style.
Maciej Los at 30-Mar-13 4:53am
   
Ok, check now ;)
ckulasekaran at 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 at 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)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100