Note that there are some explanatory texts on larger screens.

plurals
  1. POHow-to: Remove text from Word document footer using C#
    primarykey
    data
    text
    <p>I'm trying to remove a footer from a Word document using C# 4. The footer looks like this:</p> <blockquote> <p>Page 1 April 18, 2012</p> </blockquote> <p>Actually, this the text for the footer when displayed in Word VBA:</p> <blockquote> <p>Page 1 ( April 18, 2012</p> </blockquote> <p>There's actually a bullet character between "Page 1" and "April". In the end the footer should look like this:</p> <blockquote> <p>April 18, 2012</p> </blockquote> <p>Anyway, in Word VBA, I'm able to do it using this code:</p> <pre><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 </code></pre> <p>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;</p> <pre><code>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); </code></pre> <p>I even tried other ways to get rid of "Page 1" &amp; the bullet, such as:</p> <pre><code>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); </code></pre> <p>This doesn't do anything.</p> <p>Please let me know what I'm doing wrong. Thank you in advance.</p> <p>I'm not sure if this is important, but the bullet is a Unicode-2022.</p> <p>Here's a screen cap of the document footer. I just need to remove the text "Page 1" &amp; bullet and replace it with the date. The rest should remain as is.</p> <p><img src="https://i.stack.imgur.com/X2WC8.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload