Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-23
5
Medium Priority
?
1,451 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
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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

824 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