Solved

Using Excel9.CPP to convert .xls to .txt

Posted on 2004-08-02
5
655 Views
Last Modified: 2013-11-20
i'm trying to use the following function to convert an excel file to a tab delimited file.

The problem is I don't understand the VARIANT params and how to populate them.
Here is the function:

void _Workbook::SaveAs(const VARIANT& Filename, const VARIANT& FileFormat, const VARIANT& Password, const VARIANT& WriteResPassword, const VARIANT& ReadOnlyRecommended, const VARIANT& CreateBackup, long AccessMode, const VARIANT& ConflictResolution,
            const VARIANT& AddToMru, const VARIANT& TextCodepage, const VARIANT& TextVisualLayout)
{
      static BYTE parms[] =
            VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
      InvokeHelper(0x11c, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
             &Filename, &FileFormat, &Password, &WriteResPassword, &ReadOnlyRecommended, &CreateBackup, AccessMode, &ConflictResolution, &AddToMru, &TextCodepage, &TextVisualLayout);
}


I am calling it in an implementation file here:

      Worksheets oWorksheets;
      Workbooks oWorkbooks;
      _Worksheet oWorksheet;
      _Workbook oWorkbook;
      Range oRange;
      sLine = "";
      
      CString sItem;
      if (true == m_bHasExcel)
      {
            oWorkbooks = m_oExcelApp.GetWorkbooks();

            oWorkbook = oWorkbooks.Open(m_FileName,vOpt,COleVariant((short)true),
                        vOpt, vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
            
            oWorksheets = oWorkbook.GetWorksheets();
            oWorksheet = oWorksheets.GetItem(COleVariant((short)1));      

            long vLong = 0;
            VARIANT param;
            VARIANT vText;
            param.vt      = VT_BSTR;
            param.bstrVal = SysAllocString(L"c:\\test.txt\0");
        
                              //VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT      VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
            
                               //HERE
                               oWorkbook.SaveAs(param, vText, vText, vText, vText, vText, vLong , vText, vText, vText, vText);



This function call fails

If someone knows another way to do this please let me know how.
0
Comment
Question by:alan93
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11697055
>>This function call fails

Shouldn't that be

          long vLong = 0;
         VARIANT param;
         VARIANT vText;
         param.vt      = VT_BSTR;
         param.bstrVal = SysAllocString(L"c:\\test.txt\0");
         vText.vt = VT_EMPTY; // <<<<<<<<<<<---------------------!!!
       
                             //VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT      VTS_VARIANT VTS_I4 VTS_VARIANT VTS_VARIANT VTS_VARIANT VTS_VARIANT;
         
                              //HERE
                              oWorkbook.SaveAs(param, vText, vText, vText, vText, vText, vLong , vText, vText, vText, vText);

IIRC you need to set the variant to 'empty'.
0
 

Author Comment

by:alan93
ID: 11697094
probably, I changed that but it still fails.

I get: "SaveAs method of Workbook class failed."
0
 

Author Comment

by:alan93
ID: 11697132

I am probably needing a File Format value (2nd param, see void _Workbook::SaveAs above) but don't know what this should be if I want the output to be tab delimited text .  How do you find this out? Its like a hidden mystery of the universe.
0
 
LVL 16

Accepted Solution

by:
nonubik earned 500 total points
ID: 11701282
You can use xlTextwindows as the FileFormat parameter (20) or choose from the common file formats

xlCSV=6
xlCSVMac=22
xlCSVMSDOS=24
xlCSVWindows=23
xlCurrentPlatformText=-4158
xlExcel9795=436
xlTextMSDOS=21
xlTextPrinter=36
xlTextWindows=20
xlUnicodeText=42

oWorkbook.SaveAs(param, COleVariant((long)20), vText, vText, vText, vText, COleVariant((long)1) , vText, vText, vText, vText);
                                                                                                                                      ^
                                                                                                                                       |
                                                                                                                                 //AccessMode: xlNoChange = 1
0
 

Author Comment

by:alan93
ID: 11703780
Thanks, that is the ticket!

However, it errored saying "not able to convert param 7 from COleVariant to Long" but I just changed it to (long)1 and it worked.

thanks a bunch.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 180
notReplace  challenge 53 142
difference between String.subString() and String.subSequence() 6 235
unix example issues 18 99
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

838 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