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.GetFooters());
                  HeaderFooter footer1(footers.Item(1));
                  
                  footer1.SetLinkToPrevious(TRUE);
                  PageNumbers pageNumber;
                  COleVariant varPageNumber((long)wdAlignPageNumberCenter);
                  pageNumber.Add(varPageNumber,TRUE);
                  Range rangeFooter1(footer1.GetRange());

                  rangeFooter1.SetText("This is the footer for all pages");
Can someone help me sort this out please ?
Thanks!
pigeonbrAsked:
Who is Participating?
 
martynjpearsonCommented:
Try adding

range.Collapse(wdCollapseEnd)

between inserting the page number and the text - otherwise I think what Word is doing is adding the page number, then overwriting it with the text.

Martyn
0
 
martynjpearsonCommented:
I use this code to add a page number to a header / footer - the variable "range" is the result of calling HeaderFooter::GetRange() as you do in your code.

        Fields fields(range.GetFields());
        COleVariant varType((long)wdFieldPage);
        COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
        fields.Add(range, &varType, &varEmpty, &varEmpty);

All the best
Martyn
0
 
pigeonbrAuthor Commented:
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 ?
0
 
pigeonbrAuthor Commented:
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(wdCollapseEnd) 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)FALSE),vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

      COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
      COleVariant varTrue((short)VARIANT_TRUE, VT_BOOL), varFalse((short)VARIANT_FALSE, VT_BOOL);

      PrepareEmailDocument();
      int nColumn=2,nTotalRows=totalLineNumbers;
      int nRow=nTotalRows;
    _Application application;
    bool bPublished = false;
   CString strFilename = "C:\\Rapports Inspec\\RapportINSPEC.doc"; // Change this as necessary

    // Start up Word
    if (application.CreateDispatch("Word.Application"))
    {
        try
        {
                  
            // Get the documents collection and add a new document
                  Documents documents = application.GetDocuments();
                  _Document oDocument = documents.Add(vOpt,vOpt,&varEmpty,&varEmpty);            
                  // Get the selection - this is the bit that we actually do the writing to
            Selection selection(application.GetSelection());
                  //Header setup
                  //**************************************
                  Window oWin = application.GetActiveWindow();
                  Pane oPane = oWin.GetActivePane();
                  View oView = oPane.GetView();
                  oView.SetSeekView(wdSeekCurrentPageHeader);
                  //since a doc has already been opened, just find the active one
                  oDocument = application.GetActiveDocument();//
                  PageSetup oPageSetup = oDocument.GetPageSetup();
                  //This initializes oPageSetup properly
                  oPageSetup.SetDifferentFirstPageHeaderFooter(VARIANT_TRUE);                   
//                  Selection oSel = application.GetSelection();
                  InlineShapes oShapes = selection.GetInlineShapes();
//                  oShapes.AddPicture("c:\\Inspec\\LogoInspec.jpg",vFalse,vTrue,vOpt,vOpt,vOpt,vOpt,vOpt);
                  oShapes.AddPicture("C:\\Inspec\\LogoInspec.jpg", &varEmpty, &varEmpty, &varEmpty);                    
                  //******************************************                  
                  //*******************************************                  
                  //FOOTER
                  //******************************************
                  //loads header for all pages****not what I want
                  Sections sections(oDocument.GetSections()); // document is your instance of _Document
                  Section section1(sections.Item(1));
                  HeadersFooters footers(section1.GetFooters());
                  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(wdCollapseEnd);
                  COleVariant varType1((long)wdFieldPage);//puts page number at left of footer
                  COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
                  fields.Add(range, &varType1, &varEmpty, &varEmpty);
                  //end of footer code from mertynjpearson experts-exchange
                  //**********************************************
//            Selection selection(application.GetSelection());
                  //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)wdLineBreak);
                        selection.InsertBreak(&varLineBreak);
                  }                  
                  //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)wdTableFormatNone); // Change this as necessary
           table.AutoFormat(&varFormat, &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;nRow++)      //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(strCellText);
                                    _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(strCellText);
                                    _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)||(strCellText.Find('#')==0)))
                              {
                                    strCellText=strArray[j];
                                    Cell cell(table.Cell(nRow, nCol));
                                    Range range(cell.GetRange());
                                    range.InsertAfter(strCellText);
                                    _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(&varFilename, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty, &varEmpty);

            // Quit the application
            application.Quit(&varEmpty, &varEmpty, &varEmpty);
0
 
joshiraviCommented:
range.Collapse(wdCollapseEnd) is an invalid statement..
use
      COleVariant varCollapse( (long) wdCollapseEnd);
      range.Collapse( &varCollapse);

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.