none
different between Paragraph & headline RRS feed

  • Question

  • as we know the headline exactly is a Paragraph object, but when i pass through the document's Paragraphs, i want to know a Paragraph whether or not a headline, is any properties indicate the different? i don't know how to do it, please help.
    Monday, November 25, 2013 2:17 PM

Answers

  • Hi

    The ListLevelNumber property of the Style class is designed for list-numbered paragraphs.

    As a workaround you can get the localized name of each heading style creating a temporarily paragraph with required style and then deleting it behind the scene. After these steps you may save the localized name of the heading style for future comparing against real paragraph styles:

    Word.Document doc = WordApp.ActiveDocument;
    Word.Paragraph par = doc.Paragraphs.First;
    Word.Style initialStyle = (Word.Style)par.get_Style();
    object s = Word.WdBuiltinStyle.wdStyleHeading1;            
    
    WordApp.ScreenUpdating = false;
    
    Word.Paragraph testPar = doc.Paragraphs.Add();
    testPar.set_Style(ref s);
    Word.Style headingStyle = (Word.Style)testPar.get_Style();
    doc.Paragraphs[doc.Paragraphs.Count].Range.Delete();                    
                    
    if (initialStyle.BuiltIn)
    {
       if(initialStyle.NameLocal == headingStyle.NameLocal)
           MessageBox.Show("Heading1");
    }
    
    WordApp.ScreenUpdating = true;
    Thus, you will support all possible localized names.
    • Marked as answer by sendreams Thursday, November 28, 2013 7:23 AM
    Tuesday, November 26, 2013 7:55 PM

All replies

  • Hello sendreams,

    It looks like your question is related to the one (Word?) from Office host applications (not VSTO).  

    In Word you need to check out the Style property of the Paragraph object. It can return the wdStyleHeading values (see the WdBuiltinStyle enumeration for more values) in case of a headline. Is it what you are looking for?

    Monday, November 25, 2013 3:28 PM
  • think you very much.

    i try the code below:

    Paragraph p = (Paragraph)ie.Current;
    Style style = (Style)p.get_Style();
    string temp = (p.Range.Text.Trim());
    Debug.WriteLine(temp + "  " + style.NameLocal);
    i can get the style namelocal this will be help for me.

    but there is another question. the style nameLocal  property will be changed with the language. do you have any idea?

    Tuesday, November 26, 2013 3:06 AM
  • Hello sendreams,

    It looks like you use an object representing a style:

    Style style = (Style)p.get_Style();
    

    Instead, you need to convert the returning value to the WdBuiltinStyle enumeration (not Style). For example, I have found the following VBA code on the MSDN page:

    For i = 1 To ActiveDocument.Paragraphs.Count 
     If i Mod 2 = 0 Then 
     ActiveDocument.Paragraphs(i).Style = wdStyleNormal 
     Else: ActiveDocument.Paragraphs(i).Style = wdStyleHeading3 
     End If 
    Next i
    

    Tuesday, November 26, 2013 7:16 AM
  • thank you for reply. i'm not use VBA but use VSTO.

    i try you propose but unfortunately this way is not workable. 

    the return value of get_style()  can't be convert to WdBuiltinStyle

    Paragraph p = (Paragraph)ie.Current;    
    WdBuiltinStyle style = (WdBuiltinStyle)p.get_Style();  //convert error
    



    but the set_style() can use WdBuiltinStyle parameter, the code bellow will be ok

    Paragraph p = (Paragraph)ie.Current;
    object s = WdBuiltinStyle.wdStyleHeading1;
    p.set_Style(ref s);


    Tuesday, November 26, 2013 9:22 AM
  • perhaps style.ListLevelNumber will be help for me, i test the value and found the headline 1 style return value 1, headline 2 style return value 2, heandline 3 style return value 3, and the normal Paragraph return value 0.

    thanks again.

    Tuesday, November 26, 2013 9:36 AM
  • Hi

    The ListLevelNumber property of the Style class is designed for list-numbered paragraphs.

    As a workaround you can get the localized name of each heading style creating a temporarily paragraph with required style and then deleting it behind the scene. After these steps you may save the localized name of the heading style for future comparing against real paragraph styles:

    Word.Document doc = WordApp.ActiveDocument;
    Word.Paragraph par = doc.Paragraphs.First;
    Word.Style initialStyle = (Word.Style)par.get_Style();
    object s = Word.WdBuiltinStyle.wdStyleHeading1;            
    
    WordApp.ScreenUpdating = false;
    
    Word.Paragraph testPar = doc.Paragraphs.Add();
    testPar.set_Style(ref s);
    Word.Style headingStyle = (Word.Style)testPar.get_Style();
    doc.Paragraphs[doc.Paragraphs.Count].Range.Delete();                    
                    
    if (initialStyle.BuiltIn)
    {
       if(initialStyle.NameLocal == headingStyle.NameLocal)
           MessageBox.Show("Heading1");
    }
    
    WordApp.ScreenUpdating = true;
    Thus, you will support all possible localized names.
    • Marked as answer by sendreams Thursday, November 28, 2013 7:23 AM
    Tuesday, November 26, 2013 7:55 PM
  • hi

    it looks like not pretty so much, but is a secondary solution. tks

    Wednesday, November 27, 2013 5:13 AM