Word/excel automatisieren ohne #import und MFC



  • Hallo,
    ich bin gerade dabei eine Applikation in VC++ zu schreiben, welche über
    OLE auf MS Word zugreift.

    Ich erstelle über "Word.Application" eine Instance auf MS Word und öffne dort ein Dokument.

    CLSID clsidWordApplication;
    HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsidWordApplication);
    
    IDispatchPtr spIWord = NULL;
    // Word - Application erzeugen:
    hr = CoCreateInstance(clsidWordApplication, NULL, CLSCTX_SERVER, ID_IDispatch, (void **) (IDispatch *) &spIWord);
    
    //Zugriff auf Datein-Objekt
    DISPID dispidDocuments = 0;
    hr = m_pDispWord->GetIDOfName(L"Documents", &dispidDocuments);
    hr = m_pDispWord->GetProperty(dispidDocuments, &vResult);
    CComDispatchDriver spDocuments(vResult.pdispVal);
    
    //Öffnen der Datei
    hr = spDocuments.Invoke1(L"Open", &vFilename, &vResult);
    

    Nun ist es so das in diesem Word-Dokument auch Excel-Tabellen eingebettet sind.

    Ich möchte nun solch eine Tabelle bearbeiten.
    Dafür hole ich mir diese Tabell als OLE-Object.

    //OLEFormat
    IDispatchPtr spOLEFormat = NULL;
    hr = spDispShape.GetPropertyByName(L"OLEFormat", &vResult);
    spOLEFormat = vResult.pdispVal;
    CComDispatchDriver spDispOLEFormat(spOLEFormat);
    
    //OLE-Object aktivieren
    hr = spDispOLEFormat.Invoke0(L"Activate", &vResult);
    
    //OLEObject holen
    IDispatchPtr spOLEObject = NULL;
    hr = spDispOLEFormat.GetPropertyByName(L"Object", &vResult);
    spOLEObject = vResult.pdispVal;
    CComDispatchDriver spDispOLEObject(spOLEObject);
    
    //Worksheet holen
    IDispatchPtr spActiveSheet = NULL;
    hr = spDispOLEObject.GetPropertyByName(L"ActiveSheet", &vResult);
    spActiveSheet = vResult.pdispVal;
    CComDispatchDriver spDispActiveSheet(spActiveSheet);
    

    Nun möchte ich die Werte in den einzelnen Zellen ändern.

    Dafür holle ich mir über Range die Zelle.

    DispatchPtr spColumns = NULL;
    VARIANT vRangeExcel1, vRangeExcel2;
    vRangeExcel1.vt =VT_BSTR;
    vRangeExcel1.bstrVal = L"A1";	
    vRangeExcel2.vt =VT_BSTR;
    vRangeExcel2.bstrVal = L"A1";	
    hr = spDispActiveSheet.Invoke2(L"Range", &vRangeExcel1, &vRangeExcel2, &vResult);
    

    Leider bekomme ich hier aber immer die Meldung
    0x80020003 Mitglied nicht gefunden.

    Kann mir da jemand weiterhelfen bzw. mir sagen wo ich im Netz dazu Hilfe finde (ausser MSDN).

    Danke
    Friedl



  • Hallo Friedl,
    könntest du bitte mir deine ganze Quellcode schicken?
    jefryf@web.de

    Grüße

    Jefry


Anmelden zum Antworten