none
How to find whether a style is applied to a paragraph RRS feed

  • Question

  • Hi,

    How to find whether a style is applied to atleast one paragraph in a word document using vb.net.

    And also let me know if it exists, is there an api to get those paragraphs from word using word

    For example: Is there an API in vb.net to find whether "normal" style is applied to atleast one paragraph in word?

    If so can i be able to get those paragraph objects also?

    Thanks in advance

    Friday, March 3, 2017 6:01 AM

All replies

  • Prem,

    To find if there are classes related to office you better can visit the forum Office for Net

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto


    Success
    Cor

    Friday, March 3, 2017 8:11 AM
  • Hello,

    You could use DocX, a free library on NuGet for .docx format.

    http://docx.codeplex.com/

    https://www.nuget.org/packages/DocX/

    http://blogs.quovantis.com/parsing-microsoft-word-document-using-c/ (in C# but easy to translate)

    Imports Novacode
    Public Class Form1
        Private fileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Document.docx")
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim doc As DocX = DocX.Load(fileName)
            Dim paragraphs = doc.Paragraphs _
                .Select(Function(item, index) New With {.P = item, .Index = index}) _
                .Where(Function(item) item.P.StyleName.Equals(ListBox1.Text))
    
            If paragraphs.Count > 0 Then
                Dim theList As New List(Of String)
                For Each item In paragraphs
                    theList.Add(item.Index.ToString)
                Next
                MessageBox.Show(String.Join(",", theList.ToArray))
            Else
                MessageBox.Show($"none for {ListBox1.Text}")
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim doc As DocX = DocX.Load(fileName)
    
            ListBox1.Items.AddRange(doc.Paragraphs.Select(Function(p) p.StyleName).Distinct.ToArray)
            ListBox1.Items.Add("NotAStyle") ' put an invalid style name in the list
            ListBox1.SelectedIndex = 0
        End Sub
    End Class
    

    The document used above has a style KarenStyle1 in two out of three paragraphs. Paragraphs are zero based.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 3, 2017 11:28 AM
    Moderator