Solved

create an instance of Microsoft Word using Com Automation

Posted on 2004-10-08
9
393 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
  • 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: 12257458
0
 
LVL 6

Expert Comment

by:Amritpal Singh
ID: 12257465
0
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now