Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MSHTML Table Insertion using CHtmlEditView

MSHTML Table Insertion using CHtmlEditView

Scheduled Pinned Locked Moved C / C++ / MFC
c++htmlcsharphelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 51754
    wrote on last edited by
    #1

    Hello - I'm building a product that requires an html wysiwyg editor - I upgraded to Visual C++ .NET (7) to get MFC 7 - with the CHtmlEditView / CHtmlEditDoc classes, only to find that there is no IDM_TABLE command. I also tried inserting using IHTMLDocument2: BSTR str = SysAllocString(L"   "); IHTMLDocument2 *pDoc; long col=0; IDispatch * pDocDisp = NULL; // get the DOM pDocDisp = GetHtmlDocument(); HRESULT h = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); if( h == S_OK ) { IMarkupServices *pMS = NULL; IMarkupPointer *pMkStart = NULL; IMarkupPointer *pMkFinish = NULL; CComPtr pElement; HRESULT hr = pDoc->createElement(L"TABLE", &pElement); pElement->put_outerHTML(str); pDoc->QueryInterface(IID_IMarkupServices, (LPVOID *) &pMS); //pMS->MovePointersToRange( pMS->InsertElement(pElement, pMkStart, pMkFinish); and also tried IHTMLTable / QueryInterface. None worked. If anyone has a solution to my problem I'd be greateful. Thanks. Art Cote Art Cote

    A S 2 Replies Last reply
    0
    • U User 51754

      Hello - I'm building a product that requires an html wysiwyg editor - I upgraded to Visual C++ .NET (7) to get MFC 7 - with the CHtmlEditView / CHtmlEditDoc classes, only to find that there is no IDM_TABLE command. I also tried inserting using IHTMLDocument2: BSTR str = SysAllocString(L"   "); IHTMLDocument2 *pDoc; long col=0; IDispatch * pDocDisp = NULL; // get the DOM pDocDisp = GetHtmlDocument(); HRESULT h = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); if( h == S_OK ) { IMarkupServices *pMS = NULL; IMarkupPointer *pMkStart = NULL; IMarkupPointer *pMkFinish = NULL; CComPtr pElement; HRESULT hr = pDoc->createElement(L"TABLE", &pElement); pElement->put_outerHTML(str); pDoc->QueryInterface(IID_IMarkupServices, (LPVOID *) &pMS); //pMS->MovePointersToRange( pMS->InsertElement(pElement, pMkStart, pMkFinish); and also tried IHTMLTable / QueryInterface. None worked. If anyone has a solution to my problem I'd be greateful. Thanks. Art Cote Art Cote

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      IHTMLElement has a function called InsertAdjacentHTML. BTW: I would recommend you to use smart pointers which make life a lot easier, and they also help avoiding memory leaks. Just us it as follows:

      IDispatchPtr pDisp = ...
      IHTMLDocument2Ptr pDoc(pDisp);
      IHTMLElementCollectionPtr pCollection;
      pDoc->get_all(&pCollection);
      IHTMLElementCollection pAllTables(pCollection->tags(CComVariant("table")));
      ...

      1 Reply Last reply
      0
      • U User 51754

        Hello - I'm building a product that requires an html wysiwyg editor - I upgraded to Visual C++ .NET (7) to get MFC 7 - with the CHtmlEditView / CHtmlEditDoc classes, only to find that there is no IDM_TABLE command. I also tried inserting using IHTMLDocument2: BSTR str = SysAllocString(L"   "); IHTMLDocument2 *pDoc; long col=0; IDispatch * pDocDisp = NULL; // get the DOM pDocDisp = GetHtmlDocument(); HRESULT h = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); if( h == S_OK ) { IMarkupServices *pMS = NULL; IMarkupPointer *pMkStart = NULL; IMarkupPointer *pMkFinish = NULL; CComPtr pElement; HRESULT hr = pDoc->createElement(L"TABLE", &pElement); pElement->put_outerHTML(str); pDoc->QueryInterface(IID_IMarkupServices, (LPVOID *) &pMS); //pMS->MovePointersToRange( pMS->InsertElement(pElement, pMkStart, pMkFinish); and also tried IHTMLTable / QueryInterface. None worked. If anyone has a solution to my problem I'd be greateful. Thanks. Art Cote Art Cote

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        As someone said, insertAdjacentHTML is a fine stuff :

        CComQIPtr pHtmlDoc( GetHtmlDocument() );

        CComBSTR bstrWhere = "BeforeEnd";
        CComBSTR bstrText = ...; // html code to insert

        CComPtr pBody;
        pHtmlDoc->get_body( &pBody );

        pBody->insertAdjacentHTML( (BSTR)bstrWhere, (BSTR)bstrText);


        How low can you go ?
        (MS rant)

        U 1 Reply Last reply
        0
        • S Stephane Rodriguez

          As someone said, insertAdjacentHTML is a fine stuff :

          CComQIPtr pHtmlDoc( GetHtmlDocument() );

          CComBSTR bstrWhere = "BeforeEnd";
          CComBSTR bstrText = ...; // html code to insert

          CComPtr pBody;
          pHtmlDoc->get_body( &pBody );

          pBody->insertAdjacentHTML( (BSTR)bstrWhere, (BSTR)bstrText);


          How low can you go ?
          (MS rant)

          U Offline
          U Offline
          User 51754
          wrote on last edited by
          #4

          Thanks for the responses. One question - is insertAdjacentHTML feasible for drag drop operation, i.e., a la IDM_TEXTBOX message - simply drops edit box on CHtmlEditView. What I'd ultimately want to do is to be able to insert a table based on parameters entered in dialog, then drop table onto CHtmlEditView. Similar to the DHTMLedit component using IDM_TRIED_INSERTTABLE. Thanks a bunch for responding so quickly. :)

          S 1 Reply Last reply
          0
          • U User 51754

            Thanks for the responses. One question - is insertAdjacentHTML feasible for drag drop operation, i.e., a la IDM_TEXTBOX message - simply drops edit box on CHtmlEditView. What I'd ultimately want to do is to be able to insert a table based on parameters entered in dialog, then drop table onto CHtmlEditView. Similar to the DHTMLedit component using IDM_TRIED_INSERTTABLE. Thanks a bunch for responding so quickly. :)

            S Offline
            S Offline
            Stephane Rodriguez
            wrote on last edited by
            #5

            Art Cote wrote: One question - is insertAdjacentHTML feasible for drag drop operation, i.e., These are separate things. Handling drag&drop events is a different matter, which should not interfere with adding html code to an existing page.


            How low can you go ?
            (MS rant)

            U 1 Reply Last reply
            0
            • S Stephane Rodriguez

              Art Cote wrote: One question - is insertAdjacentHTML feasible for drag drop operation, i.e., These are separate things. Handling drag&drop events is a different matter, which should not interfere with adding html code to an existing page.


              How low can you go ?
              (MS rant)

              U Offline
              U Offline
              User 51754
              wrote on last edited by
              #6

              The solution: BSTR bstrText = SysAllocString(L"             "); // TODO: Add your command handler code here IHTMLDocument2 *pDoc; IDispatch * pDocDisp = NULL; pDocDisp = GetHtmlDocument(); HRESULT h = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); BSTR bstrWhere = SysAllocString(L"BeforeEnd"); IHTMLElement *pBody; IHTMLLocation *pLocation; pDoc->get_body( &pBody ); pBody->insertAdjacentHTML( (BSTR)bstrWhere, (BSTR)bstrText); SysFreeString(bstrText); SysFreeString(bstrWhere); pDoc->Release(); pDocDisp->Release(); works fine - putting a table into CHtmlEditView. However how does one access the IHTMLTable interface - when I to get_activeElement I get the body, not the selected table. Thanks. Art

              S 1 Reply Last reply
              0
              • U User 51754

                The solution: BSTR bstrText = SysAllocString(L"             "); // TODO: Add your command handler code here IHTMLDocument2 *pDoc; IDispatch * pDocDisp = NULL; pDocDisp = GetHtmlDocument(); HRESULT h = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc ); BSTR bstrWhere = SysAllocString(L"BeforeEnd"); IHTMLElement *pBody; IHTMLLocation *pLocation; pDoc->get_body( &pBody ); pBody->insertAdjacentHTML( (BSTR)bstrWhere, (BSTR)bstrText); SysFreeString(bstrText); SysFreeString(bstrWhere); pDoc->Release(); pDocDisp->Release(); works fine - putting a table into CHtmlEditView. However how does one access the IHTMLTable interface - when I to get_activeElement I get the body, not the selected table. Thanks. Art

                S Offline
                S Offline
                Stephane Rodriguez
                wrote on last edited by
                #7

                Art Cote wrote: I to get_activeElement I get the body, not the selected table. What selected table ? You haven't selected anything here.:confused: I guess you mean you are willing to get a IHTMLTable pointer from the table you have just inserted. That's easy, change your html tags so that you add a <table ...> with a name="mytable" attribute. Then from the document.all collection, you can ask the DOM to retrieve the IHTMLElement whose name is "mytable" : IHTMLElementCollection::item Method HRESULT item( VARIANT name /*VT_BSTR*/, <-- "mytable" VARIANT index, IDispatch** pdisp ); Finally, cast it to IHTMLTable


                How low can you go ?
                (MS rant)

                U 1 Reply Last reply
                0
                • S Stephane Rodriguez

                  Art Cote wrote: I to get_activeElement I get the body, not the selected table. What selected table ? You haven't selected anything here.:confused: I guess you mean you are willing to get a IHTMLTable pointer from the table you have just inserted. That's easy, change your html tags so that you add a <table ...> with a name="mytable" attribute. Then from the document.all collection, you can ask the DOM to retrieve the IHTMLElement whose name is "mytable" : IHTMLElementCollection::item Method HRESULT item( VARIANT name /*VT_BSTR*/, <-- "mytable" VARIANT index, IDispatch** pdisp ); Finally, cast it to IHTMLTable


                  How low can you go ?
                  (MS rant)

                  U Offline
                  U Offline
                  User 51754
                  wrote on last edited by
                  #8

                  Oops - I meant to get the only table element on the page- eventually i will setup element selection. The code works - does one need to update the document somehow ?? so the changes to the table are shown? (fragment) pDoc->get_all(&pCollect); pCollect->item(varID,varIdx, &pDocDisp ); pDocDisp->QueryInterface(IID_IHTMLTable, (void**) &pElem); pElem->put_cols(cols); . . . Thanks again... :-D art

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups