Solved

word automation

Posted on 2004-10-14
15
231 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now