none
Microsoft.Office.Interop.Word c# check footer page number RRS feed

  • Question

  • I am working on Office.Interop.Word using c#. I have a simple winform and has a button and all this button does,  place the name of the document at the footer. But the only problem is, it removes everything before it insert the Doc number. So if the document had page number in it or any other footer, it will remove everything and put the document number. 

    What can I do to check if there is page number, dont remove it or dont remove existing footer specially the existing page number. and append the name at the footer. Here is my code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using word = Microsoft.Office.Interop.Word;
    
    namespace WordAddIn1
    {
        public partial class HB_File_Stamp : Form
        {
            public HB_File_Stamp()
            {
                InitializeComponent();
            }
    
    		private void button3_Click(object sender, EventArgs e)
    		{
    			foreach (word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                {
    				string documentname = Globals.ThisAddIn.Application.ActiveDocument.Name;
    				word.Range footerRange = wordSection.Footers[word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    footerRange.Font.ColorIndex = word.WdColorIndex.wdBlack;
                    footerRange.Font.Size = 7;
                    footerRange.Text = documentname;
    			}
    		}
    	}
    }


    Monday, January 22, 2018 2:42 PM

Answers

  • Hi newbiesaspnet,

    You can try to check whether Footer.Range is null or not.

    If it is not null then you can try to use 'Range.insertAfter'.

    It will keep your Footer as it is and append new text to it.

    Code :

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                foreach (word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                {
                  
                    string documentname = Globals.ThisAddIn.Application.ActiveDocument.Name;
                    word.Range footerRange = wordSection.Footers[word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    
    
    
                        footerRange.Font.ColorIndex = word.WdColorIndex.wdBlack;
                        footerRange.Font.Size = 7;
                    footerRange.InsertAfter("Doc No: " + documentname);
                   
    
    
    
                }
            }

    Footer before running the code looks like below.

    Footer after running the code:

    Further, you can apply desired formatting to it.

    Reference:

    Range.InsertAfter Method (Word)

    Regards

    Deepak


    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, January 23, 2018 10:17 AM
    Moderator

All replies

  • Hi newbiesaspnet,

    You can try to check whether Footer.Range is null or not.

    If it is not null then you can try to use 'Range.insertAfter'.

    It will keep your Footer as it is and append new text to it.

    Code :

    private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                foreach (word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
                {
                  
                    string documentname = Globals.ThisAddIn.Application.ActiveDocument.Name;
                    word.Range footerRange = wordSection.Footers[word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    
    
    
                        footerRange.Font.ColorIndex = word.WdColorIndex.wdBlack;
                        footerRange.Font.Size = 7;
                    footerRange.InsertAfter("Doc No: " + documentname);
                   
    
    
    
                }
            }

    Footer before running the code looks like below.

    Footer after running the code:

    Further, you can apply desired formatting to it.

    Reference:

    Range.InsertAfter Method (Word)

    Regards

    Deepak


    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, January 23, 2018 10:17 AM
    Moderator
  • It works. Thank You. Do you know how can I only keep the existing page number at the footer but replace everything else at the footer with my new footer ?
    Tuesday, January 23, 2018 2:03 PM
  • Hi newbiesaspnet,

    You had asked,"how can I only keep the existing page number at the footer but replace everything else at the footer with my new footer ?"

    Several ways that may help to solve your issue.

    If you already know that which text you want to replace with a new text then you can try to use Find and replace in Footer.

    Example:

    object replaceAll = MSWord.WdReplace.wdReplaceAll;
    
    foreach (Microsoft.Office.Interop.Word.Section section in oDoc.Sections)
    {
          Microsoft.Office.Interop.Word.Range footerRange = section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
          footerRange.Find.Text = "Some Text";
          footerRange.Find.Replacement.Text = "Replace Text";
          footerRange.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);
                        }

    Reference:

    How to: Programmatically Search for and Replace Text in Documents

    If you don't know the contents of footer then you can try to replace all the contents including page numbers and after adding your content in footer you can again add the page numbers in footer with help of line below.

      wordSection.Footers[word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add();

    Reference:

    PageNumbers.Add Method (Word)

    Regards

    Deepak


    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.

    Wednesday, January 24, 2018 5:52 AM
    Moderator