none
Failed to QueryInterface(COM) IInplacePrintPreview of Excel 2016 RRS feed

  • Question

  • I wanna show printpreivew dialog in my application. The following code works with older version Excel but failed with Excel 2016, and returned error is 0x80040155 (Interface not registered)

    LPOLEOBJECT lpObject;

    IInplacePrintPreview pPrintPrieviw;

    HRESULT hr = lpObject->QueryInterface(IID_IInplacePrintPreview, reinterpret_cast<void**>(&pPrintPrieviw));

    So does Excel 2016 not support this interface or any alternative interface is available?


    Wednesday, July 25, 2018 3:10 AM

All replies

  • Hi Michael,

    To use the PrintPriview function via C++, you can refer the code below:

    void printPreviewExcel()
    {
    	// Translate server ProgID into a CLSID. ClsidFromProgID
    	// gets this information from the registry.
    	CLSID clsid;
    	CLSIDFromProgID(L"Excel.Application", &clsid);
    
    	CoInitialize(nullptr); // NULL if using older VC++
    
    						   // Get an interface to the running instance, if any..
    	IUnknown *pUnk;
    	HRESULT hr = GetActiveObject(clsid, NULL, (IUnknown**)&pUnk);
    
    	assert(!FAILED(hr));
    
    	// Get IDispatch interface for Automation...
    	IDispatch *pDisp;
    	hr = pUnk->QueryInterface(IID_IDispatch, (void **)&pDisp);
    	assert(!FAILED(hr));
    
    	//get active presentation 
    	DISPID dispID;
    	LPOLESTR lpDesc = (LPOLESTR)L"ActiveWorkbook";
    	hr = pDisp->GetIDsOfNames(IID_NULL, &lpDesc, 1, LOCALE_USER_DEFAULT, &dispID);
    	
    
    	VARIANT *pVarResult = new VARIANT();
    	VariantInit(pVarResult);
    
    	DISPPARAMS *dispparams = new DISPPARAMS();
    	pDisp->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, dispparams, pVarResult, NULL, NULL);
    
    
    	//call PrintPreview
    	lpDesc = (LPOLESTR)L"PrintPreview";
    	hr = pVarResult->pdispVal->GetIDsOfNames(IID_NULL, &lpDesc, 1, LOCALE_USER_DEFAULT, &dispID);
    	hr = pVarResult->pdispVal->Invoke(dispID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, dispparams, pVarResult, NULL, NULL);
    	
    	// Release the no-longer-needed IUnknown...
    	pUnk->Release();
    }
    Regards & Fei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 26, 2018 9:34 AM
    Moderator