Click here to Skip to main content
15,914,013 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to recognize word from sentence? Pin
Shaun Wilde17-Sep-02 5:20
Shaun Wilde17-Sep-02 5:20 
GeneralRe: how to recognize word from sentence? Pin
Bart Robeyns17-Sep-02 5:30
Bart Robeyns17-Sep-02 5:30 
GeneralRe: how to recognize word from sentence? Pin
Shaun Wilde17-Sep-02 5:39
Shaun Wilde17-Sep-02 5:39 
GeneralRe: how to recognize word from sentence? Pin
wey_horng17-Sep-02 7:30
wey_horng17-Sep-02 7:30 
GeneralRe: how to recognize word from sentence? Pin
Ryan B.17-Sep-02 9:01
Ryan B.17-Sep-02 9:01 
Generalparsing word from sentence? Pin
wey_horng18-Sep-02 4:05
wey_horng18-Sep-02 4:05 
Generalscrollbars in a richedit Pin
ns17-Sep-02 4:36
ns17-Sep-02 4:36 
GeneralWord in C++ Pin
insanely42017-Sep-02 4:24
insanely42017-Sep-02 4:24 
Greetings

I have been able to automate Word using my application. I have been able to write three lines of text into it.

Now how should I create tables and format text in it.

Now how should I add tables for formatting.

Ritesh

Below is the code which i am using
=============================================================================
#include <ole2.h>
#include <stdio.h>

int main(int argc, char* argv[])
{

// ******************* Declare Some Variables ********************

// Variables that will be used and re-used in our calls
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
OLECHAR FAR* szFunction;
BSTR bstrTemp;

// IDispatch pointers for Word's objects
IDispatch* pDispDocs; //Documents collection
IDispatch* pDispSel; //Selection object
IDispatch* pDispActiveDoc; //ActiveDocument object

// DISPID's
DISPID dispid_Docs; //Documents property of Application object
DISPID dispid_DocsAdd; //Add method of Documents collection
//object
DISPID dispid_Sel; //Selection property of Applicaiton object
DISPID dispid_TypeText; //TypeText method of Selection object
DISPID dispid_TypePara; //TypeParagraph method of Selection object
DISPID dispid_ActiveDoc; //ActiveDocument property of Application
//obj
DISPID dispid_SaveAs; //SaveAs method of the Document object
DISPID dispid_Quit; //Quit method of the Application object

// ******************** Start Automation ***********************

//Initialize the COM libraries
::CoInitialize(NULL);

// Create an instance of the Word application and obtain the pointer
// to the application's IDispatch interface.
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid);

IUnknown* pUnk;
HRESULT hr = ::CoCreateInstance( clsid, NULL, CLSCTX_SERVER,
IID_IUnknown, (void**) &pUnk);
IDispatch* pDispApp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDispApp);

// Get IDispatch* for the Documents collection object
szFunction = OLESTR("Documents");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Docs);
hr = pDispApp->Invoke (dispid_Docs, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &dpNoArgs, &vResult,
NULL, NULL);
pDispDocs = vResult.pdispVal;

// Invoke the Add method on the Documents collection object
// to create a new document in Word
// Note that the Add method can take up to 3 arguments, all of which
// are optional. We are not passing it any so we are using an empty
// DISPPARAMS structure
szFunction = OLESTR("Add");
hr = pDispDocs->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_DocsAdd);
hr = pDispDocs->Invoke(dispid_DocsAdd, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dpNoArgs, &vResult, NULL,
NULL);


// Get IDispatch* for the Selection object
szFunction = OLESTR("Selection");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Sel);
hr = pDispApp->Invoke (dispid_Sel, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, &dpNoArgs, &vResult,
NULL, NULL);
pDispSel = vResult.pdispVal;


// Get the DISPIDs of the TypeText and TypeParagraph methods of the
// Selection object. We'll use these DISPIDs multiple times.
szFunction = OLESTR("TypeText");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_TypeText);

szFunction = OLESTR("TypeParagraph");
hr = pDispSel->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_TypePara);

// The TypeText method has and requires only one argument, a string,
// so set up the DISPPARAMS accordingly
VARIANT vArgsTypeText[1];
DISPPARAMS dpTypeText;

bstrTemp = ::SysAllocString(OLESTR("Metallica"));
vArgsTypeText [0].vt = VT_BSTR;
vArgsTypeText [0].bstrVal = bstrTemp;
dpTypeText.cArgs = 1;
dpTypeText.cNamedArgs = 0;
dpTypeText.rgvarg = vArgsTypeText;

//Invoke the first TypeText and TypeParagraph pair
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);

//Invoke the second TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Pantera"));
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);

//Invoke the third TypeText and TypeParagraph pair
bstrTemp = ::SysAllocString(OLESTR("Godsmack"));
hr = pDispSel->Invoke (dispid_TypeText, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpTypeText, NULL, NULL, NULL);
hr = pDispSel->Invoke (dispid_TypePara, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpNoArgs, NULL, NULL, NULL);
::SysFreeString(bstrTemp);

// Get IDispatch* for the ActiveDocument object
szFunction = OLESTR("ActiveDocument");
hr = pDispApp->GetIDsOfNames (IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT,
&dispid_ActiveDoc);
hr = pDispApp->Invoke (dispid_ActiveDoc, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
&dpNoArgs, &vResult, NULL, NULL);
pDispActiveDoc = vResult.pdispVal;

