none
Get paragraphs with style name in word RRS feed

  • Question

  • Hi

     Is there a way to get all the paragraphs by giving a style name?

    For example: If i have to get all the paragraphs with Normal style is there an API for it in vb.net?

    Thanks in advance


    Thursday, March 2, 2017 6:39 PM

All replies

  • Hi Prem-,

    This forum discusses and ask questions about .NET Framework Base Classes, Since your issue is more related to word for developer, We'll move it to word for developer for suitable support.

    Thanks for your understanding and cooperation.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 3, 2017 1:58 AM
  • 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 10:58 AM
  • Hi

     Is there a way to get all the paragraphs by giving a style name?

    For example: If i have to get all the paragraphs with Normal style is there an API for it in vb.net?

    Thanks in advance

    Friday, March 3, 2017 10:59 AM
  • Here is a sample of VBA that might help you.

    Sub ChangeTheseStyles()
        ChangeStyleGlobal ActiveDocument, "Heading 1", "LGP Module Name"
        ChangeStyleGlobal ActiveDocument, "Heading 2", "LGP Lesson Name"
        ChangeStyleGlobal ActiveDocument, "LDT Program Name", "LGP Program Name"
        ChangeStyleGlobal ActiveDocument, "LDT Title", "LGP Title"
        ChangeStyleGlobal ActiveDocument, "LDT Table Header Text", "LGP Table Header Text"
        ChangeStyleGlobal ActiveDocument, "LDT Table Text", "LGP Table Text"
        ChangeStyleGlobal ActiveDocument, "LDT Text", "LGP Text"
    End Sub
    
    Private Function ChangeStyleGlobal(ByRef doc As Word.Document, ByVal StyleName1 As String, ByVal StyleName2 As String)
        On Error GoTo errHandler
        Dim rng As Word.Range
        Set rng = doc.Content
        With rng.Find
            .ClearFormatting
            .Text = ""
            .Style = doc.Styles(StyleName1).NameLocal
            .Replacement.ClearFormatting
            .Replacement.Style = doc.Styles(StyleName2).NameLocal
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
        End With
        rng.Find.Execute Replace:=wdReplaceAll
        Exit Function
    errHandler:
        Err.Clear
    End Function
    
    If you are looking to swap styles from text with the style name of "Normal" to some other style that is already in the document, this code should help you figure it out in VB.Net.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Sunday, March 5, 2017 1:42 AM
  • This is a duplicate posting... please see my response to the other one you made on the same day.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Sunday, March 5, 2017 1:46 AM
  • This is yet another duplicate posting.

    Kind Regards, Rich ... http://greatcirclelearning.com

    Sunday, March 5, 2017 1:48 AM
  • How to get here the paragraphs which is having the matched style

    Dim rng As Range
            rng = doc.Content
           
            With rng.Find
                .Style = doc.Styles("Normal").NameLocal
            End With

            MsgBox(rng.Find.Execute)

    Sunday, March 5, 2017 4:34 PM
  • You wouldn't put the rng.Find.Execute command in a message box, you would put the results of the text found in the range itself. Try

    debug.print rng.text
    
    or
    
    msgbox rng.text

    I hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Sunday, March 5, 2017 6:08 PM
  • This is yet another duplicate posting.

    Kind Regards, Rich ... http://greatcirclelearning.com

    So where are all the others? Some links would be useful...

    As for the OP's question, this is something handled quite easily in Word via Find.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 5, 2017 9:47 PM
  • I want the list of paragraghs (list<paragraphs>) which is having the given style?

    Monday, March 6, 2017 9:33 AM
  • It would be helpful if you actually said what you wanted to do. A search would also be likely to turn up a solution - I have posted code for this very task in the past...

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, March 6, 2017 8:59 PM
  • I want to know the following for the paragraph of the given style (its first occurence)

    1.section(section number)

    2.page  (page number)

    Tuesday, March 7, 2017 5:30 PM
  • Using the same range variable name mention above, the VB.Net commands would be:

    MsgBox(rng.Information(Word.WdInformation.wdActiveEndPageNumber))
    MsgBox(rng.Information(Word.WdInformation.wdActiveEndSectionNumber))
    


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 7, 2017 6:31 PM
  • Following code snippets enable you to get the paragraphs that have a Normal style. It depends on .NET Word component.
    Private Shared Sub Main(args As String())
    	Dim doc As New Document()
    	doc.LoadFromFile("example.docx")
    
    	For Each section As Section In doc.Sections
    		For Each paragraph As Paragraph In section.Paragraphs
    					'...
    			If paragraph.StyleName = "Normal" Then
    			End If
    		Next
    	Next
    End Sub

    Tuesday, March 21, 2017 6:31 AM