Solved

How to add page number to text in footer, using word automation from VC++, MFC??

Posted on 2003-11-23
5
1,435 Views
Last Modified: 2013-11-20
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!
0
Comment
Question by:pigeonbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9810894
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
 

Author Comment

by:pigeonbr
ID: 9814858
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
 
LVL 8

Accepted Solution

by:
martynjpearson earned 250 total points
ID: 9816403
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
 

Author Comment

by:pigeonbr
ID: 9822096
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
 

Expert Comment

by:joshiravi
ID: 11217943
range.Collapse(wdCollapseEnd) is an invalid statement..
use
      COleVariant varCollapse( (long) wdCollapseEnd);
      range.Collapse( &varCollapse);

0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question