Solved

Using Excel9.CPP to convert .xls to .txt

Posted on 2004-08-02
5
653 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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 100
How to create frequencies of a variable from SAS dataset? 10 125
fix34  challenge 9 117
Bartender label printing - switch on and off graphics 3 36
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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