Link to home
Start Free TrialLog in
Avatar of leeseifer
leeseifer

asked on

create an instance of Microsoft Word using Com Automation

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.
Avatar of Amritpal Singh
Amritpal Singh
Flag of India image

u can use shell command or shellexecute
Avatar of AndyAinscow
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"));

ASKER CERTIFIED SOLUTION
Avatar of roshkm
roshkm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leeseifer
leeseifer

ASKER

Hey i did it... But How do i add bookmark automaticaly?????
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
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
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