Solved

word automation

Posted on 2004-10-14
15
232 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to retreive data from .TFS file exported by DVR 8 177
ShiftLeft challenge 21 76
wordcount challenge 11 106
Excel file not created as expected 7 48
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

911 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

18 Experts available now in Live!

Get 1:1 Help Now