How correctly set User Properties to MS Outlook Mail Item in MFC
-
Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks
-
Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks
Member 2972992 wrote:
CString propertyName = _T("Email Marking");
outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here!Using a
CString
object is probably what is causing you grief."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Member 2972992 wrote:
CString propertyName = _T("Email Marking");
outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here!Using a
CString
object is probably what is causing you grief."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
No, I think - tatic BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; is correct because I have seen this in http://repositorium.googlecode.com/svn/trunk/AbstractSpoon/OutlookImpExp/Source/msoutl.cpp What should CString be?
-
No, I think - tatic BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; is correct because I have seen this in http://repositorium.googlecode.com/svn/trunk/AbstractSpoon/OutlookImpExp/Source/msoutl.cpp What should CString be?
Member 2972992 wrote:
What should CString be?
Without actually trying it, I honestly do not know. I'd try
VARIANT
andBSTR
to see what they produce. SinceInvokeHelper()
throws an exception, why are you not catching it and examining the actual cause?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Hi I have a problem with an MFC Project that opens an email template, and sets some user properties on the mail item. The code I have been given with is as follows: SetProperties() { LPDISPATCH userProps; outlookmailitem->InvokeHelper(0xf816, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&userProps, NULL); COleDispatchDriver* outlookUser = new COleDispatchDriver(userProps); LPDISPATCH userProp1; static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT; CString propertyName = _T("Email Marking"); outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, propertyName, 1); //falls here! COleDispatchDriver* outlookUserProp = new COleDispatchDriver(userProp1); static BYTE params2[] = VTS_VARIANT; VARIANT va1; va1.vt = VT_BSTR; va1.bstrVal = emailMarking.AllocSysString(); outlookUserProp->InvokeHelper(0x0, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, params2, &va1); delete(outlookUserProp); Where I have added "//falls here!" is where it fails, and I think this is parameter issue, again. I cannot tell you what the error message I am getting because all that VS2010 is giving me is unhandled exception. Looking at MSDN for VBA - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties.add.aspx#Y19 - it is final two parameters to me that Outlook is expecting, add to folders and display format. What I am trying to achieve is to ensure that the add to folder is set to false, and for the final one, I would like to set this to text if possible. How can set theses in MFC? What is the correct syntax? Do they need to set in this way or could Outlook set theses with the parameters being passed? Thanks
I would hazard a guess from the code that its the same issue you had before, regarding the passing of
[in, optional] VARIANT
parameters. Everytime you come across an[in, optional] VARIANT
parameter, what you have to pass is the address of a VARAIANT that contains type VT_ERROR, and value DISP_E_PARAMNOTFOUND. In this case, from thestatic BYTE params[]
declaration there are two "optional" parameters.COleVariant varOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
LPDISPATCH userProp1;
static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT;
CString propertyName = _T("Email Marking");
outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, (LPCTSTR)propertyName, 1, &varOptional, &varOptional);Notes: I have not looked up what dispatch method 0x66 is so I am not 100% sure. The presence of a VTS_VARIANT in the params array does not always mean optional, however an optional parameter is always a variant. The VTS_BSTR in the params array of an InvokeHelper call is expected to be an LPCTSTR. to allow the CString to pass the correct value always type cast it to (LPCTSTR). The type casting is neccessary because InvokeHelper is declared as
void AFX_CDECL InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet, void* pvRet, const BYTE* pbParamInfo, ...);
so the compiler doesnt know specifically what to pass. By type casting it you ensure the correct value is passed. -
I would hazard a guess from the code that its the same issue you had before, regarding the passing of
[in, optional] VARIANT
parameters. Everytime you come across an[in, optional] VARIANT
parameter, what you have to pass is the address of a VARAIANT that contains type VT_ERROR, and value DISP_E_PARAMNOTFOUND. In this case, from thestatic BYTE params[]
declaration there are two "optional" parameters.COleVariant varOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
LPDISPATCH userProp1;
static BYTE params[] = VTS_BSTR VTS_I4 VTS_VARIANT VTS_VARIANT;
CString propertyName = _T("Email Marking");
outlookUser->InvokeHelper(0x66, DISPATCH_METHOD, VT_DISPATCH, (void*)&userProp1, params, (LPCTSTR)propertyName, 1, &varOptional, &varOptional);Notes: I have not looked up what dispatch method 0x66 is so I am not 100% sure. The presence of a VTS_VARIANT in the params array does not always mean optional, however an optional parameter is always a variant. The VTS_BSTR in the params array of an InvokeHelper call is expected to be an LPCTSTR. to allow the CString to pass the correct value always type cast it to (LPCTSTR). The type casting is neccessary because InvokeHelper is declared as
void AFX_CDECL InvokeHelper(DISPID dwDispID, WORD wFlags, VARTYPE vtRet, void* pvRet, const BYTE* pbParamInfo, ...);
so the compiler doesnt know specifically what to pass. By type casting it you ensure the correct value is passed.Hi I have tried your suggestion and that did work! However, because one of parameters is a bool what is the correct syntax for passing a bool? Would it be COleVariantBool varOptionalBool (VT_TRUE, VT_ERROR); ? Further, when the line for saving the properties is called InvokeHelper(0xf048, DISPATCH_METHOD, VT_EMPTY, NULL, NULL); It gives an unhandled exception again. Looking at the MS Office VBA for this, properties don't save method, and it appears to me that when you add and set the properties this carries out the saving. Have I got this wrong? Is there something else I need to do? Thanks
-
Hi I have tried your suggestion and that did work! However, because one of parameters is a bool what is the correct syntax for passing a bool? Would it be COleVariantBool varOptionalBool (VT_TRUE, VT_ERROR); ? Further, when the line for saving the properties is called InvokeHelper(0xf048, DISPATCH_METHOD, VT_EMPTY, NULL, NULL); It gives an unhandled exception again. Looking at the MS Office VBA for this, properties don't save method, and it appears to me that when you add and set the properties this carries out the saving. Have I got this wrong? Is there something else I need to do? Thanks
To pass a bool use one of these
COleVariant varTRUE((short)-1,VT\_BOOL); COleVariant varFALSE((short)0,VT\_BOOL);
I am unsure why Save (0xf048) would cause an exception. What is the exception ? I am also unsure why you dont use type-safe methods of invoking Outlook. Open the Class Wizard dialog "key combination Ctrl+W". Click "Add Class...". Click "From a type library...". Locate your MSOUTLxx.OLB, mine is in "C:\Program Files\Microsoft Office\Office12" click Open. Select the appropriate classes, or all of them, and click OK. This will add to your project header and implementation files that, in my humble opinion, will make your life easier.