Solved

word automation

Posted on 2004-10-14
15
241 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
  • 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 6

Expert Comment

by:Amritpal Singh
ID: 12307836
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 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