Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. WorkBooks::OpenText file not loading

WorkBooks::OpenText file not loading

Scheduled Pinned Locked Moved COM
csharpdatabasevisual-studiotestingbeta-testing
6 Posts 2 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi, I have the following code sequences whose pourpose is to display a .txt file in a Excel Spreadsheet, however the .txt file is not being loaded after I execute workbooks.opentext a workbook is loaded but there are no sheets displayed let alone text I get good return codes from IDispatch->invoke and good return codes in excpInfo I have changed the file name to a bogous name and the same thing happens

    app.CreateDispatch("Excel.Application"); // this works my code I usally check AX register
    Workbooks objBooks(app.Workbooks()); // while testing

    Workbooks objBooks = app.GetWorkbooks();

    COleVariant FilenameOpen(File), 
        FilenameSave(XlsFile), 
        Origin((short)2), // xlWindows 
        StartRow((short)1), 
        DataType((short)1), // xlDelimited 
        TextQualifier((short)1), // xlDoubleQuote 
        ConsecutiveDelimiter((long)FALSE, VT\_BOOL), 
        Tab((long)FALSE, VT\_BOOL), 
        Semicolon((long)TRUE, VT\_BOOL),
    	Comma((long)FALSE, VT\_BOOL), 
    	Space((long)FALSE, VT\_BOOL),
    	Other((long)FALSE, VT\_BOOL),
    	Otherchar((long)FALSE,VT\_BOOL),
    	Fieldinfo((long) FALSE, VT\_BOOL),
    	TextVisualLayout((long)FALSE, VT\_BOOL),
    	DecimalSeperator((long) FALSE, VT\_BOOL),
    	ThousandSepartor((long) FALSE, VT\_BOOL),
    	TrailingMinusNumbers((long) FALSE, VT\_BOOL),
    	Local((long) FALSE, VT\_BOOL),
                  Fileformat((short)33), // xlExcel4 
                  Save((long)FALSE, VT\_BOOL);
    

    COleSafeArray saRet;
    DWORD numElements[2];
    numElements[0] = 2;
    numElements[1] = 2;
    saRet.Create(VT_I4,2,numElements);
    long index[2];
    long val;
    index[0]=0;
    index[1]=0;
    val = 1;
    saRet.PutElement(index,&val);
    index[0] = 1;
    index[1] = 0;
    val = 2;
    index[0] = 1;
    index[1] = 1;
    val = 9;

    objBooks.OpenText((LPCTSTR)"C:\\Program Files\\Microsoft VIsual Studio\\MyProjects\\I46023\\I46023.txt", Origin, COleVariant((long)1),COleVariant((long)1),
    1,
    ConsecutiveDelimiter, Tab, Semicolon,
    Comma,Space,Other,Otherchar,saRet,TextVisualLayout,
    DecimalSeperator,ThousandSepartor,TrailingMinusNumbers,
    Local);

    app.SetVisible(true);
    app.SetUserControl(TRUE);

    L 1 Reply Last reply
    0
    • F ForNow

      Hi, I have the following code sequences whose pourpose is to display a .txt file in a Excel Spreadsheet, however the .txt file is not being loaded after I execute workbooks.opentext a workbook is loaded but there are no sheets displayed let alone text I get good return codes from IDispatch->invoke and good return codes in excpInfo I have changed the file name to a bogous name and the same thing happens

      app.CreateDispatch("Excel.Application"); // this works my code I usally check AX register
      Workbooks objBooks(app.Workbooks()); // while testing

      Workbooks objBooks = app.GetWorkbooks();

      COleVariant FilenameOpen(File), 
          FilenameSave(XlsFile), 
          Origin((short)2), // xlWindows 
          StartRow((short)1), 
          DataType((short)1), // xlDelimited 
          TextQualifier((short)1), // xlDoubleQuote 
          ConsecutiveDelimiter((long)FALSE, VT\_BOOL), 
          Tab((long)FALSE, VT\_BOOL), 
          Semicolon((long)TRUE, VT\_BOOL),
      	Comma((long)FALSE, VT\_BOOL), 
      	Space((long)FALSE, VT\_BOOL),
      	Other((long)FALSE, VT\_BOOL),
      	Otherchar((long)FALSE,VT\_BOOL),
      	Fieldinfo((long) FALSE, VT\_BOOL),
      	TextVisualLayout((long)FALSE, VT\_BOOL),
      	DecimalSeperator((long) FALSE, VT\_BOOL),
      	ThousandSepartor((long) FALSE, VT\_BOOL),
      	TrailingMinusNumbers((long) FALSE, VT\_BOOL),
      	Local((long) FALSE, VT\_BOOL),
                    Fileformat((short)33), // xlExcel4 
                    Save((long)FALSE, VT\_BOOL);
      

      COleSafeArray saRet;
      DWORD numElements[2];
      numElements[0] = 2;
      numElements[1] = 2;
      saRet.Create(VT_I4,2,numElements);
      long index[2];
      long val;
      index[0]=0;
      index[1]=0;
      val = 1;
      saRet.PutElement(index,&val);
      index[0] = 1;
      index[1] = 0;
      val = 2;
      index[0] = 1;
      index[1] = 1;
      val = 9;

      objBooks.OpenText((LPCTSTR)"C:\\Program Files\\Microsoft VIsual Studio\\MyProjects\\I46023\\I46023.txt", Origin, COleVariant((long)1),COleVariant((long)1),
      1,
      ConsecutiveDelimiter, Tab, Semicolon,
      Comma,Space,Other,Otherchar,saRet,TextVisualLayout,
      DecimalSeperator,ThousandSepartor,TrailingMinusNumbers,
      Local);

      app.SetVisible(true);
      app.SetUserControl(TRUE);

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why are you using the (LPCTSTR) cast on your file path? Do you fully understand what a cast does?

      Use the best guess

      F 1 Reply Last reply
      0
      • L Lost User

        Why are you using the (LPCTSTR) cast on your file path? Do you fully understand what a cast does?

        Use the best guess

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        Richard This is the function prototype in excel.cpp

        oid Workbooks::OpenText(LPCTSTR Filename, const VARIANT& Origin, const VARIANT& StartRow, const VARIANT& DataType, long TextQualifier, const VARIANT& ConsecutiveDelimiter, const VARIANT& Tab, const VARIANT& Semicolon, const VARIANT& Comma,
        const VARIANT& Space, const VARIANT& Other, const VARIANT& OtherChar, const VARIANT& FieldInfo, const VARIANT& TextVisualLayout, const VARIANT& DecimalSeparator, const VARIANT& ThousandsSeparator, const VARIANT& TrailingMinusNumbers,
        const VARIANT& Local)

        Filename is Of type LPCTSTR Thanks

        L 1 Reply Last reply
        0
        • F ForNow

          Richard This is the function prototype in excel.cpp

          oid Workbooks::OpenText(LPCTSTR Filename, const VARIANT& Origin, const VARIANT& StartRow, const VARIANT& DataType, long TextQualifier, const VARIANT& ConsecutiveDelimiter, const VARIANT& Tab, const VARIANT& Semicolon, const VARIANT& Comma,
          const VARIANT& Space, const VARIANT& Other, const VARIANT& OtherChar, const VARIANT& FieldInfo, const VARIANT& TextVisualLayout, const VARIANT& DecimalSeparator, const VARIANT& ThousandsSeparator, const VARIANT& TrailingMinusNumbers,
          const VARIANT& Local)

          Filename is Of type LPCTSTR Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          ForNow wrote:

          Filename is Of type LPCTSTR

          Then you should provide a LPCTSTR in your call, rather than providing a LPCSTR, and then telling the compiler to ignore the fact that the string may not be in the correct format. Lookup the use of the TEXT() macro and make sure your strings are generated correctly, and read the documentation on the correct use of casts.

          Use the best guess

          F 1 Reply Last reply
          0
          • L Lost User

            ForNow wrote:

            Filename is Of type LPCTSTR

            Then you should provide a LPCTSTR in your call, rather than providing a LPCSTR, and then telling the compiler to ignore the fact that the string may not be in the correct format. Lookup the use of the TEXT() macro and make sure your strings are generated correctly, and read the documentation on the correct use of casts.

            Use the best guess

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            The Text macro interperts the string if ansi is defined according to the current codepage I think the string I am passing is being interpetted correctly looking at the varaint type of the file it was being interpetted as a VT_BSTR and had the correct value

            case VT_BSTR:
            {
            LPCOLESTR lpsz = va_arg(argList, LPOLESTR);
            pArg->bstrVal = ::SysAllocString(lpsz);
            if (lpsz != NULL && pArg->bstrVal == NULL)
            AfxThrowMemoryException();

            L 1 Reply Last reply
            0
            • F ForNow

              The Text macro interperts the string if ansi is defined according to the current codepage I think the string I am passing is being interpetted correctly looking at the varaint type of the file it was being interpetted as a VT_BSTR and had the correct value

              case VT_BSTR:
              {
              LPCOLESTR lpsz = va_arg(argList, LPOLESTR);
              pArg->bstrVal = ::SysAllocString(lpsz);
              if (lpsz != NULL && pArg->bstrVal == NULL)
              AfxThrowMemoryException();

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              ForNow wrote:

              I think the string I am passing is being interpetted correctly

              By luck, not judgement; if your project is built in UNICODE mode then it will be wrong. You should not use a cast unless you understand the consequences. Instead, use the TEXT() macro to ensure that your character string is generated in the appropriate character set to match the build. Your cast in the foregoing code is telling the compiler to ignore the fact that the string may be in the wrong character set.

              Use the best guess

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups