Reputation: 58
Please help to remove a header and footer from the word document and change the font and save the doc.
Here is my code done only for change of font.
var application = new Microsoft.Office.Interop.Word.Application();
var doc = application.Documents.Open("word doc file here");
object start = doc.Content.Start;
object end = doc.Content.End;
Word.Range rng = doc.Range(ref start, ref end);
rng.Font.Name = "Times New Roman";
rng.Select();
doc.Save();
doc.Close();
Upvotes: 0
Views: 3079
Reputation: 25693
I'm assuming the code you show us works for you as far as changing the font and saving the document.
The following snippet shows two alternatives for changing the font. I used the font color to make things more obvious. The first changes the base style that underlies most of the font formatting you'll find in Word; the second is basically what you have - applying the formatting as if you'd select the body of the document and format it. The difference in my code is that it uses Document.Content
which returns the Range
for the main body of the document without needing to specify start and end values.
The snippet also demonstrates how to access a document's default Header and Footer. Note that Word documents can be extremely complex, with multiple Section
objects, and each section object can have a "normal" header and footers as well as optionally a different header and footer for the first page. This snippet assumes the document has but the one section, and no different first page.
Word.HeaderFooter hdr = doc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
hdr.Range.Delete();
Word.HeaderFooter ftr = doc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
ftr.Range.Delete();
doc.Styles[Word.WdBuiltinStyle.wdStyleNormal].Font.ColorIndex = Word.WdColorIndex.wdBlue;
doc.Content.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
Upvotes: 1