Search code examples
c#ms-wordms-officeoffice-interop

Adding More then one items in Microsoft.Office.Interop.Word header


So in using Microsoft.Office.Interop.Word to generate letters automatically, the issue I am having is the header section. Whenever I run the code below it just replaces the first Item in the header on all pages instead of having both the client name and client address on the header of all pages on different lines, I just get the client address

foreach (Section section in document.Sections)
                {
                    //Get the header range and add the header details.
                    var headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    var headerRange1 = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

                    headerRange.Fields.Add(headerRange, WdFieldType.wdFieldPage);
                    headerRange1.Fields.Add(headerRange, WdFieldType.wdFieldPage);

                    headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    headerRange1.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;

                    headerRange.Font.ColorIndex = WdColorIndex.wdBlack;
                    headerRange1.Font.ColorIndex = WdColorIndex.wdBlack;

                    headerRange.Font.Size = 12;
                    headerRange1.Font.Size = 12;

                    headerRange.Font.Name = "Arial";
                    headerRange1.Font.Name = "Arial";

                    headerRange.Font.Bold = 1;
                    headerRange1.Font.Bold = 1;

                    headerRange.Text =  ClientNameBox.Text;
                    headerRange.InsertParagraphAfter();
                    headerRange1.Text = ClientsAddressBox.Text;


                    headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    headerRange1.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;

                }

Solution

  • Think of a Range like an invisible selection: if you type something when content is selected, what you type replaces what was selected. So if you assign something to a Range that has content, what you assign replaces the content. It doesn't matter whether you assign the same content to two separate Range objects - since they both encompass the same start and end points, changing the one changes the other.

    The trick with a Range, as with a Selection, is to "collapse" it. For a selection, you press an arrow key; for a Range there is a Collapse method, where you specify the direction: either to the Start or to the End.

    foreach (Section section in document.Sections)
                {
                    //Get the header range and add the header details.
                    var headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    
                    headerRange.Fields.Add(headerRange, WdFieldType.wdFieldPage);
    
                    headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                    headerRange.Font.ColorIndex = WdColorIndex.wdBlack;
                    headerRange.Font.Size = 12;
                    headerRange.Font.Name = "Arial";
                    headerRange.Font.Bold = 1;
                    headerRange.Text =  ClientNameBox.Text;
                    headerRange.InsertParagraphAfter();
    
                    object oCollapseEnd = WdCollapseDirection.wdCollapseEnd;
                    headerRange.Collapse(ref oCollapseEnd);
                    headerRange.Text = ClientsAddressBox.Text;
    
                    headerRange.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                }