Home > OS >  How to batch modify or replace the WORD multiple text in the document header
How to batch modify or replace the WORD multiple text in the document header

Time:09-21

Please directly, a rookie, thank you! 1!!!!!

CodePudding user response:

This will use word's own library, import or use vs imported first,
Concrete train of thought, in word use vba macros now generate vba code, reference vba code, and then import the corresponding class from the library, and then realize the function you,
The following code, the hope can help you m_WordDoc represents a document object, you first obtain sectional character sets, and then obtain section, and then from each section in the footer and header,
CSections sections;
Sections. AttachDispatch (m_WordDoc get_Sections ());
If (sections. M_lpDispatch==nullptr)
Return TCHandleResult (TC_FAILED, _T (" failed to get sectional character set "));
Long lSectionNum=sections. Get_Count ();

For (long I=0; I & lt; LSectionNum; I++)
{
CSection section;
CHeadersFooters headersFooters;
Section. AttachDispatch (sections. The Item (I + 1));
If (section. M_lpDispatch==nullptr)
continue;
HeadersFooters. AttachDispatch (section. Get_Footers ());
If (headersFooters m_lpDispatch==nullptr)
continue;
Long sFooterCount=headersFooters. Get_Count ();
For (long j=0; J & lt; SFooterCount; J++)
{
CHeaderFooter headerFooter;
HeaderFooter. AttachDispatch (headersFooters. Item (j + 1));
If (headerFooter m_lpDispatch==nullptr)
continue;
CRange range=headerFooter. Get_Range ();
If (range. M_lpDispatch==NULL)
Return TCHandleResult (TC_FAILED, _T (" failed to get footer headers range object "));

Cstrings sText=range. Get_Text ();
CTables0 tables=range. Get_Tables ();
If ((sText GetLength () & lt; 5) & amp; & (0==tables. Get_Count ()))//remove the footer header is empty or only the page number
continue;
}
HeadersFooters. AttachDispatch (section. The get_Headers ());
If (headersFooters m_lpDispatch==nullptr)
continue;
SFooterCount=headersFooters. Get_Count ();
For (long j=0; J & lt; SFooterCount; J++)
{
CHeaderFooter headerFooter;
HeaderFooter. AttachDispatch (headersFooters. Item (j + 1));
If (headerFooter m_lpDispatch==nullptr)
continue;
CRange range=headerFooter. Get_Range ();
If (range. M_lpDispatch==NULL)
Return TCHandleResult (TC_FAILED, _T (" failed to get footer headers range object "));
Cstrings sText=range. Get_Text ();
CTables0 tables=range. Get_Tables ();
}
}
}
  • Related