Solved

create an instance of Microsoft Word using Com Automation

Posted on 2004-10-08
9
398 Views
Last Modified: 2013-11-20
hi everyone
   I wanna write a small form in c++(a simple dialog box) with 1 button on it.
   
   When i click on the button, create an instance of Microsoft Word using Com Automation, create a new document and write some text in the document.
0
Comment
Question by:leeseifer
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12257445
u can use shell command or shellexecute
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12257465
0
Industry Leaders: 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: 12257658
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"));

0
 
LVL 4

Accepted Solution

by:
roshkm earned 500 total points
ID: 12259871
I would like to add some more to AndyAinscow..

For the code to recognise _Application, you will have to create the cpp/h using ClassWizard.

ClassWizard-> Add Class-> From a type Library-> (installed Path of MSWord)/Offlice11/MSWORD.OLB-> Open

Select _application, Documents, _Document, Range from the dialog box.

You will get an MSWORD.cpp/.h in your workspace. This will hold the information of what is _application.. etc

Cheers,
RKM
0
 

Author Comment

by:leeseifer
ID: 12295402
Hey i did it... But How do i add bookmark automaticaly?????
0
 
LVL 4

Expert Comment

by:roshkm
ID: 12296473
Hi again,
U cannot add bookmark automatically, May be u can write something like this. You have to select the BookMark from the MSWORD.OLB


_Document document ;
Bookmarks oBks(document .GetBookmarks());

CString csBk;
csBk.Format("B_%d",nBkPos);

VARIANT vtBkRange;
vtBkRange.vt = VT_ERROR;
vtBkRange.scode = DISP_E_PARAMNOTFOUND;                        

oBks.Add(csBk,&vtBkRange);

Regards,
RKM
0
 

Author Comment

by:leeseifer
ID: 12304880
Hi there
    I try to add the exist document into Word.

docPtr = appPtr->Documents;
_docPtr = docPtr->Add();

These code will add blank Document. But i wanna add a exist file document.

I saw "(VARIANT *Template = &vnMissing)" in Add method ...How can i do with it?????

Thanks Advance
0
 
LVL 4

Expert Comment

by:roshkm
ID: 12305166
simple.. how do u do that in WinWord...Open it..!!

CString csFirstFile = "c:\Myfile.rtf";
vtFirstFile.bstrVal = csFirstFile.AllocSysString();

VARIANT vtNull, vtFalse;
vtNull.vt            = VT_ERROR            ;
vtNull.scode      = DISP_E_PARAMNOTFOUND      ;
vtFalse.vt            = VT_BOOL            ;
vtFalse.boolVal       = VARIANT_FALSE            ;

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

Documents oDocuments= application.GetDocuments();
_Document oDoc;

oDoc = oDocuments.Open(&vtFirstFile, &vtNull, &vtNull,&vtNull,&vtNull,&vtNull,&vtNull,&vtNull,&vtNull,&vtNull,&vtNull,&vtFalse,&vtFalse,&vtNull,&vtNull);

The Parameters will change with the MSWord version, like this is for MSWordXP, You have to add one more &vtNull for MSWord2003.
You will get used to it.. ;). AND Beware of memory leaks.

Cheers,
RKM


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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