[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

create an instance of Microsoft Word using Com Automation

Posted on 2004-10-08
9
Medium Priority
?
400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 45

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 1500 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

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

649 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