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. MS Word Automation - I get so far - now what?

MS Word Automation - I get so far - now what?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++algorithmstestingtools
3 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.
  • B Offline
    B Offline
    Bryan Anslow
    wrote on last edited by
    #1

    Hi, I am trying to rewrite an old VC++ wrapper class around MS Word Automation - it used to use *.tli and *.tlh files, but now MS recommends just using the *.h files which get generated. So, that is another level of complexity. So, I got so far, now I am wondering what to do next. Here are some code snips, the real code obviously has error correction etc., so here is what I have so far;

    IDispatch *pWordInst;
    CApplication *pWordApp;
    CDocument0 *pDoc;
    IUnknown *pUnknown;

    HRESULT res = CoInitialize(NULL);

    CLSID clsid;
    HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);

    hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWordInst);

    pUnknown = NULL;
    REFIID iid2 = __uuidof(IUnknown);

    hr = pWordInst->QueryInterface(iid2, (void **)&pUnknown);

    So, now I need to get the Application and Document objects (assuming that this code creates a new document as it used to). I am guessing I need a getApplication() but what MS class do I use for that? Once I have the Application, then I can get the Document, presumably with a get_Documents() or get_ActiveDocument()?. After that I guess it will run like my older version used to. Any help and advice would be great. Many Thanks, Bryan.

    L D 2 Replies Last reply
    0
    • B Bryan Anslow

      Hi, I am trying to rewrite an old VC++ wrapper class around MS Word Automation - it used to use *.tli and *.tlh files, but now MS recommends just using the *.h files which get generated. So, that is another level of complexity. So, I got so far, now I am wondering what to do next. Here are some code snips, the real code obviously has error correction etc., so here is what I have so far;

      IDispatch *pWordInst;
      CApplication *pWordApp;
      CDocument0 *pDoc;
      IUnknown *pUnknown;

      HRESULT res = CoInitialize(NULL);

      CLSID clsid;
      HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);

      hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWordInst);

      pUnknown = NULL;
      REFIID iid2 = __uuidof(IUnknown);

      hr = pWordInst->QueryInterface(iid2, (void **)&pUnknown);

      So, now I need to get the Application and Document objects (assuming that this code creates a new document as it used to). I am guessing I need a getApplication() but what MS class do I use for that? Once I have the Application, then I can get the Document, presumably with a get_Documents() or get_ActiveDocument()?. After that I guess it will run like my older version used to. Any help and advice would be great. Many Thanks, Bryan.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Take a look at MS Office OLE Automation Using C++[^].

      1 Reply Last reply
      0
      • B Bryan Anslow

        Hi, I am trying to rewrite an old VC++ wrapper class around MS Word Automation - it used to use *.tli and *.tlh files, but now MS recommends just using the *.h files which get generated. So, that is another level of complexity. So, I got so far, now I am wondering what to do next. Here are some code snips, the real code obviously has error correction etc., so here is what I have so far;

        IDispatch *pWordInst;
        CApplication *pWordApp;
        CDocument0 *pDoc;
        IUnknown *pUnknown;

        HRESULT res = CoInitialize(NULL);

        CLSID clsid;
        HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);

        hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWordInst);

        pUnknown = NULL;
        REFIID iid2 = __uuidof(IUnknown);

        hr = pWordInst->QueryInterface(iid2, (void **)&pUnknown);

        So, now I need to get the Application and Document objects (assuming that this code creates a new document as it used to). I am guessing I need a getApplication() but what MS class do I use for that? Once I have the Application, then I can get the Document, presumably with a get_Documents() or get_ActiveDocument()?. After that I guess it will run like my older version used to. Any help and advice would be great. Many Thanks, Bryan.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        I've always done it like:

        _Application app;
        Documents docs;
        _Document doc;

        if (app.CreateDispatch("Word.Application") == TRUE)
        {
        docs = app.GetDocuments();

        doc = docs.Open(COleVariant(strFileName), ...);
        
        // do something with the document
        
        docs.Close(vtFalse, vtOptional, vtOptional);
        
        app.Quit(vtOptional, vtOptional, vtOptional);
        

        }

        Not sure if that helps you or not.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        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