I'm using Word automation and I want to get the document property that
tells me the last time the document was saved (wdPropertyTimeLastSaved).
I've found some Delphi samples of how to do this, but I'm having trouble
getting
it converted to C++. Anything I get to compile bombs at runtime. I'm
obviously
not doing something right. Does anyone have a C++ code sample that does
this?
Thanks to anyone who can help.
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Variant myword;
Variant docs;
Variant doc;
Variant builtInProp;
try{
try{
myword= Variant::GetActiveObject("word.application");
docs = myword.OlePropertyGet("Documents") ;
doc = myword.OlePropertyGet("ActiveDocument") ;
builtInProp = doc.OlePropertyGet("BuiltInDocumentProperties");
PropertyGet Item("Item") ;
Item.ClearArgs() ;
TDateTime tt = builtInProp.Exec(Item << (Variant)12); // 12 is the
index of wdPropertyTimeLastSaved
ShowMessage(tt.DateTimeString() );
}
__finally{
myword = Unassigned;
docs = Unassigned;
doc = Unassigned;
builtInProp = Unassigned;
}
}
catch(...){
myword = Unassigned;
docs = Unassigned;
doc = Unassigned;
builtInProp = Unassigned;
return;
}
}
//---------------------------------------------------------------------------
-Minas Charokopos
Looking at your sample code, should I be able to do this:
builtInProp = TESTWordDocument->BuiltInDocumentProperties;
in place of:
builtInProp = doc.OlePropertyGet("BuiltInDocumentProperties");
It does compile, but I'm thinking that maybe assigning IDispatch* to a
Variant isn't going in the right direction
I get a runtime error (with your code or mine) with this line:
TDateTime tt = builtInProp.Exec(Item << (Variant)12);
The error is:
"Could not convert variant of type (Dispatch) into type (Date).
in your first post said about Word automation that's why I posted that code
which uses Ole Automation . TWordApplication,TWordDocument etc components
are the ATL version of Automation objects. I use Ole Automation instead of
ATL and I see now that you are referring to OleServer components.
Can you,plz, post the part of code you are talking about ?
-minas
Main.h contains this:
TWordDocument *TESTWordDocument;
TWordApplication *TESTWordApplication;
OleVariant vFileName;
Main.cpp contains this:
AnsiString tempExtractionFile = "testfile";
IDispatch *pBuiltInProp;
vFileName = tempExtractionFile;
TESTWordDocument->ConnectTo(TESTWordApplication->Documents->Open(vFileName))
;
pBuiltInProp = TESTWordDocument->BuiltInDocumentProperties;
I'm not exactly sure how to proceed with pBuiltInProp. IDispatch is,
I think, an abstract class.
I've found some sample code that suggests I could do use something like
this:
?? =
TESTWordDocument->BuiltInDocumentProperties[wdPropertyTimeLastSaved];
But, I'm not sure what to use for ??. It's been suggested that I
typecast IDispatch* to something
else as BuiltInDocumentProperties may actually be returning something
other than IDispatch*.
I know that TESTWordDocument is successfully connected to a document
because I can
get and use other properties with no problem (Name, CodeName, Sentences,
etc...). It's
this IDispatch thing that is giving me fits for some reason.
Does this help you?
"HF" <(min_charATyahooPUTADOTgr)(HellenicFire)> wrote in message
news:440f...@newsgroups.borland.com...
i wrote this and is working to me
Is not optimized , just to show the steps. Put your try{} catch{} to handle
exceptions .
In certain points (case failure put your return commands).
Use IDispatch interface ( GetIDsOfNames, Invoke) to obtain the properties,
first with property name Item which will give us a new IDispatch and later
on that interface using property name Value to get the final value.
//
TESTWordDocument->ConnectTo(TESTWordApplication->Documents->Open(vFileName));
//
// *********************************************
wchar_t *szMember = L"Item";
DISPID dispid;
VARIANT FAR VarData;
HRESULT hresult =
WordApplication->ActiveDocument->BuiltInDocumentProperties->GetIDsOfNames(IID_NULL,
&szMember, 1, LOCALE_USER_DEFAULT, &dispid);
switch (hresult)
{
case E_OUTOFMEMORY: ShowMessage("Out of Memory");break;
case DISP_E_UNKNOWNNAME: ShowMessage("Unknown Name...");break;
case DISP_E_UNKNOWNLCID: ShowMessage("The LCID was not
recognized");break;
}
VARIANTARG param ;
VariantInit(¶m);
DISPPARAMS dispparams; // the list of parameters
memset(&dispparams, 0, sizeof(DISPPARAMS));
dispparams.rgvarg = ¶m;
dispparams.rgvarg[0].vt = VT_INT;
dispparams.rgvarg[0].intVal = wdPropertyTimeLastSaved; //
wdPropertyAuthor,wdPropertyTitle
dispparams.cArgs = 1;
dispparams.cNamedArgs =0;
dispparams.rgdispidNamedArgs = NULL;
VARIANT result;
VariantInit(&result);
VARIANT valResult;
VariantInit(&valResult);
hresult =
WordApplication->ActiveDocument->BuiltInDocumentProperties->Invoke(dispid,
IID_NULL, LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET, &dispparams, &result, 0,
NULL);
if (result.vt == VT_DISPATCH){
szMember = L"Value";
result.pdispVal->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_USER_DEFAULT, &dispid);
switch (hresult)
{
case E_OUTOFMEMORY: ShowMessage("Out of Memory");break;
case DISP_E_UNKNOWNNAME: ShowMessage("Unknown Name...");break;
case DISP_E_UNKNOWNLCID: ShowMessage("The LCID was not
recognized");break;
}
DISPPARAMS params; // the list of parameters
memset(¶ms, 0, sizeof(DISPPARAMS));
hresult = result.pdispVal->Invoke(dispid, IID_NULL,
LOCALE_USER_DEFAULT,DISPATCH_PROPERTYGET, ¶ms, &valResult, 0, NULL);
}
/*
// Check for errors
switch (hresult)
{
case DISP_E_BADPARAMCOUNT:ShowMessage("DISP_E_BADPARAMCOUNT") ;break;
case DISP_E_BADVARTYPE:ShowMessage("DISP_E_BADVARTYPE") ;break;
case DISP_E_EXCEPTION:ShowMessage("DISP_E_EXCEPTION") ;break;
case DISP_E_MEMBERNOTFOUND:ShowMessage("DISP_E_MEMBERNOTFOUND") ;break;
case DISP_E_NONAMEDARGS:ShowMessage("DISP_E_NONAMEDARGS") ;break;
case DISP_E_OVERFLOW:ShowMessage("DISP_E_OVERFLOW") ;break;
case DISP_E_PARAMNOTFOUND:ShowMessage("DISP_E_PARAMNOTFOUND") ;break;
case DISP_E_TYPEMISMATCH:ShowMessage("DISP_E_TYPEMISMATCH") ;break;
case DISP_E_UNKNOWNINTERFACE:ShowMessage("DISP_E_UNKNOWNINTERFACE")
;break;
case DISP_E_UNKNOWNLCID:ShowMessage("DISP_E_UNKNOWNLCID") ;break;
case DISP_E_PARAMNOTOPTIONAL:
ShowMessage("DISP_E_PARAMNOTOPTIONAL");break;
}
*/
// in this case valResult.vt = = VT_DATE
TDateTime dt = valResult.date ;
ShowMessage(dt.DateTimeString());
// *********************************************
-Minas Charokopos
This worked just fine. It gave me exactly what I needed. I appreciate
the help!
...Charlie
"HF" <(min_charATyahooPUTADOTgr)(HellenicFire)> wrote in message
news:4411...@newsgroups.borland.com...