Brades

I would like to know if it is possible to create an instance of Word and pass the "-a" parameter/switch (Start Word and prevent add-ins )

CDispatch oWord;

try
{
CP(_T("GenerateWithWord ------> Attempting to create Word Application"));
oWord.CreateInstance("Word.Application");
}
catch (...)
{
CP(_T("GenerateWithWord ------> Unhandled Exception occurred!"));
throw CMAException(TRANSLATE(_T("NOWORD")));
}


Any Ideas


Re: Visual C++ General Question on Automating Word Passing Parameters

Simple Samples

You can either create a process to execute Word or use automation. CDispatch is for using automation, and you probably don't need to do that. See Processes and Threads for the documentation of creating processes.






Re: Visual C++ General Question on Automating Word Passing Parameters

Sahir Shah

Brades wrote:
I would like to know if it is possible to create an instance of Word and pass the "-a" parameter/switch (Start Word and prevent add-ins )

Starting Word with /a switch does not prevent add-ins it just starts Word without the add-ins. You can do the same thing using automation by iterating though the Application object's AddIns collection and calling delete on each AddIn object.





Re: Visual C++ General Question on Automating Word Passing Parameters

Simple Samples

Sahir Shah wrote:

Starting Word with -a switch does not prevent add-ins it just starts Word without the add-ins. You can do the same thing using automation by iterating though the Application object's AddIns collection and calling delete on each AddIn object.

Assuming that use of the "-a" parameter is what is needed, regardless of what it does, then is it easier to use automation instead than to use ShellExecute or CreateProcess






Re: Visual C++ General Question on Automating Word Passing Parameters

Brades

Sahir, thank you for your reply. I am a relative new comer to C++ and would be very grateful if you could give me an code example of how to do this.




Re: Visual C++ General Question on Automating Word Passing Parameters

Sahir Shah

OK. Since you did not post the version of office you want it for we shall use the method described here : http://support.microsoft.com/kb/q216686/

Though this method is a bit tedious, it has an advantage of being able to work with any version of Office and also the ability to make a graceful exit if Word is not installed on the target machine.

Code Block

int _tmain(int argc, _TCHAR* argv[]) {

VARIANT wdApp = {0};
VARIANT wdAddIns = {0}; // collection of addins
VARIANT wdAddIn = {0}; // an add in object
VARIANT vCount = {0};
VARIANT lgVariant = {0};
OleInitialize(NULL);
CLSID clsid;
CLSIDFromProgID(L"Word.Application", &clsid);
HRESULT hr = CoCreateInstance(clsid,
NULL,
CLSCTX_LOCAL_SERVER|CLSCTX_INPROC_SERVER,
IID_IDispatch,
(void **)&wdApp.pdispVal);
if( hr != S_OK) {
cout<<"CoCreateInstance failed."<<endl;
return 0;
}
VariantClear(&lgVariant);
// set visible to true
lgVariant.vt = VT_I4;
lgVariant.lVal = 1;
AutoWrap(DISPATCH_PROPERTYPUT,
NULL, wdApp.pdispVal, L"Visible", 1, lgVariant);

AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD,
&wdAddIns, wdApp.pdispVal, L"AddIns", 0);

AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD,
&vCount, wdAddIns.pdispVal, L"Count", 0);

for(long j = 0; j < vCount.lVal; ++j) {
AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD,
&wdAddIn, wdAddIns.pdispVal, L"Item", 1, lgVariant);
// use a constant value of 1 for index
AutoWrap(DISPATCH_METHOD, NULL,
wdAddIn.pdispVal, L"Delete", 0);
}
cout<<"ok done!"<<endl;
getch();
return 0;
}

The AutoWrap function is in the KB article mentioned above. One thing to remember when deleting from the collection : the index values get "pushed up" as you delete each item, i.e. when you delete item 1 item 2 becomes item 1 and so on, so we need to use a constant index value of 1 while iterating through the collection.

Regards

Sahir Shah