word automation

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
novakvAsked:
Who is Participating?
 
Amritpal SinghConnect With a Mentor Commented:
have a look at the following link
i think this may help

http://support.microsoft.com/default.aspx?scid=kb;EN-US;178784
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
novakvAuthor Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
novakvAuthor Commented:
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
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
novakvAuthor Commented:
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
 
novakvAuthor Commented:
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
 
novakvAuthor Commented:
sorry rProps(lpDisp); is not there...
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
novakvAuthor Commented:
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
 
novakvAuthor Commented:
increasing points since this is quite important for me...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.