Solved

For martynjpearson:Getting a CString into a COleVariant

Posted on 2003-11-26
4
307 Views
Last Modified: 2013-11-20
Hi again;

I've been working hard on my problems...if I do this to get both date and text and page number one beside the other in the footer:

                  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(FALSE);
                  Range range(footer1.GetRange());      
                  Fields fields(range.GetFields());
                  COleVariant varType((long)wdFieldDate);//puts date at left of footer
                  COleVariant varType1((long)wdFieldPage);//puts page number at left of footer
                  COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);
                  COleVariant varCollapseEnd((long)wdCollapseEnd);

                  CString strPage("               Page  ");
                  COleVariant varText;
                  varText=COleVariant(strPage);

                  CString strInspecTitle("        INSPEC-Inspections de bâtiment ");
                  COleVariant varTextInspec;
                  varTextInspec=COleVariant(strInspecTitle);

                  fields.Add(range, &varType, &varEmpty, &varEmpty);
                  range.Collapse(varCollapseEnd);
                  fields.Add(range,&varText, &varEmpty, &varEmpty);
                  range.Collapse(varCollapseEnd);
                  fields.Add(range, &varType1, &varEmpty, &varEmpty);                  
                  range.Collapse(varCollapseEnd);
                  fields.Add(range, &varTextInspec, &varEmpty, &varEmpty);                  

....I get a run error in the line :

fields.Add(range,&varText, &varEmpty, &varEmpty);

...where I'm taking a CString putting it into a COleVariant, and then adding it to the display, but it crashes here...seems to have no problems doing the conversion, but gives me a "Failed" at run time when it gets to this line. Any ideas ? Am I not converting my text to display properly ? Thanks!

0
Comment
Question by:pigeonbr
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9831177
The variant has to be one of the constants indicating the field type - unfortunately, you can't just add a text field like this.

If you want to have a field, then some text, and then some more fields, I believe you'll have to get the start range, get the fields collection, add your field(s), collapse the range, add some text, collapse the range, get the fields collection, add your field(s)... etc.

Hope this helps
Martyn
0
 

Author Comment

by:pigeonbr
ID: 9832731
martyn,
An actual code sample would really be nice here:) since I'm a beginner in automation. I've been racking my brains on this one for 2 weeks every evening...getting closer, but I need this ready for monday...thanks:)
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 125 total points
ID: 9835952
OK, here goes...

        Documents docs(application.GetDocuments());
        _Document doc(docs.Add(&varEmpty, &varEmpty));

        Sections sections(doc.GetSections());
        Section section1(sections.Item(1));
        HeadersFooters footers(section1.GetFooters());
        HeaderFooter footer1(footers.Item(1));              
        footer1.SetLinkToPrevious(FALSE);
        Range range(footer1.GetRange());

        COleVariant varTypeDate((long)wdFieldDate);
        COleVariant varTypePage((long)wdFieldPage);
        COleVariant varCollapse((long)wdCollapseEnd);
        COleVariant varStory((long)wdStory);

        CString strPage("               Page  ");
        CString strInspecTitle("        INSPEC-Inspections de bâtiment ");

        // First off, add the date
        Fields fields(range.GetFields());
        fields.Add(range, &varTypeDate, &varEmpty, &varEmpty);

        range.Collapse(&varCollapse);
        range.SetText(strPage);

        range.Collapse(&varCollapse);
        fields=range.GetFields();
        fields.Add(range, &varTypePage, &varEmpty, &varEmpty);
       
        range.Collapse(&varCollapse);
        range.Move(&varStory, COleVariant((long)1));
        range.SetText(strInspecTitle);

I had to add the call to Range::Move(), as the range did not seem to collapse past the field that I'd just inserted. Strange!

All the best
Martyn
0
 

Author Comment

by:pigeonbr
ID: 9842185
martyn;
Thanks for the code sample...I really appreciate it. from this I gather I can use the same setup for a header. The only thing still illuding me is the setup for getting different headers for different parts of a documnet... we are working that subject together on another question...I will do some more work and get back to you in my other question...thanks very very much...I am getting to understand what is going on. It's not obvious when you've never done it, even from reading etc....thanks again martyn,..really appreciate it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 108
windows 10 how make picture as desktop background 2 65
no14 challenge 14 70
Fibonacci challenge 11 133
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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