none
Interop.word How to find all horizontal lines in a word document using c# RRS feed

  • Question

  • I have a word document , In which horizontal lines exists. Some horizontal lines as a border lines and some as a shape and some as a pictures and some of them also have object anchor.I want to find all of the horizontal lines.Kindly anyone help me to do it using C#

    Thanks Santosh

    • Moved by Fei Hu Monday, April 9, 2018 5:23 AM Moved Fron C#
    Saturday, April 7, 2018 8:27 PM

Answers

  • Hello santoshsp,

    According to your screenshot, what you are finding is a line shape object, not a horizontal line object.

    We could still loop through the document and check if it is line shape according to Shape.Type.

    Please note it will find all line shape not just horizontal line shapes since there is not property to tell us if the line is horizontal or vertical.

     Word.Application wdApp = Globals.ThisAddIn.Application;
                Word.Document document = wdApp.ActiveDocument;
                int count = 0;
                foreach (Word.Shape shape in document.Shapes)
                {
    
                    if (shape.Type == MsoShapeType.msoLine)
                    {
                        count = count + 1;
                    }
                }
                MessageBox.Show("Find " + count + " Lines In This Document");

    Best Regards,

    Terry


    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.



    • Edited by Terry Xu - MSFT Thursday, April 12, 2018 2:01 AM
    • Marked as answer by santoshsp Sunday, April 15, 2018 7:23 PM
    Thursday, April 12, 2018 2:00 AM

All replies

  • Hi Santosh,

    Your question is more related to office, I will move the thread to word for developers for better support. If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    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.

    • Edited by Fei Hu Monday, April 9, 2018 5:23 AM
    Monday, April 9, 2018 5:22 AM
  • Hello santoshsp,

    You could try to loop through InlineShapes collection of the document and find horizontal line according to InlineShape.Type.

    Here is simply code.

     Word.Application wdApp = Globals.ThisAddIn.Application;
                Word.Document document = wdApp.ActiveDocument;
                int count=0;
                foreach(Word.InlineShape inlineShape in document.InlineShapes)
                {
    
                    if(inlineShape.Type == Word.WdInlineShapeType.wdInlineShapeHorizontalLine)
                    {
                        count = count + 1;
                    }
                }
                MessageBox.Show("Find " + count + " Horizontal Lines In This Document");

    Best Regards,

    Terry


    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.

    Tuesday, April 10, 2018 2:42 AM
  • Hi Terry,

    I go with this, but could not find the same line. It seems that it can be a picture or Auto shape type thing. When I select it and right click , It shown as below.Please help me to find this line or shape.



    Thanks Santosh

    Wednesday, April 11, 2018 3:36 PM
  • Hello santoshsp,

    According to your screenshot, what you are finding is a line shape object, not a horizontal line object.

    We could still loop through the document and check if it is line shape according to Shape.Type.

    Please note it will find all line shape not just horizontal line shapes since there is not property to tell us if the line is horizontal or vertical.

     Word.Application wdApp = Globals.ThisAddIn.Application;
                Word.Document document = wdApp.ActiveDocument;
                int count = 0;
                foreach (Word.Shape shape in document.Shapes)
                {
    
                    if (shape.Type == MsoShapeType.msoLine)
                    {
                        count = count + 1;
                    }
                }
                MessageBox.Show("Find " + count + " Lines In This Document");

    Best Regards,

    Terry


    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.



    • Edited by Terry Xu - MSFT Thursday, April 12, 2018 2:01 AM
    • Marked as answer by santoshsp Sunday, April 15, 2018 7:23 PM
    Thursday, April 12, 2018 2:00 AM