Solved

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

Posted on 2003-11-23
5
1,389 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
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:martynjpearson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
range.Collapse(wdCollapseEnd) is an invalid statement..
use
      COleVariant varCollapse( (long) wdCollapseEnd);
      range.Collapse( &varCollapse);

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now