Solved

create an instance of Microsoft Word using Com Automation

Posted on 2004-10-08
9
394 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
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 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

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
WinWaitActive parameters 12 31
Prevent this page from creating additional dialogs. 3 388
ShiftLeft challenge 21 76
Making an alias 7 77
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

930 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

13 Experts available now in Live!

Get 1:1 Help Now