none
Slow performance RRS feed

  • Question

  • if (range.Start == start)
    //We have found the last (same as previous) occurance in the document content range 
    break; 
    else 
    {
     start = range.Start; 
    //Handle occureance here
     //... //... 
    range.MoveStart(WdUnits.wdParagraph, 1); 
    }
     range.Find.ParagraphFormat.OutlineLevel = WdOutlineLevel.wdOutlineLevel1;
     } 

    Word 2010

    C#

    I am trying to use the code above to replace the code below to enhance performance.  How do I access a paragraph here.

    Any other suggestions on how to trouble shoot a slow Word interop process would be appreciated.

     internal void FormatLetter(ref Word.Document aDoc, ref Word.Application wordApp)
        {
        
          for (int i = 1; i < aDoc.Paragraphs.Count; i++)
          {
           // ErrorLogManager.AddErrorLog("F04", i.ToString(), "FormatLetter", "");
           
           // if (i == 1) this.DeleteLastParagraph(ref aDoc);
            Word.Paragraph para = aDoc.Paragraphs[i];
            if (para.Range.Text.Contains("<O>"))
            {
              object objEnd = para.Range.Start + para.Range.Text.IndexOf("</O>");
              object objStart = para.Range.Start + para.Range.Text.IndexOf("<O>") + 3;
              Word.Range rng0 = aDoc.Range(ref objStart, ref objEnd);
              rng0.Font.Bold = 0;
              rng0.Font.Size = 14F;
              rng0.Font.Name = "OCR A Extended";
              rng0.Select();
            }
            if (para.Range.Text.Contains("<B>"))
            {
              object objEnd = para.Range.Start + para.Range.Text.IndexOf("</B>");
              object objStart = para.Range.Start + para.Range.Text.IndexOf("<B>") + 3;
              Word.Range rng1 = aDoc.Range(ref objStart, ref objEnd);
              rng1.Font.Bold = 1;
              rng1.Font.Name = "Arial";
              rng1.Select();
            }


    Leonard Swarczinski Software Developer Postal Center International

    Friday, October 18, 2013 5:34 PM

Answers

  • It's not possible to give you a suggestion based on the method you show.

    What you need to do is create a table or list of all the formatting the document might require and WHY it's required. This should be done based on the corporate as well as the structural requirements of the document. You start at the "bottom", with the basic ("default") font name and size used for the body of text. This should be used for the Normal style, or if you don't want to use Normal, for the style you want to use on which other styles bases.

    Then you decide which font is to be used for Headings. Generally, it's a good idea to use Word's built-in Heading styles, unless you have a good reason not to, as Word bases many automatisms on those.

    You then lay out the additional formatting requirements. If they're only font requirements, such as bolding within text, define CHARACTER styles. If entire paragraphs should be different, base a new style on the Normal style and define what should be different: that style will inherit everything else from the Normal style, so only the minimal change is necessary.

    Once you know what styles you need, and why, you set up code that will be executed when the document is created to write these styles into the document.

    And you adjust the rest of your code to apply the appropriate style, based on the requirements that determine the parameters for the method you show us. To put it another way, you'd no longer need this method as you can substitute applying the style for calling the method.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 22, 2013 6:36 AM
    Moderator

All replies

  • Hi Leonard

    Well, one thing that would certainly speed things up would be for you to create a set of STYLES for the formatting you want to apply then apply the style (one command) rather than multiple formatting commands. Every single command you send across the interop and application barriers is going to slow things down, so there should be as few as possible.

    <<I am trying to use the code above to replace the code below to enhance performance.  How do I access a paragraph here.>>

    Do you mean, you don't know how to get the Paragraph object from range.MoveStart(WdUnits.wdParagraph, 1);

    That would be something like: Paragraph para = range.Paragraphs[1];


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, October 19, 2013 6:50 PM
    Moderator
  • Given the method below,  how would you suggest the use of a style to improve performance?
        public void WriteParagraph(string line, ref Microsoft.Office.Interop.Word.Document WordDocument,
                                   string Font, float FontSize, float IndentLeft, bool Bold, bool Italics,
                                   Word.WdParagraphAlignment ParagraphAlignment)
        {
          object oMissing = Missing.Value;
          object DocumentEndIndex = "\\endofdoc";
          object endDocument;
    
          endDocument = WordDocument.Bookmarks.get_Item(ref DocumentEndIndex).Range;
          Word.Paragraph para = WordDocument.Content.Paragraphs.Add(ref endDocument);
          para.Range.Text = line;
          if (Bold)
          {
            para.Range.Font.Bold = 1;
          }
          else
          {
            para.Range.Font.Bold = 0;
          }
          para.Range.Font.Italic = (Italics) ? 1 : 0;
          para.Range.Font.Name = Font;
          para.Range.Font.Size = FontSize;
          para.Format.LeftIndent = IndentLeft;
          para.Range.ParagraphFormat.Alignment = ParagraphAlignment;
          para.Range.InsertParagraphAfter();
          // }
        }


    Leonard Swarczinski Software Developer Postal Center International

    Monday, October 21, 2013 6:10 PM
  • It's not possible to give you a suggestion based on the method you show.

    What you need to do is create a table or list of all the formatting the document might require and WHY it's required. This should be done based on the corporate as well as the structural requirements of the document. You start at the "bottom", with the basic ("default") font name and size used for the body of text. This should be used for the Normal style, or if you don't want to use Normal, for the style you want to use on which other styles bases.

    Then you decide which font is to be used for Headings. Generally, it's a good idea to use Word's built-in Heading styles, unless you have a good reason not to, as Word bases many automatisms on those.

    You then lay out the additional formatting requirements. If they're only font requirements, such as bolding within text, define CHARACTER styles. If entire paragraphs should be different, base a new style on the Normal style and define what should be different: that style will inherit everything else from the Normal style, so only the minimal change is necessary.

    Once you know what styles you need, and why, you set up code that will be executed when the document is created to write these styles into the document.

    And you adjust the rest of your code to apply the appropriate style, based on the requirements that determine the parameters for the method you show us. To put it another way, you'd no longer need this method as you can substitute applying the style for calling the method.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 22, 2013 6:36 AM
    Moderator