word中怎么设置文档不同部分页眉页脚内容不同?修改页眉和页脚的最快

word

1个回答

写回答

卷发妹妹

2025-09-30 06:50

+ 关注

word
word

private void SetHeadersFooters(string file) { System.Object nothing = System.Reflection.Missing.Value; word.Application oApp = new word.Application(); oApp.Visible = true; object wordPath = file; word.Document oDoc = oApp.Documents.Open( ref wordPath, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing,ref nothing, ref nothing ); word.Section oSel = null; word.Range oPageStart = null; int iPage; int iTotalPages; int iSection;

iTotalPages = oDoc.ComputeStatistics(word.WdStatistic.wdStatisticPages, ref nothing);

//Retrieve the headers and footers on each page

Total
Total

iSection = 0; for( iPage = 1; iPage=iTotalPages;++iPage) { //Go to the page represented by the page number iPage and //retrieve its section object which = word.WdGoToDirection.wdGoToABSolute; object what = word.WdGoToItem.wdGoToPage; object count = iPage; oPageStart = oDoc.GoTo( ref what, ref which, ref count, ref nothing); word.Section oSec = oPageStart.Sections.Item(1);

//'If this is a different section than the one in the previous //'iteration and it has a first page header/.footer, then //'retrieve the first page header/footer for this section. //'Otherwise, retrieve the primary header/footer for this section if( (iSection oSec.Index) && (oSec.PageSetup.DifferentFirstPageHeaderFooter==1) ) { //sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.Text; oSec.Headers.Item( word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.Text = string.Empty;//可以设置为您的新内容哦 oSec.Footers.Item(word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.Text = string.Empty;//可以设置为您的新内容哦 //sFooter = oSec.Footers(wdHeaderFooterFirstPage).Range.Text; } else { //sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.Text; //sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.Text; oSec.Headers.Item(word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = string.Empty;//可以设置为您的新内容哦 oSec.Footers.Item(word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = string.Empty;//可以设置为您的新内容哦 }

iSection = oSec.Index; } }

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号