Saving HTML page to disc with CHtmlView - Programmer

This is a discussion on Saving HTML page to disc with CHtmlView - Programmer ; Hello, I have an application using a view which displays HTML pages (eventually from URLs) using the MFC class CHtmlView. Is there a simple way to save the content of the view directly to a file, as when one uses ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Saving HTML page to disc with CHtmlView

  1. Saving HTML page to disc with CHtmlView

    Hello,

    I have an application using a view which displays HTML pages (eventually
    from URLs)
    using the MFC class CHtmlView. Is there a simple way to save the content of
    the view directly
    to a file, as when one uses the command 'Save As...' of Internet Explorer?

    Thanks for any suggestion (please reply also to
    jean-claude.garreau@univ-lille1.fr).

    Jean-Claude



  2. Re: Saving HTML page to disc with CHtmlView

    Hi,
    There is a number of methods to save your page.
    1) use a IOleCommandTarget interface implementation provided by Web Browser
    (and CHtmlView):

    void CMyHtmlView::OnHtmlSaveAs()
    {
    ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
    }

    2) You can query your HTML document for IPersistFile interface and use its
    functionality:

    void CMyHtmlView::OnHtmlSave()
    {
    LPDISPATCH pDispatch = GetHtmlDocument();
    if(pDispatch)
    {
    IPersistFile* pFile;
    HRESULT hr = pDispatch->QueryInterface( __uuidof( IPersistFile ),
    (void**)&pFile );
    if (SUCCEEDED(hr))
    {
    pFile->Save( L"с:\\myfile.htm", FALSE);
    pFile->Release();
    }
    pDispatch->Release();
    }
    }

    3) By using document.all collection:

    void CMyHtmlView::HtmlSaveText( IHTMLDocument2* pDoc, CString sFileName)
    // Сохранить текст документа pDoc в файл sFileName
    {
    USES_CONVERSION;

    CComQIPtr< IHTMLElementCollection, &IID_IHTMLElementCollection> pAll;
    pDoc->get_all( &pAll );

    CComBSTR bsAll;
    CComVariant v1( 0), v2;
    CComQIPtr pDisp;
    pAll->item( v1, v2, &pDisp);

    CComQIPtr pElement;
    pElement = pDisp;
    pElement->get_outerHTML( &bsAll);

    ofstream to( (LPCSTR)sFileName);
    if (to)
    {
    CString sHtml( W2A( bsAll));
    to.write( sHtml, sHtml.GetLength());
    to.close();
    }
    }

    --
    WBR, Vitaly Brusentsev,
    AWinstall development team.
    http://www.awinstall.com mailto:vb@awinstall.com

    "Jean-Claude Garreau" wrote in message
    news:bm6qmh$sc9$1@netserv.univ-lille1.fr...
    > Hello,
    >
    > I have an application using a view which displays HTML pages

    (eventually
    > from URLs)
    > using the MFC class CHtmlView. Is there a simple way to save the content

    of
    > the view directly
    > to a file, as when one uses the command 'Save As...' of Internet Explorer?
    >
    > Thanks for any suggestion (please reply also to
    > jean-claude.garreau@univ-lille1.fr).
    >
    > Jean-Claude
    >
    >





+ Reply to Thread