pigeonbr
asked on
How to add page number to text in footer, using word automation from VC++, MFC??
I'm using VC++ and MFC to make a .doc thru automation. All works ok. But I'm having trouble with headers and footers. I have all the ms word constants added to my project, so I have access to all the pertinent classes.
Here is my question:
How to add a footer with text AND the page number?
This is my code THAT DOES NOT WORK so far:
//footer
HeadersFooters footers(section1.GetFooter s());
HeaderFooter footer1(footers.Item(1));
footer1.SetLinkToPrevious( TRUE);
PageNumbers pageNumber;
COleVariant varPageNumber((long)wdAlig nPageNumbe rCenter);
pageNumber.Add(varPageNumb er,TRUE);
Range rangeFooter1(footer1.GetRa nge());
rangeFooter1.SetText("This is the footer for all pages");
Can someone help me sort this out please ?
Thanks!
Here is my question:
How to add a footer with text AND the page number?
This is my code THAT DOES NOT WORK so far:
//footer
HeadersFooters footers(section1.GetFooter
HeaderFooter footer1(footers.Item(1));
footer1.SetLinkToPrevious(
PageNumbers pageNumber;
COleVariant varPageNumber((long)wdAlig
pageNumber.Add(varPageNumb
Range rangeFooter1(footer1.GetRa
rangeFooter1.SetText("This
Can someone help me sort this out please ?
Thanks!
ASKER
martyn,
I can get the date or time or page number seperately...but the question was how to get both on the same line in the footer? Say, the date at the left side and pagge number in middle ?
I can get the date or time or page number seperately...but the question was how to get both on the same line in the footer? Say, the date at the left side and pagge number in middle ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
martyn,
I'm having lots of headaches on this thing.:( As you can probably deduct from the questions you are helping me on, I'm attempting to get a header with logo on FIRST PAGE only, and a footer with date and page number . Sounds easy..:) hahaha... well, here is all my code...it includes the creation of the objects, and the table, and filling the table from my VC++ app. That all worked well before I got into this header/footer business.! Is there an order in which I should do things..? Like can I put the code for the headers and fotters right before inserting empty lines on first page to leave room for logo, and then creating table and filling it ? When I do this all compiles, but I get a run error when it gets to the inserting my lines with the InsertBreak loop...
And the range.Collapse(wdCollapseE nd) gives me a runtime error...I I leave it out, I get the InsertBreak error...can you shed some light ? I might be getting closer, but seems worse...Thanks!
Hers is all my code so far:
COleVariant vTrue((short)TRUE),vFalse( (short)FAL SE),vOpt(( long)DISP_ E_PARAMNOT FOUND, VT_ERROR);
COleVariant varEmpty(DISP_E_PARAMNOTFO UND, VT_ERROR);
COleVariant varTrue((short)VARIANT_TRU E, VT_BOOL), varFalse((short)VARIANT_FA LSE, VT_BOOL);
PrepareEmailDocument();
int nColumn=2,nTotalRows=total LineNumber s;
int nRow=nTotalRows;
_Application application;
bool bPublished = false;
CString strFilename = "C:\\Rapports Inspec\\RapportINSPEC.doc" ; // Change this as necessary
// Start up Word
if (application.CreateDispatc h("Word.Ap plication" ))
{
try
{
// Get the documents collection and add a new document
Documents documents = application.GetDocuments() ;
_Document oDocument = documents.Add(vOpt,vOpt,&v arEmpty,&v arEmpty);
// Get the selection - this is the bit that we actually do the writing to
Selection selection(application.GetS election() );
//Header setup
//************************ ********** ****
Window oWin = application.GetActiveWindo w();
Pane oPane = oWin.GetActivePane();
View oView = oPane.GetView();
oView.SetSeekView(wdSeekCu rrentPageH eader);
//since a doc has already been opened, just find the active one
oDocument = application.GetActiveDocum ent();//
PageSetup oPageSetup = oDocument.GetPageSetup();
//This initializes oPageSetup properly
oPageSetup.SetDifferentFir stPageHead erFooter(V ARIANT_TRU E);
// Selection oSel = application.GetSelection() ;
InlineShapes oShapes = selection.GetInlineShapes( );
// oShapes.AddPicture("c:\\In spec\\Logo Inspec.jpg ",vFalse,v True,vOpt, vOpt,vOpt, vOpt,vOpt) ;
oShapes.AddPicture("C:\\In spec\\Logo Inspec.jpg ", &varEmpty, &varEmpty, &varEmpty);
//************************ ********** ********
//************************ ********** *********
//FOOTER
//************************ ********** ********
//loads header for all pages****not what I want
Sections sections(oDocument.GetSect ions()); // document is your instance of _Document
Section section1(sections.Item(1)) ;
HeadersFooters footers(section1.GetFooter s());
HeaderFooter footer1(footers.Item(1));
footer1.SetLinkToPrevious( TRUE);
Range range(footer1.GetRange());
Fields fields(range.GetFields());
COleVariant varType((long)wdFieldDate) ;//puts date at left of footer
range.Collapse(wdCollapseE nd);
COleVariant varType1((long)wdFieldPage );//puts page number at left of footer
COleVariant varEmpty(DISP_E_PARAMNOTFO UND, VT_ERROR);
fields.Add(range, &varType1, &varEmpty, &varEmpty);
//end of footer code from mertynjpearson experts-exchange
//************************ ********** ********** **
// Selection selection(application.GetS election() );
//go down on page before displaying table to display just first page info
//put some empty lines at beginning of first page
//to leave room for logo at top left.
for(int lower=1;lower<=28;lower++)
{
COleVariant varLineBreak((long)wdLineB reak);
selection.InsertBreak(&var LineBreak) ;
}
//Insert a table
Tables tables(selection.GetTables ());
Table table(tables.Add(selection .GetRange( ), nRow, nColumn, &varEmpty, &varEmpty)); // nRows and nColumns need to be set!
COleVariant varFormat((long)wdTableFor matNone); // Change this as necessary
table.AutoFormat(&varForma t, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty);
//************************ ********** ********** ********** ****
//Dump arrayIndex into the table
j=0;
int titleFlag=0;
CString strCellText;
// for(int nRow=1;nRow<=24;nRow++) //count for line numbers
for(int nRow=1;nRow<=nTotalRows;nR ow++) //count for line numbers
{
for(int nCol=1;nCol<=nColumn;nCol+ +)
{
strCellText=strArray[j];
if(strCellText.Find('=')== 0)
{
strCellText=strArray[j];
strCellText.Remove('=');
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT ext);
_Font font(range.GetFont());
font.SetName("Bookman Old Style Bold");
font.SetSize(14.0);
font.SetUnderline(VARIANT_ TRUE);
titleFlag=1;
}
strCellText=strArray[j];
if(strCellText.Find('#')== 0)
{
strCellText=strArray[j];
strCellText.Remove('#');
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT ext);
_Font font(range.GetFont());
font.SetName("Bookman Old Style Bold");
font.SetSize(12.0);
font.SetUnderline(VARIANT_ TRUE);
titleFlag=1;
}
strCellText=strArray[j];
if(!((strCellText.Find('=' )==0)||(st rCellText. Find('#')= =0)))
{
strCellText=strArray[j];
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT ext);
_Font font(range.GetFont());
font.SetName("Arial");
font.SetSize(11.0);
}
if(titleFlag==1)
{
j+=4;
nCol=3;
titleFlag=0;
}
else
{
j+=1;
}
}
if(j>=actualarraySize)
break;
if(nRow==24)
{
//after this row is sent to the table, the next page is a new page and
//I don't want the logo to appear from here on.
//do I add the code here?? What I've tried doent work...HELP!!
}
}
//************************ ********** ********
//end of code for Detailed report for .doc file
//************************ ********** ********** ********** *****
// Save the document
COleVariant varFilename(strFilename); // strFilename should be set to the filename
oDocument.SaveAs(&varFilen ame, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty);
// Quit the application
application.Quit(&varEmpty , &varEmpty, &varEmpty);
I'm having lots of headaches on this thing.:( As you can probably deduct from the questions you are helping me on, I'm attempting to get a header with logo on FIRST PAGE only, and a footer with date and page number . Sounds easy..:) hahaha... well, here is all my code...it includes the creation of the objects, and the table, and filling the table from my VC++ app. That all worked well before I got into this header/footer business.! Is there an order in which I should do things..? Like can I put the code for the headers and fotters right before inserting empty lines on first page to leave room for logo, and then creating table and filling it ? When I do this all compiles, but I get a run error when it gets to the inserting my lines with the InsertBreak loop...
And the range.Collapse(wdCollapseE
Hers is all my code so far:
COleVariant vTrue((short)TRUE),vFalse(
COleVariant varEmpty(DISP_E_PARAMNOTFO
COleVariant varTrue((short)VARIANT_TRU
PrepareEmailDocument();
int nColumn=2,nTotalRows=total
int nRow=nTotalRows;
_Application application;
bool bPublished = false;
CString strFilename = "C:\\Rapports Inspec\\RapportINSPEC.doc"
// Start up Word
if (application.CreateDispatc
{
try
{
// Get the documents collection and add a new document
Documents documents = application.GetDocuments()
_Document oDocument = documents.Add(vOpt,vOpt,&v
// Get the selection - this is the bit that we actually do the writing to
Selection selection(application.GetS
//Header setup
//************************
Window oWin = application.GetActiveWindo
Pane oPane = oWin.GetActivePane();
View oView = oPane.GetView();
oView.SetSeekView(wdSeekCu
//since a doc has already been opened, just find the active one
oDocument = application.GetActiveDocum
PageSetup oPageSetup = oDocument.GetPageSetup();
//This initializes oPageSetup properly
oPageSetup.SetDifferentFir
// Selection oSel = application.GetSelection()
InlineShapes oShapes = selection.GetInlineShapes(
// oShapes.AddPicture("c:\\In
oShapes.AddPicture("C:\\In
//************************
//************************
//FOOTER
//************************
//loads header for all pages****not what I want
Sections sections(oDocument.GetSect
Section section1(sections.Item(1))
HeadersFooters footers(section1.GetFooter
HeaderFooter footer1(footers.Item(1));
footer1.SetLinkToPrevious(
Range range(footer1.GetRange());
Fields fields(range.GetFields());
COleVariant varType((long)wdFieldDate)
range.Collapse(wdCollapseE
COleVariant varType1((long)wdFieldPage
COleVariant varEmpty(DISP_E_PARAMNOTFO
fields.Add(range, &varType1, &varEmpty, &varEmpty);
//end of footer code from mertynjpearson experts-exchange
//************************
// Selection selection(application.GetS
//go down on page before displaying table to display just first page info
//put some empty lines at beginning of first page
//to leave room for logo at top left.
for(int lower=1;lower<=28;lower++)
{
COleVariant varLineBreak((long)wdLineB
selection.InsertBreak(&var
}
//Insert a table
Tables tables(selection.GetTables
Table table(tables.Add(selection
COleVariant varFormat((long)wdTableFor
table.AutoFormat(&varForma
//************************
//Dump arrayIndex into the table
j=0;
int titleFlag=0;
CString strCellText;
// for(int nRow=1;nRow<=24;nRow++) //count for line numbers
for(int nRow=1;nRow<=nTotalRows;nR
{
for(int nCol=1;nCol<=nColumn;nCol+
{
strCellText=strArray[j];
if(strCellText.Find('=')==
{
strCellText=strArray[j];
strCellText.Remove('=');
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT
_Font font(range.GetFont());
font.SetName("Bookman Old Style Bold");
font.SetSize(14.0);
font.SetUnderline(VARIANT_
titleFlag=1;
}
strCellText=strArray[j];
if(strCellText.Find('#')==
{
strCellText=strArray[j];
strCellText.Remove('#');
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT
_Font font(range.GetFont());
font.SetName("Bookman Old Style Bold");
font.SetSize(12.0);
font.SetUnderline(VARIANT_
titleFlag=1;
}
strCellText=strArray[j];
if(!((strCellText.Find('='
{
strCellText=strArray[j];
Cell cell(table.Cell(nRow, nCol));
Range range(cell.GetRange());
range.InsertAfter(strCellT
_Font font(range.GetFont());
font.SetName("Arial");
font.SetSize(11.0);
}
if(titleFlag==1)
{
j+=4;
nCol=3;
titleFlag=0;
}
else
{
j+=1;
}
}
if(j>=actualarraySize)
break;
if(nRow==24)
{
//after this row is sent to the table, the next page is a new page and
//I don't want the logo to appear from here on.
//do I add the code here?? What I've tried doent work...HELP!!
}
}
//************************
//end of code for Detailed report for .doc file
//************************
// Save the document
COleVariant varFilename(strFilename); // strFilename should be set to the filename
oDocument.SaveAs(&varFilen
// Quit the application
application.Quit(&varEmpty
range.Collapse(wdCollapseE nd) is an invalid statement..
use
COleVariant varCollapse( (long) wdCollapseEnd);
range.Collapse( &varCollapse);
use
COleVariant varCollapse( (long) wdCollapseEnd);
range.Collapse( &varCollapse);
Fields fields(range.GetFields());
COleVariant varType((long)wdFieldPage)
COleVariant varEmpty(DISP_E_PARAMNOTFO
fields.Add(range, &varType, &varEmpty, &varEmpty);
All the best
Martyn