Solved

word automation

Posted on 2004-10-14
15
243 Views
Last Modified: 2013-11-20
Hi all,
I spend long time trying handle Word document form MFC dialog aplication. I have found some complicated solutions and no documentation.

I need to open document, change something and print it out. Is there a simple way to do this simple task? Is there any documentation on this?

thanx
Vit
0
Comment
Question by:novakv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
15 Comments
 
LVL 6

Accepted Solution

by:
Amritpal Singh earned 250 total points
ID: 12307824
have a look at the following link
i think this may help

http://support.microsoft.com/default.aspx?scid=kb;EN-US;178784
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 12308154
Use the class wizard to add files from a type library (MSWORD.OLB).

This will create a new doc and put some text in it
 Give this a try

COleVariant var;
_Application application;

var.vt = VT_ERROR;
var.scode = DISP_E_PARAMNOTFOUND;

application.CreateDispatch(_T("word.application"));
application.SetVisible(Visible);

Documents documents = application.GetDocuments();
_Document document = documents.Add(var,var);

Range range = document.Range(var,var);
range.InsertAfter(_T("hello"));


The document has an Open command - you can use that to open an existing doc.

It also has a PrintOut command (guess what that does).
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12308174
It is also worth doing a search here - try search on word automation and look at the MFC area.  There are plenty of code snippets to be found.
0
 

Author Comment

by:novakv
ID: 12316390
to AndyAinscow
yes I know, but what you wrote - Visible is undefinied (I tryied TRUE) and documents.Add takes 4 parameters not 2. And I have no idea what they means and what values are enabled - everything is variant and where is the doc?

I could find some examples but no reference manual or someting. I found this link on MSDN: http://support.microsoft.com/support/officedev/automation.asp but its dead.

> It is also worth doing a search here - try search on word automation and look at the MFC area.  >There are plenty of code snippets to be found.

You know I wold love to search here just if I had the premium membership :-)
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12316481
The parameter count varies with each version of word.


<The document has an Open command - you can use that to open an existing doc.> - the doc is an existing winword document on your hard disc, you supply the path as one of the params of the open call.  Yes params supplied to the functions are usually as variant.  You will see in the generated .cpp/.h files the params required.   ***Important, typo - it is the Documents (plural) that has the Open command).



'You know I wold love to search here just if I had the premium membership' - sorry I didn't realise search wasn't available as a free service.  The hard way is to look at the list of closed questions, word automation seems to be a not uncommon topic.
There should be a number of automating word examples on the microsoft site.  Try "word automation" AND MFC as the search params.
0
 

Author Comment

by:novakv
ID: 12316584
It's not so simple to find

application.CreateDispatch(_T("word.application")); returns NULL, is the Word.application correct for MSO 2003? What could be the problem?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12316847
This works for various word versions

      _Application app;  

      COleVariant covTrue((short)TRUE);
      COleVariant covFalse((short)FALSE);
      COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
      
      LPDISPATCH pDisp;
      LPUNKNOWN pUnk;
      CLSID clsid;

      ::CLSIDFromProgID(L"Word.Application", &clsid); // from registry
      if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
      {
            TRACE0("attaching\n");
            VERIFY(pUnk->QueryInterface(IID_IDispatch, (void**) &pDisp) == S_OK);
            app.AttachDispatch(pDisp);
            pUnk->Release();
      }
      else
      {

            TRACE0("creating\n");
            if(!app.CreateDispatch(clsid))
            {
                  TRACE0("Attatching after create\n");
                  if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
                  {
                        TRACE0("attaching after create - with the prog\n");
                        VERIFY(pUnk->QueryInterface(IID_IDispatch, (void**) &pDisp) == S_OK);
                        app.AttachDispatch(pDisp);
                        pUnk->Release();
                  }
                  else
                  {
                        AfxMessageBox(IDS_5_ERR_UNK, MB_OK | MB_ICONSTOP);
                        return false;
                  }
            }
      }
0
 

Author Comment

by:novakv
ID: 12316865
Yes, its working I just forgot to do the CoInitialize(NULL) at the beginning and I am printing :-)

Now I need to change the document: possibly open a template and put some text on particular places (table). For example name and adress...
0
 

Author Comment

by:novakv
ID: 12319355
What I need is to create (manualy) a document with custom properties and connect some fields in the document with these properties. Then when I open the document, I need to set these properties and update the fields before PrintOut is called.

I found an example how to read properties but its real pain with GetIDsOfNames and InvokeHelper methods and even that it works I have no Idea how to set the properties.

I also found CustomProperties class in my generated code (in msword.h) but when I try this:

  LPDISPATCH  lpDisp;
  lpDisp = testDoc.GetCustomDocumentProperties();
  CustomProperties rProp(lpDisp);
  rProps(lpDisp);
  long lCount = rProp.GetCount();

an exception is raised and messagebox shown: Member not found

please help
0
 

Author Comment

by:novakv
ID: 12319379
sorry rProps(lpDisp); is not there...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12319922
This is some VB code about Customproperties
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbawd10/html/woobjcustomproperties.asp
You should be able to understand the logic code similar in C++
0
 

Author Comment

by:novakv
ID: 12336680
It didn't really help me much. I don't know how the smart tags bear on the document properties. Anyway the C++ interface looks a bit diffrent then the VB one.

when I try this:
-----
  LPDISPATCH  lpDisp;
  long lCount;

  lpDisp = testDoc.GetSmartTags();

  SmartTags rTags(lpDisp);
  lCount = rTags.GetCount();
-----
lCount is zero.

and this:
-----
 LPDISPATCH  lpDisp;
  lpDisp = testDoc.GetCustomDocumentProperties();
  CustomProperties rProp(lpDisp);
  long lCount = rProp.GetCount();
-----
then an exception is raised as I wrote before.
I am quite desperate...
0
 

Author Comment

by:novakv
ID: 12345498
increasing points since this is quite important for me...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question