//Set up the DISPPARAMS for the SaveAs method (11 arguments)
VARIANT vArgsSaveAs[11];
DISPPARAMS dpSaveAs;
dpSaveAs.cArgs = 11;
dpSaveAs.cNamedArgs = 0;
dpSaveAs.rgvarg = vArgsSaveAs;

BSTR bstrEmptyString;
bstrEmptyString = ::SysAllocString(OLESTR(""));

VARIANT vFalse;
vFalse.vt = VT_BOOL;
vFalse.boolVal = FALSE;

bstrTemp = ::SysAllocString(OLESTR("c:\\doc1.doc"));
vArgsSaveAs[10].vt = VT_BSTR;
vArgsSaveAs[10].bstrVal = bstrTemp; //Filename
vArgsSaveAs[9].vt = VT_I4;
vArgsSaveAs[9].lVal = 0; //FileFormat
vArgsSaveAs[8] = vFalse; //LockComments
vArgsSaveAs[7].vt = VT_BSTR;
vArgsSaveAs[7].bstrVal = bstrEmptyString; //Password
vArgsSaveAs[6].vt = VT_BOOL;
vArgsSaveAs[6].boolVal = TRUE; //AddToRecentFiles
vArgsSaveAs[5].vt = VT_BSTR;
vArgsSaveAs[5].bstrVal = bstrEmptyString; //WritePassword
vArgsSaveAs[4] = vFalse; //ReadOnlyRecommended
vArgsSaveAs[3] = vFalse; //EmbedTrueTypeFonts
vArgsSaveAs[2] = vFalse; //SaveNativePictureFormat
vArgsSaveAs[1] = vFalse; //SaveFormsData
vArgsSaveAs[0] = vFalse; //SaveAsOCELetter


//Invoke the SaveAs method
szFunction = OLESTR("SaveAs");
hr = pDispActiveDoc->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_SaveAs);
hr = pDispActiveDoc->Invoke(dispid_SaveAs, IID_NULL,
LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dpSaveAs, NULL, NULL, NULL);
::SysFreeString(bstrEmptyString);

//Invoke the Quit method
szFunction = OLESTR("Quit");
hr = pDispApp->GetIDsOfNames(IID_NULL, &szFunction, 1,
LOCALE_USER_DEFAULT, &dispid_Quit);
hr = pDispApp->Invoke (dispid_Quit, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, &dpNoArgs, NULL, NULL, NULL);

//Clean-up
::SysFreeString(bstrTemp);
pDispActiveDoc->Release();
pDispSel->Release();
pDispDocs->Release();
pDispApp->Release();
pUnk->Release();

::CoUninitialize();

return 0;

}
GeneralRe: Word in C++ Pin
jmkhael17-Sep-02 6:48
jmkhael17-Sep-02 6:48 
GeneralRe: Word in C++ Pin
Bill Wilson17-Sep-02 10:27
Bill Wilson17-Sep-02 10:27 
GeneralRe: Word in C++ Pin
insanely42017-Sep-02 11:00
insanely42017-Sep-02 11:00 
GeneralWinSock: How do I get the Host Name Pin
Robert Scheckle17-Sep-02 3:44
Robert Scheckle17-Sep-02 3:44 
GeneralRe: WinSock: How do I get the Host Name Pin
Bart Robeyns17-Sep-02 4:43
Bart Robeyns17-Sep-02 4:43 
GeneralWorkspace Model Pin
Ryan B.17-Sep-02 3:41
Ryan B.17-Sep-02 3:41 
GeneralRe: Workspace Model Pin
Ryan B.17-Sep-02 8:54
Ryan B.17-Sep-02 8:54 
GeneralRe: Workspace Model Pin
Bill Wilson17-Sep-02 11:21
Bill Wilson17-Sep-02 11:21 
Questionhow to get input from user Pin
ns17-Sep-02 3:04
ns17-Sep-02 3:04 
AnswerRe: how to get input from user Pin
Ravi Bhavnani17-Sep-02 3:31
professionalRavi Bhavnani17-Sep-02 3:31 
GeneralRe: how to get input from user Pin
ns17-Sep-02 3:38
ns17-Sep-02 3:38 
GeneralRe: how to get input from user Pin
Ravi Bhavnani17-Sep-02 3:36
professionalRavi Bhavnani17-Sep-02 3:36 
GeneralRe: how to get input from user Pin
ns17-Sep-02 3:59
ns17-Sep-02 3:59 
QuestionBIG OR LITTLE QUESTION? Pin
ZBUILDER17-Sep-02 2:46
ZBUILDER17-Sep-02 2:46 
AnswerRe: BIG OR LITTLE QUESTION? Pin
jhwurmbach17-Sep-02 2:54
jhwurmbach17-Sep-02 2:54 
AnswerRe: BIG OR LITTLE QUESTION? Pin
Bart Robeyns17-Sep-02 2:57
Bart Robeyns17-Sep-02 2:57 
GeneralRe: BIG OR LITTLE QUESTION? Pin
ZBUILDER17-Sep-02 3:08
ZBUILDER17-Sep-02 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.