Search code examples
visual-c++mfcmshtml

Get Current User Selection(IHTMLTxtRangePtr) in MSHTML


How can I get current user selection or current cursor location in HTML document? I am using MSHTML interfaces in MFC.


Solution

  •     CComPtr<IHTMLSelectionObject> pSelection;
        hr = pHTMLDocument->get_selection(&pSelection);
        if (FAILED(hr) || pSelection==NULL)
                return false;
    
        CComPtr<IDispatch> pDispRange;
        hr = pSelection->createRange(&pDispRange);
        if (FAILED(hr) || pDispRange==NULL)
                return false;
        CComPtr<IHTMLTxtRange> pRange;
        hr = pDispRange->QueryInterface(IID_IHTMLTxtRange,
                reinterpret_cast<void**>(&pRange));
        if (FAILED(hr)|| pRange ==NULL)
                return false;