Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "Microsoft Office Spreadsheet10" ActiveX object in VC.NET Dialog

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
0
0
GB
Hello,

I have a dialog window which has a Microsoft Office Spreadsheet10 activeX object inserted into it. I assigned a variable to this object which it defines automatically as CSpreadsheet1 SS (and i have called this SS).

On my dialog I also have a button which when pressed I want the name of the active sheet to alter and the value in cells"A1" to change.

I have included the code below, showing the class definition and the code used when the button is pressed. It compiles ok with no warnings, however the problem is that when I press the button I get a access violation error (Unhandled exception at 0x7c59ba9d in excelTest.exe: Microsoft C++ exception: _com_error @ 0x0012efc4.) to be presice. adn I have the break/continue/help options (which I have tried all but have not got anywhere)

Anyone know what I'm doing wrong? Any help much appreciated. Thanks in advance.

Code:
 class CexcelTestDlg : public CDialog
 {
 // Construction
 public:
    // Standard constructor
    CexcelTestDlg(CWnd* pParent = NULL);

    // Dialog data
   enum { IDD = IDD_EXCELTEST_DIALOG };

protected:
    // DDX/DDV support 
    virtual void DoDataExchange(CDataExchange* pDX);

// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();

    DECLARE_MESSAGE_MAP()
public:
  CSpreadsheet1 SS;

  afx_msg void OnBnClickedOk();
};
Code:
#include "stdafx.h"
#import "progid:Excel.Sheet" auto_search auto_rename rename_search_namespace("Office10")

void CexcelTestDlg::OnBnClickedOk()
{
    using namespace Excel;

    CSpreadsheet1* Ss;
    Ss = &SS;

    _WorksheetPtr sheet;
    sheet = Ss->get_ActiveSheet();
    sheet->Range["A1"]->Value2 = "10";
    sheet->Name = "MY NEW SHEET NAME";
}
 
Microsoft Office Spreadsheet (I think the ProgID is "OWC.spreadsheet") is quite another thing than excel sheet("Excel.Sheet"). They are different ActiveX objects with different properties. So make sure which one of them you're using

Ion Filipski
1c.bmp
 
Ok I made the mistake as I was trying to copy of of the MFC examples which launced excel seperately.

I have a Microsoft Office Spreadsheet Active X control (CSpreadsheet1) how do I modify the contents of the cells????

I'm going round in circles with everything I do???

Thanks
 
you shouldn't mix managed code with unmanaged code.
First of all add a reference to Microsoft Office Web Components into your project. se only .NET native methods to access your COM object.

Ion Filipski
1c.bmp
 
Cheers for the advice, but I dont know waht your talking about! My C++ background is in UNIX so I somethimes have trouble with what is generally perceived as simple Windows based operations (e.g. adding in ActiveX controls)
 
Cheers, I've just got you. Your project is a native project.
First of all, ActiveX is a enough complex technology, and there is no way to do a fast start in ActiveX.
At the second you should know, spread sheet is not excel sheet and is not excel. It only looks like excel but no more than that.
Look in menu Tools/OleView and see the type library for office spreadsheet.
Also, on starting program you should initialize ActiveX. I think this function is AfxOleInitModule.

Ion Filipski
1c.bmp
 
Cheers for the help.

Yes I did realise that the spreadsheet was not an actual excel spreadsheet.

My project compiles and loads as expected and I can enter text/numbers into all the cells in my spreadsheet. I just want to do it in the code.

I have found the library for Office10 (i think) "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL" and import it.

I can do certain things with my spreadsheet (e.g. SS->put_DisplayVerticalScrollBar(false); hides the vertical scrollbar) but have yet to find out how to put values int he cells and ultimatlely fix height and width of cells, format cells etc............
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top