none
How-to: Remove text from Word document footer using C# RRS feed

  • Question

  • I'm trying to remove a footer from a Word document using C# 4.  The footer looks like this:
    Page 1 April 18, 2012

    Actually, this the text for the footer when displayed in Word VBA:
    Page 1 ( April 18, 2012

    There's actually a bullet character between "Page 1" and "April".  In the end the footer should look like this:
    April 18, 2012

    Anyway, in Word VBA, I'm able to do it using this code:   
    Dim rngFtr As Range    
    Set rngFtr = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range    
    rngFtr.Collapse wdCollapseStart    
    rngFtr.MoveStart wdParagraph, 1    
    rngFtr.MoveEnd wdWord, 4    
    rngFtr.Delete
        
    I tried the same thing in C# but it removes the footer entirely.  Here's my code in C# 4:
    using Microsoft.Office.Interop.Word;    
    Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();    
    Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);    
    doc.Activate();    
    Microsoft.Office.Interop.Word.Range ftrRng =        doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;    
    ftrRng.Collapse(WdCollapseDirection.wdCollapseEnd);    
    ftrRng.MoveStart(WdUnits.wdParagraph, 1);    
    ftrRng.MoveEnd(WdUnits.wdWord, 4);    
    ftrRng.Delete();        
    ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);    
    ((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);        
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
         
    I even tried other ways to get rid of "Page 1" & the bullet, such as:
    var replaceText = string.Empty;    
    object mis = System.Type.Missing;    
    var targetFooterText =        doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.ToString().Substring(1, 10);    
    doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Execute(        
        targetFooterText,        
        ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, 
        ref mis, ref mis,        
        replaceText,        
        ref mis, ref mis, ref mis, ref mis, ref mis);
    This doesn't do anything.

    Please let me know what I'm doing wrong.  Thank you in advance.

    I'm not sure if this is important, but the bullet is a Unicode-2022.
    Friday, April 20, 2012 3:24 AM

Answers

  • Thanks to Bruce Song, I was able to get the right answer.

    Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();

    Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);
    doc
    .Activate();

    // These 3 lines did the trick.
    doc
    .ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
    doc
    .Application.Selection.MoveRight(WdUnits.wdCharacter, 1);
    doc
    .Application.Selection.Delete(WdUnits.wdCharacter, 9);

    ap
    .Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

    ((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);

    Thursday, April 26, 2012 2:30 PM

All replies

  • I'm trying to remove a footer from a Word document using C# 4.  The footer looks like this:
    Page 1 April 18, 2012

    Actually, this the text for the footer when displayed in Word VBA:
    Page 1 ( April 18, 2012

    There's actually a bullet character between "Page 1" and "April".  In the end the footer should look like this:
    April 18, 2012

    Anyway, in Word VBA, I'm able to do it using this code:   
    Dim rngFtr As Range    
    Set rngFtr = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range    
    rngFtr.Collapse wdCollapseStart    
    rngFtr.MoveStart wdParagraph, 1    
    rngFtr.MoveEnd wdWord, 4    
    rngFtr.Delete
        
    I tried the same thing in C# but it removes the footer entirely.  Here's my code in C# 4:
    using Microsoft.Office.Interop.Word;    
    Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();    
    Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);    
    doc.Activate();    
    Microsoft.Office.Interop.Word.Range ftrRng =        doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;    
    ftrRng.Collapse(WdCollapseDirection.wdCollapseEnd);    
    ftrRng.MoveStart(WdUnits.wdParagraph, 1);    
    ftrRng.MoveEnd(WdUnits.wdWord, 4);    
    ftrRng.Delete();        
    ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);    
    ((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);        
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
         
    I even tried other ways to get rid of "Page 1" & the bullet, such as:
    var replaceText = string.Empty;    
    object mis = System.Type.Missing;    
    var targetFooterText =        doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text.ToString().Substring(1, 10);    
    doc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Find.Execute(        
        targetFooterText,        
        ref mis, ref mis, ref mis, ref mis, ref mis, ref mis, 
        ref mis, ref mis,        
        replaceText,        
        ref mis, ref mis, ref mis, ref mis, ref mis);
    This doesn't do anything.

    Please let me know what I'm doing wrong.  Thank you in advance.

    I'm not sure if this is important, but the bullet is a Unicode-2022.
    Friday, April 20, 2012 3:24 AM
  • I should've mentioned that I have an image on the right side of the footer.  How do I preserve all existing formats and images as it & just modify the text alone?

    Here's a screencap of document footer for your reference.  I just need to remove "Page 1" & bullet and have the date there.  The rest shouldn't change.  So, I need the line & the image thats' on the right.

    Friday, April 20, 2012 5:41 PM
  • Hi inquistive_one,

    Thanks for posting in the MSDN Forum.

    I hope this snippet will help you:

                Word.Application app = new Word.Application();
                Word.Document doc = app.Documents.Open(usefile);
                app.Visible = true;
                Word.HeaderFooter f = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
                Regex reg = new Regex("Page \\d ● ");
                f.Range.Text = reg.Replace(f.Range.Text, "");

    This is the original footer:

    I think it's similar issue for you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, April 23, 2012 2:11 AM
    Moderator
  • Thanks to Bruce Song, I was able to get the right answer.

    Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();

    Document doc = ap.Documents.Open(docFile.FullName, ReadOnly: false, Visible: false);
    doc
    .Activate();

    // These 3 lines did the trick.
    doc
    .ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
    doc
    .Application.Selection.MoveRight(WdUnits.wdCharacter, 1);
    doc
    .Application.Selection.Delete(WdUnits.wdCharacter, 9);

    ap
    .Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

    ((_Application) ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);

    Thursday, April 26, 2012 2:30 PM